while循环只打印一次而不是整个循环

时间:2013-04-20 23:48:33

标签: java printing while-loop

我有一个程序,我在其中使用while循环将两者打印到控制台和html文件。但是,即使它打印到控制台,程序也只会打印最后一行到html文件。这是我的循环的代码,它应该打印从第9到第16行的文本文件中提取的信息。

  //read the txt file
Path objPath = Paths.get("dirsize.txt");  
if (Files.exists(objPath))
{

    File objFile = objPath.toFile();
  try(BufferedReader in = new BufferedReader(
                          new FileReader(objFile)))
   {
    String line = in.readLine();
    int lineNum = 1;
    //extract the month
    String monthSubstring = line.substring(25,27);
    month = Integer.parseInt(monthSubstring) -1 ;
    //extact the year
    String yearSubstring = line.substring(31,35);
    year = (Integer.parseInt(yearSubstring));
    //extract the date
    String daySubstring = line.substring(28,30);
    day = Integer.parseInt(daySubstring);
     PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter("Output.html")));
    while(line != null)
    {
      line = in.readLine();
      lineNum ++;
      if (lineNum == 3)
       {
           //extract the hour
            String hourSubstring = line.substring(21,23);
             hour = Integer.parseInt(hourSubstring); 
            //extract the minute
            String minuteSubstring = line.substring(24,26);
             minute = Integer.parseInt(minuteSubstring);
            //extract the second
            String secondSubstring = line.substring(27,29);
            second= Integer.parseInt(secondSubstring);
           }
      if(lineNum ==5)
       {
            //Extract the branch
            strBranch = line.substring(22, 27);        
       }        
      if (lineNum == 6)
      {
             //Extract the computer
             strCPU = line.substring(26,32);
      }   
     if (lineNum >= 9 && lineNum <= 16)
      {
          //call the MyDirectory methods to get the files name,size and number of files
          MyDirectory directory = new MyDirectory(line);
          fileName = directory.getName();
          fileSize = directory.getsize();
          fileNum = directory.getNum();
          //create the dteDate calender
          GregorianCalendar dteDate = new GregorianCalendar(year, month, day, hour, minute, second);
          //fromat the date
          SimpleDateFormat strDate = new SimpleDateFormat("MMM dd, yyyy h:m");
        //Write the data to the file
         out.println((strBranch + "; " +
                  strCPU + "; "  + 
                  strDate.format(dteDate.getTime())) + "; " +
                  fileName + "; " +
                  fileSize + "; " + 
                  fileNum);
         //create the necessary output for the console
          System.out.println((strBranch + "; " +
                  strCPU + "; "  + 
                  strDate.format(dteDate.getTime())) + "; " +
                  fileName + "; " +
                  fileSize + "; " + 
                  fileNum);
        }    
       out.flush();
    }

   }   

  //catch any IOExceptions and print out e
 catch (IOException e)
  {
    System.out.println(e);  
  }

}
编辑:通过在答案中发布的建议我现在有输出的所有内容,但我很难格式化html文件的所有行没有间距我尝试在输出结束时使用“\ n”但是然后我的代码没有编译,因为它说不允许null有人知道如何将它格式化成行,当它不会让我在我的print方法的末尾加上“\ n”时?

我不确定这是代码还是不足以解决问题,如果我应该在帖子中添加任何内容或取消任何内容,请告诉我。

2 个答案:

答案 0 :(得分:3)

这很明显 - 每次while循环迭代时都会创建文件(见下文):

             PrintWriter out = new PrintWriter (
                           new BufferedWriter(
                           new FileWriter("Output.html")));
        //Write the data to the file
         out.println((strBranch + "; " +
                  strCPU + "; "  + 
                  strDate.format(dteDate.getTime())) + "; " +
                  fileName + "; " +
                  fileSize + "; " + 
                  fileNum);
        //flush the data and close the output stream
         out.close();

您应该在输入while循环之前创建文件。

所以你的代码看起来应该是这样的:

     PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter("Output.html")));
     while(line != null)
    {
        //perform your file operations here
    }
    out.close();

----编辑----

您已更新了代码,但仍然不行。您在第二个循环中关闭文件:

        (...)
 while (lineNum >= 9 && lineNum <= 16)
  {
      //call the MyDirectory methods to get the files name,size and number of files
      MyDirectory directory = new MyDirectory(line);
      fileName = directory.getName();
      fileSize = directory.getsize();
      fileNum = directory.getNum();
      //create the dteDate calender
      GregorianCalendar dteDate = new GregorianCalendar(year, month, day, hour, minute, second);
      //fromat the date
      SimpleDateFormat strDate = new SimpleDateFormat("MMM dd, yyyy h:m");
    //Write the data to the file
     out.println((strBranch + "; " +
              strCPU + "; "  + 
              strDate.format(dteDate.getTime())) + "; " +
              fileName + "; " +
              fileSize + "; " + 
              fileNum);
    //flush the data and close the output stream
     //################# YOU CLOSE YOUR FILE HERE:
     out.close();
     //create the necessary output for the console
      System.out.println((strBranch + "; " +
              strCPU + "; "  + 
              strDate.format(dteDate.getTime())) + "; " +
              fileName + "; " +
              fileSize + "; " + 
              fileNum);
     //move on in the loop so it's not infinte
      break;
   }    
}

当下一次循环迭代时,你仍然会尝试写入你的文件(然后再次关闭它,即使它在之前的迭代中被关闭了)。

答案 1 :(得分:0)

您只在输出文件中看到一行,因为每次写入文件时都会覆盖该文件。

如果您必须在while(linenum&gt; = 9&amp;&amp; linenum&lt; = 16)循环内创建PrintWriter,请以追加模式打开文件:

新FileWriter(“Output.html”,true);

将解决您的问题。