我有一個程序,我在其中使用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