You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用BufferedReader从文本文件提取指定行并赋值给变量?

问题分析

你的问题很清晰——现在的代码在每读取一行之后就立刻打印三个变量,所以每处理一行就输出一次当前的变量状态,自然会出现重复输出的情况。比如读第一行时只有description有值,另外两个是null;读第二行时image也有值了,以此类推,所以会看到三次递进式的输出。

解决方案

核心思路就是:先遍历完所有行,把所有符合条件的内容都赋值给对应变量,之后再一次性打印最终结果。

修改后的基础版本代码

package inputOutput; 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner; 

public class Test_leser { 
    public static void main(String[] args) throws IOException { 
        // 询问要打开的文件名
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Filename ");
        BufferedReader bReader = null; 

        // 打开指定文件
        try { 
            bReader = new BufferedReader( 
                new FileReader("C:\\Users\\valat\\OneDrive\\Desktop\\image-data\\" + scan.next())
            ); 
        } catch (FileNotFoundException fnfex) { 
            System.out.println(fnfex.getMessage()); 
            System.exit(0); 
        } 

        String line; 
        String resolution = null; 
        String image = null; 
        String description = null; 

        // 遍历文件行,赋值对应变量
        while ((line = bReader.readLine()) != null) { 
            if (line.startsWith("description:")) { 
                description = line; 
            } else if (line.startsWith("image-file")) { 
                image = line; 
            } else if (line.startsWith("resolution:")) { 
                resolution = line; 
            } else { 
                System.out.println("something is REALLY wrong"); 
            }
            // 移除这里的打印语句!原来的重复输出就是因为它在循环里
        }

        // 循环结束后,统一打印所有变量的最终值
        System.out.println(description);
        System.out.println(image);
        System.out.println(resolution);
        
        // 手动关闭资源,避免内存泄漏
        if (bReader != null) {
            bReader.close();
        }
        scan.close();
    } 
}

更简洁安全的优化版本(推荐)

可以使用Java的try-with-resources语法,它会自动关闭实现了AutoCloseable接口的资源(比如BufferedReader和Scanner),不用手动写close代码,更安全也更简洁:

package inputOutput; 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner; 

public class Test_leser { 
    public static void main(String[] args) throws IOException { 
        // try-with-resources自动关闭Scanner
        try (Scanner scan = new Scanner(System.in)) {
            System.out.println("Enter Filename ");
            
            // try-with-resources自动关闭BufferedReader
            try (BufferedReader bReader = new BufferedReader( 
                    new FileReader("C:\\Users\\valat\\OneDrive\\Desktop\\image-data\\" + scan.next())
                )) {

                String line; 
                String resolution = null; 
                String image = null; 
                String description = null; 

                while ((line = bReader.readLine()) != null) { 
                    if (line.startsWith("description:")) { 
                        description = line; 
                    } else if (line.startsWith("image-file")) { 
                        image = line; 
                    } else if (line.startsWith("resolution:")) { 
                        resolution = line; 
                    } else { 
                        System.out.println("something is REALLY wrong"); 
                    }
                }

                // 统一打印最终结果
                System.out.println(description);
                System.out.println(image);
                System.out.println(resolution);

            } catch (FileNotFoundException fnfex) { 
                System.out.println(fnfex.getMessage()); 
                System.exit(0); 
            }
        }
    } 
}

修改后运行代码,就能得到你预期的输出:

description: Blutausstrich (Mensch)
image-file: image01.jpg
resolution: 0.002 mm

内容的提问来源于stack exchange,提问作者Nilfgaardian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:28:29