2013-04-20 168 views
0

我有一个程序,我在其中使用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不允许有任何人知道如何当它不会让我把“\ n”放在我的打印方法的末尾时,将其格式化为行?

我不确定这是否代码太多或不足以解决问题,如果我应该添加任何东西到我的文章或拿出任何东西只是让我知道。

回答

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(); 

---- ----编辑

你更新了你的代码,但它仍然不能确定。你关闭了文件第二循环内:

当下次这个循环迭代你还是试着写信给你的文件(然后再次关闭它,即使它是在先前的迭代关闭)。

+0

也许我实现了这个错误,但它并没有解决我的问题我更新我的代码到我原来的帖子,所以你可以看到我是怎么做到的 – 2013-04-21 00:01:52

+0

嗯现在我没有得到任何东西在HTML文件它完全是空编辑我的代码,以显示我改变了什么,因为我可以做到这一点错了 – 2013-04-21 00:12:02

+0

我把关闭操作后第二个}后循环结束,但现在我的HTML文件是空的,并没有收到我的打印方法的任何数据 – 2013-04-21 00:18:48

0

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

新的FileWriter( “Output.html”,真正:

如果你需要创建而内的PrintWriter(LINENUM> = 9 & & LINENUM < = 16)循环,在追加模式打开文件);

将解决您的问题。

+0

这确实解决了不是所有的问题写入文件但创建文件上的所有内容都是在一起而不是新行的问题。我试图用out.println(打印的东西在这里)的结尾处的“\ n”来解决这个问题,但是我得到了一个错误,那就是void类型是不允许的 – 2013-04-21 00:07:16

+0

实际上,你不需要在BufferedWriter中包装PrintWriter, BufferedWriter单独适用于你的情况。只需调用BufferedWriter对象上的write()方法,然后调用newLine()方法即可。这应该解决它。 – Suchet 2013-04-21 00:15:51

+0

我改变了我的构造函数为BufferedWriter out = new BufferedWriter((new FileWriter(“Output.html”)));但现在程序无法找到out的println方法。println – 2013-04-21 00:25:12