JAVA io流筆記01 File的建立
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?"刪除成功":"刪除失敗");
}
}
}