如何创建一个文件夹,文件和以及他们如何删除?

xiaming 2001-08-08 08:26:45
...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2001-08-08
  • 打赏
  • 举报
回复
对看看java.io.File类。
lafare 2001-08-08
  • 打赏
  • 举报
回复
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:\ccccc\a"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");
}
}
hexiaofeng 2001-08-08
  • 打赏
  • 举报
回复
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");
}




}
}
bobosji 2001-08-08
  • 打赏
  • 举报
回复
java.io.File类应该能满足你的要求

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧