1

我正在尝试将文件打印到文本文件。虽然我已经设法让它工作,但它只打印出控制台上打印的最后一行。例如,我的控制台大约有 8000 行,但它只打印出文本文件中的最后一行,我想将所有行打印到文本文件中。这是代码:

       try {
        BufferedReader reader = new BufferedReader(new FileReader("JDT.txt"));
        Writer output = null;
        File file = new File("output.txt");
        output = new BufferedWriter(new FileWriter(file));


        String line = reader.readLine();
        int count=0;
        while(line !=null)
        {
            for(int i = 0 ; i<faults.length;i++){
                if(line.contains(faults[i]))
                    System.out.println(line);
                count++; 
                output.write(line +"Total: "+count);
                //System.out.println("File Written");
        }
            line=reader.readLine();
            }                  
        System.out.println("Printed Lines =" +count); 
                   output.close();     
        }
    catch (Exception ex) {
        System.out.println(ex.getMessage());
        }       
}
    }

提前谢谢你。

4

3 回答 3

2

您在每个循环中重新创建和覆盖您的文件。创建你的文件和你BufferedWriter的循环之前,并在你的循环完成后关闭它。

于 2013-02-21T19:37:12.580 回答
1

您应该将这些行放在while循环开始之前。

Writer output = null;
File file = new File("output.txt");
output = new BufferedWriter(new FileWriter(file));

并关闭Writerwhile循环退出后。

output.close();

该语句output.write(line +"Total: "+count);还试图将每一行写在另一行的末尾,从而产生一大行输出。将其替换为:

output.write(line + "Total: " + count + "\n");

这应该导致每一行都打印在新行上。

不确定它是否是预期的,但在您的代码中可能会根据变量output.write的长度在同一行执行多次。faults这将导致同一行被多次打印。

如果 的长度faultsnwrite被调用n次数为 相同的line

我认为你需要这个:

while (line != null) {
    for (int i = 0; i < faults.length; i++) {
        if (line.contains(faults[i])) {
            count++;
            output.write(line + "Total: " + count + "\n");
            System.out.println(line);
            break;
        }
    }
    // System.out.println("File Written");
    line = reader.readLine();
}
于 2013-02-21T19:41:07.407 回答
1

我想我会把它放在这里以供参考。这只是将行从输入文件复制到输出文件的一般方法。

final class Sample
{
   private static final String inputFile = "input_file.txt";
   private static final String outputFile = "output_file.txt";

   public void start()
   {

      BufferedReader bufferedReader;
      BufferedWriter bufferedWriter;
      try
      {
         bufferedReader = new BufferedReader(new FileReader(inputFile));
         bufferedWriter = new BufferedWriter(new FileWriter(outputFile));
         copyContents(bufferedReader, bufferedWriter);
         bufferedReader.close();
         bufferedWriter.close();
      }
      catch (final IOException e)
      {
         e.printStackTrace();
      }
   }

   private void copyContents(final BufferedReader bufferedReader, final BufferedWriter bufferedWriter)
   {
      try
      {
         String line = bufferedReader.readLine();
         while (null != line)
         {
            bufferedWriter.write(line + '\n');
            line = bufferedReader.readLine();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}
于 2013-02-21T20:00:41.687 回答