我正在使用nio2在我的桌面上使用eclipse讀取外部文件。我得到以下代碼的例外。訪問外部文件
「java.nio.file.NoSuchFileException
:C:\Users\User\Desktop\JEE\FirstFolder\first.txt
」
請告知如何解決呢?也嘗試使用命令提示符。獲得相同的例外。
public class ReadingExternalFile {
public static void main(String[] args) {
Path p1= Paths.get("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt");
System.out.println(p1.toString());
System.out.println(p1.getRoot());
try(InputStream in = Files.newInputStream(p1);
BufferedReader reader = new BufferedReader(new InputStreamReader(in)))
{
System.out.println("Inside try");
String line=null;
while((line=reader.readLine())!=null){
if (!line.equals("")) {
System.out.println(line);
}
//System.out.println(line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
你好謝謝你的info.I我知道這樣,我努力學習NIO2並得到了錯誤,同時使用「路徑」 class.Can你建議如何在這種情況下使用nio2來讀取文件? – Yathish
如果您可以添加控制檯輸出,包括問題底部的錯誤,並在您的問題中明確表示要使用nio2軟件包,那將會很不錯。 – James