我有一个程序,我在其中使用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”放在我的打印方法的末尾时,将其格式化为行?
我不确定这是否代码太多或不足以解决问题,如果我应该添加任何东西到我的文章或拿出任何东西只是让我知道。
也许我实现了这个错误,但它并没有解决我的问题我更新我的代码到我原来的帖子,所以你可以看到我是怎么做到的 – 2013-04-21 00:01:52
嗯现在我没有得到任何东西在HTML文件它完全是空编辑我的代码,以显示我改变了什么,因为我可以做到这一点错了 – 2013-04-21 00:12:02
我把关闭操作后第二个}后循环结束,但现在我的HTML文件是空的,并没有收到我的打印方法的任何数据 – 2013-04-21 00:18:48