我試過這種方式,但它沒有找到文本文件。try { FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println(strLine); } // Close the input stream in.close();} catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage());}所有文件都在同一個包中。
1 回答

喵喵時光機
TA貢獻1846條經驗 獲得超7個贊
您的文本文件位于“src”中的“trainer”包中,因此當您請求它時,您必須使用"src/trainer/textfile.txt"
. 前面/
表示應用程序的根,例如,如果您不導出到可運行的 jar,則它是可選的。
FileInputStream fstream = new FileInputStream("src/trainer/textfile.txt");
添加回答
舉報
0/150
提交
取消