我想寫一個處理讀取文件的類。爲了逐字閱讀文件,我使用了我在互聯網上找到的以下代碼。 Netbeans似乎不同意,並表示它無法在while循環內找到symbole br。BufferReader裏面的try子句:找不到符號
public class Reader {
public String file2string() {
String line;
try (InputStream fis = new FileInputStream("smth")) {
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
{
while ((line = br.readLine()) != null) {
String[] words = line.split(" ");
// Now you have a String array containing each word in the current line
}
}
return line;
}
}
請縮進代碼,如果正常,你有求於人,試圖讀取它。 – khelwood
你的'while循環'超出'try-catch',所以變量'br'超出了範圍 – SomeJavaGuy