【问题标题】:Reading from the text file using Buffered Reader使用缓冲阅读器从文本文件中读取
【发布时间】:2016-11-02 14:24:49
【问题描述】:

我正在尝试从文本文件中读取, 我正在使用 try 和资源块从平面文件中读取:

ABC 24 32
1234567
2345678
3456789
CDE 25 35
4567890
5678901

我想要这样的输出:

ABC, 1234567
ABC, 2345678
ABC, 3456789
CDE, 4567890
CDE, 5678901

我尝试使用mark()reset() 进行研究。 任何建议都会有很大帮助

public static void advParse(String fileName){

        File file = new File("test1.txt");
        ArrayList<Head> abc = new ArrayList<Head>();

        try( BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
            Head head = new Head();
            String line ;

            while(((line = bufferedReader.readLine()) != null)){

                while(line.length()==7 ){

                 System.out.println(line);




        } 
            }
        }catch (FileNotFoundException e) {
            System.out.println("File not found for  "+file.toString());
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Unable to the read the file "+file.toString());
            e.printStackTrace();
        };


}

【问题讨论】:

  • 您“尝试过研究”,但您是否尝试过编写任何实际代码?我们必须相信您为此付出的努力,为什么不向我们展示您所做的工作,以便我们可以看到您为此付出的努力。
  • 这里有一个建议:发布您已经尝试过的有问题的东西,告诉我们您遇到了什么问题,我们可以为您提供帮助
  • 究竟是什么不起作用?
  • 您需要使用一个变量来记住最近阅读的字母行。测试一行是否由字母字符组成最好使用regular expression
  • 你为什么没有在你的调试器中逐步完成这个?

标签: java bufferedreader flat


【解决方案1】:

你在文件1234567的第二行,长度为7,你有这个内部循环:

while(line.length()==7 ){
  System.out.println(line);
}

所以我会假设它会永远迭代。 也许用if 子句改变它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2013-03-29
    • 2015-08-30
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多