当前位置: 技术问答>java相关
如何创建一个文件夹,文件和以及他们如何删除?
来源: 互联网 发布时间:2015-01-22
本文导语: | import java.io.*; /* create tmp file */ public class tmp { public static void main(String args[]) { try { File fff=new File("."); fff=null; //now directory File tmpfile=File.createTempFile("tmp",".tmp",fff)...
|
import java.io.*;
/*
create tmp file
*/
public class tmp
{
public static void main(String args[])
{
try
{
File fff=new File(".");
fff=null; //now directory
File tmpfile=File.createTempFile("tmp",".tmp",fff);//create tmp file
FileOutputStream fout=new FileOutputStream(tmpfile);
PrintStream out=new PrintStream(fout);
out.println("aaaaaaaaaaaaaaaaaaaaaaaa");//write to tmp file
tmpfile.deleteOnExit();//del tmp file
System.out.println("exit");
System.exit(0);
}
catch(IOException ii)
{
System.out.println("can not create tmp file !");
}
catch(IllegalArgumentException eee)
{
System.out.println("ERROR");
}
catch(SecurityException ee)
{
System.out.println("ERROR");
}
}
}
/*
create tmp file
*/
public class tmp
{
public static void main(String args[])
{
try
{
File fff=new File(".");
fff=null; //now directory
File tmpfile=File.createTempFile("tmp",".tmp",fff);//create tmp file
FileOutputStream fout=new FileOutputStream(tmpfile);
PrintStream out=new PrintStream(fout);
out.println("aaaaaaaaaaaaaaaaaaaaaaaa");//write to tmp file
tmpfile.deleteOnExit();//del tmp file
System.out.println("exit");
System.exit(0);
}
catch(IOException ii)
{
System.out.println("can not create tmp file !");
}
catch(IllegalArgumentException eee)
{
System.out.println("ERROR");
}
catch(SecurityException ee)
{
System.out.println("ERROR");
}
}
}
|
java.io.File类应该能满足你的要求
|
try this
good luck to you!
:)
import java.io.*;
import java.lang.*;
public class helloWorld {
File directory;
public helloWorld() {
System.out.println("this is the hello world test for make directory and delete it");
}
public void createDir(String dir) {
if(dir==null) {
directory = new File("d:/newDir/new/temp");
}
else {
directory = new File(dir);
}
/** create new Directory:
* create new file then use: directory.createNewFile(dir);
* for example: (delete the file d:/cccc/1.txt)
* directory=new File("d:/cccc","1.txt");
* directory.delete();
*/
boolean flagCreate=directory.mkdirs();
System.out.print("Create Directory: "+ directory.toString() +"--------n" + flagCreate+"n");
}
public void deleteDir(String dir) {
if(dir==null) {
directory = new File("d:/newDir/new/temp");
}
else {
directory = new File(dir);
}
if (directory.exists()&& directory.listFiles().length ==0) {
/* delete the directory if it is blank
in this example it only del the directory "d:ccccca"but the "d:/ccccc" still exist */
boolean flagDelete=directory.delete() ;
System.out.print("Delete Directory: "+ directory.toString() +"--------n" +flagDelete +"n");
}
}
public static void main(String args[]) {
helloWorld tt=new helloWorld();
tt.createDir("d:/ccccc/a");
tt.deleteDir("d:/ccccc/a");
}
}
good luck to you!
:)
import java.io.*;
import java.lang.*;
public class helloWorld {
File directory;
public helloWorld() {
System.out.println("this is the hello world test for make directory and delete it");
}
public void createDir(String dir) {
if(dir==null) {
directory = new File("d:/newDir/new/temp");
}
else {
directory = new File(dir);
}
/** create new Directory:
* create new file then use: directory.createNewFile(dir);
* for example: (delete the file d:/cccc/1.txt)
* directory=new File("d:/cccc","1.txt");
* directory.delete();
*/
boolean flagCreate=directory.mkdirs();
System.out.print("Create Directory: "+ directory.toString() +"--------n" + flagCreate+"n");
}
public void deleteDir(String dir) {
if(dir==null) {
directory = new File("d:/newDir/new/temp");
}
else {
directory = new File(dir);
}
if (directory.exists()&& directory.listFiles().length ==0) {
/* delete the directory if it is blank
in this example it only del the directory "d:ccccca"but the "d:/ccccc" still exist */
boolean flagDelete=directory.delete() ;
System.out.print("Delete Directory: "+ directory.toString() +"--------n" +flagDelete +"n");
}
}
public static void main(String args[]) {
helloWorld tt=new helloWorld();
tt.createDir("d:/ccccc/a");
tt.deleteDir("d:/ccccc/a");
}
}
|
对看看java.io.File类。