[英]How to give path to txt to read from in java?
我正在使用BuffererReader閱讀txt,我想知道如何在BufferedReader的參數中提供指向我的txt的路徑。
BufferedReader input = new Bufferedreader(
new FileReader(D:VI.félév\Prog gyak\Project\proba.txt));
這似乎不起作用。
您的問題是:
new Filereader( .. )
應該
new FileReader( .. )
路徑必須類似於:
new FileReader("D:\\directory\\proba.txt");
您需要一個附加的\\
作為轉義字符
如果發生問題,請try..catch
使用try..catch
完整示例。
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("D:\\directory\\proba.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.