![](/img/trans.png)
[英]Reading numbers from a .txt/input file and printing a triangle in Java Eclipse
[英]Reading a .txt file in java in eclipse
我正在嘗試從計算機中讀取文件,但顯示為java.io.FileNotFoundException(系統找不到指定的文件)
System.out.println("READING FILE");
File file = new File("Testing.txt"); //Reading file from E
FileInputStream in = null;
BufferedInputStream buff = null;
DataInputStream data= null;
String Line=""; // declare a string
try
{
in = new FileInputStream(file); // pick up the file
buff = new BufferedInputStream(in);
data = new DataInputStream(buff);
while (data.available() != 0)
{ // Read the file line by line till it reaches the end of file
Line=Line+data; // concatenate line into string
System.out.println(data.readLine()); // print line by line
}
} catch (IOException e)
{
e.printStackTrace();
}
File file = new File("E:\\Testing.txt");
如果您嘗試訪問E,則這是實現此目的的方法之一
如果您使用Windows,請嘗試:
File file = new File("e:/Testing.txt");
(這意味着:使用斜杠/代替反斜杠\\)
您需要:
Testing.txt的路徑在哪里?
File file = new File("Testing.txt"); //Reading file from E
在這里,您正在從src路徑中讀取java,如果文件位於src中,則應修改路徑
File file = new File("src/Testing.txt"); //Reading file from E
另一個解決方案是輸入文件的完整路徑
指定文件的完整路徑即可。
給出完整路徑而不是相對路徑
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.