java-IO學習

來源:互聯網
上載者:User

標籤: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學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.