java - 在 While 循环外访问值

标签 java string file

我正在读取整个文件,如果它包含特定字符串,我想使用该行。我无法使用该字符串,因为它在 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/

相关文章:

java - Java中用字符增加数字

java - 页面包含带有 LayoutTransition (或 animateLayoutChanges ="true")的 ViewGroup,这会干扰滚动动画

java - 如何在不使用 String 的情况下创建 JSON 对象?

c - 如何从文件名的字符串数组中读取文件?

java - 从另一个类设置 JTable 列宽度

java - Android 进度条大小调整

c - 如何将匿名字符串文字传递给函数?

swift - String(validatingUTF8 :) and String(utf8String:)? 之间有区别吗

C#,识别目录数组中重复文件的最快(最好?)方法

c - C 中特定格式的输出文件