1. 程式人生 > >讀取專案中放置的檔案

讀取專案中放置的檔案

1. 獲取檔案3.txt:

A、相對路徑:從src開始寫起

//裡面路徑寫成:3.txt報錯:java.io.FileNotFoundException: 3.txt (系統找不到指定的檔案。)
FileInputStream in = new FileInputStream( "src\\3.txt"); 

B、絕對路徑:

String path = new File("").getCanonicalPath()+ "\\src\\";
System.out.println("path: " + path);//path: E:\programfiles\idea\testboot\src\
FileReader reader = new FileReader(path + "3.txt");

2. 獲取檔案1.txt

InputStream in3 = new FileInputStream("1.txt");