標籤:apach xxxxx file類 exit tabs exist 虛擬機器 bsp 下載
之前一直沒去學過IO,現在要寫上傳下載,必須得看了。
File類
public class FileTest
{
public static void main(String[] args)
throws IOException
{
// 以當前路徑來建立一個File對象
//建立一個file的對象,這個對象,代表的是一個路徑,“.”是目前的目錄的相對路徑。
//File類,使用檔案路徑字串來建立File執行個體,該字串可以是絕對路徑,也可以是相對路徑
File file = new File(".");
// 直接擷取檔案名稱,輸出一點
System.out.println(file.getName());
// 擷取相對路徑的父路徑可能出錯,下面代碼輸出null
System.out.println(file.getParent());
// 擷取絕對路徑
//這裡的路徑是java虛擬機器啟動並執行所在地址,這裡輸出的是tomcat的bin目錄位址
System.out.println(file.getAbsoluteFile());
// 擷取上一級路徑
System.out.println(file.getAbsoluteFile().getParent());
// 在當前路徑下建立一個臨時檔案
//現在就真的建立了一個檔案 aaaXXXXXXXXX.txt
//File tmpFile = File.createTempFile("aaa", ".txt", file);
}
}
File 的 exits()方法
File file = new File(".");
System.out.println(file.exists());// true 當前路徑存在,返回true
File file1=new File("test.txt");
System.out.println(file1.exists());//true 當前路徑有test.txt檔案,返回true
File file2=new File("test");
System.out.println(file2.exists());// false 當前路徑沒有test 檔案,返回false
File file3=new File("content.txt");
System.out.println(file3.exists());//false 當前路徑沒有content.txt,返回false
File file4=new File("D:\\apache-tomcat-9.0.0.M26\\bin\\content.txt");
System.out.println(file4.exists());//true 該絕對路徑有content.txt 返回true
java-IO學習