我正在读取整个文件,如果它包含特定字符串,我想使用该行。我无法使用该字符串,因为它在 while 循环外打印 null
,尽管我已经在循环外对其进行了初始化。
FileInputStream wf = new FileInputStream(pr.getSplitDir() + listfiles[i]);
BufferedReader wbf = new BufferedReader(new InputStreamReader(wf));
String wfl = "";
while ((wfl = wbf.readLine()) != null) {
if (wfl.contains("A/C NO:")){
// System.out.println(wfl); // Here it is Printing the correct line
}
}
System.out.println(wfl); // Here it is printing null
请帮忙。
最佳答案
在下面试试这个,你必须使用另一个 String 或 StringBuilder 来获得最终输出
FileInputStream wf = new FileInputStream(pr.getSplitDir() + listfiles[i]);
BufferedReader wbf = new BufferedReader(new InputStreamReader(wf));
String wfl = "";
StringBuilder sb = new StringBuilder();
while ((wfl = wbf.readLine()) != null) {
if(wfl.contains("A/C NO:")){
//System.out.println(wfl);//Here it is Printing the correct line
sb.append(wfl);
}
}
System.out.println(sb.toString());//Here it is printing null
关于java - 在 While 循环外访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19653120/