package FileText;
import java.io.File;
import java.io.IOException;
public class File01 {
public static void main(String args[]) throws IOException{
FileText();
CreateFile();
DeleteFile();
}
public static void FileText(){//File的相对路径以及绝对路径
File file = new File("D:/text/dd/text.txt");//绝对路径
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.exists());
System.out.println("##########################");
File file2 = new File("D:/text");
File file2s = new File(file2,"dd/text.txt");//相对路径
System.out.println(file2s.getName());
System.out.println(file2s.getPath());
System.out.println(file2s.exists());
}
public static void CreateFile() throws IOException{//创建文件
String str = "D:/text/dd/word.txt";
File file = new File(str);
if(!file.exists()){
//newfile 为boolean型
//file.createNewFile() 用于创建文件
boolean newfile = file.createNewFile();
System.out.println(newfile?"创建成功":"创建失败");
}
}
public static void DeleteFile(){//删除文件
String str = "D:/text/dd/word.txt";
File file = new File(str);
if(file.exists()){
boolean deletefile = file.delete();
System.out.println(deletefile?"删除成功":"删除失败");
}
}
}