Java 而循环仅打印一次,而不是整个循环
我有一个程序,在其中我使用while循环来打印控制台和html文件。然而,即使它打印到控制台,程序也只会将最后一行打印到html文件中。下面是我的循环代码,它应该打印从文本文件中提取的第9行到第16行的信息Java 而循环仅打印一次,而不是整个循环,java,printing,while-loop,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(Buffered
//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);
}
}
//读取txt文件
Path objPath=Path.get(“dirsize.txt”);
if(Files.exists(objPath))
{
File objFile=objPath.toFile();
try(BufferedReader in=新的BufferedReader(
新文件读取器(objFile)))
{
String line=in.readLine();
int lineNum=1;
//提取月份
String monthSubstring=line.substring(25,27);
month=Integer.parseInt(monthSubstring)-1;
//年终
字符串yearSubstring=line.substring(31,35);
year=(Integer.parseInt(yearSubstring));
//提取日期
字符串daySubstring=line.substring(28,30);
day=Integer.parseInt(daySubstring);
PrintWriter out=新的PrintWriter(新的BufferedWriter(新的FileWriter(“Output.html”));
while(行!=null)
{
line=in.readLine();
lineNum++;
如果(lineNum==3)
{
//抽出时间
字符串hourSubstring=line.substring(21,23);
hour=Integer.parseInt(hourSubstring);
//节录
String minuteSubstring=line.substring(24,26);
分钟=整数.parseInt(分钟子字符串);
//提取第二个
字符串secondSubstring=line.substring(27,29);
second=Integer.parseInt(secondSubstring);
}
如果(lineNum==5)
{
//拔出树枝
strBranch=line.substring(22,27);
}
如果(lineNum==6)
{
//解压计算机
strCPU=行子串(26,32);
}
如果(lineNum>=9&&lineNum,这是非常明显的-每次while循环迭代时都会创建文件(请参见下文):
您应该在进入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;
}
}
(…)
while(lineNum>=9&&lineNum这很明显-每次while循环迭代时都会创建一个文件(见下文):
您应该在进入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;
}
}
(…)
当(lineNum>=9&&lineNum时,您在输出文件中只看到一行,因为每次写入时都会覆盖该文件
如果必须在while(linenum>=9&&linenum内创建PrintWriter,则输出文件中只会看到一行,因为每次写入时都会覆盖该文件
如果必须在一段时间内创建PrintWriter(linenum>=9&&linenum可能我实现了这个错误,但它没有解决我的问题。我会将我的代码更新到我的原始帖子中,这样你就可以看到我是如何做到的。现在我在html文件中没有得到任何东西。它完全是空的。我会编辑我的代码,以显示我更改了什么部分,因为我可能做错了。我在第二}在循环结束后,但现在我的html文件是空的,没有从我的打印方法接收任何数据。当我没有break语句时,它打印出了第一行forever。也许我实现了这个错误,但它没有解决我的问题。我会将我的代码更新到我的原始帖子中,这样你就可以看到我是如何做到的了。现在我没有得到任何信息在html文件中,它完全是空的。我会编辑我的代码,以显示我更改了什么部分,因为我可能会出错。我将关闭操作移到第二个}在循环结束后,但现在我的html文件是空的,并且没有从打印方法接收任何数据。当我没有break语句时,它打印出了第一行forever。这确实解决了不是所有内容都写入文件的问题,而是造成了文件上的所有内容都在一起而不是新行的问题。我尝试了若要在out.println(此处为Printed stuff)的末尾使用“\n”来修复此问题,但随后我得到了一个错误,即不允许使用void类型。实际上,您不需要在BufferedWriter周围包装PrintWriter,BufferedWriter在您的情况下单独工作。只需调用BufferedWriter对象上的write()方法,然后调用newLine()方法。这应该可以解决问题。我将构造函数更改为BufferedWriter out=new BufferedWriter((new FileWriter(“Output.html”));但是现在程序找不到out.println的println方法BufferedWriter类没有println()方法。正如我在评论中提到的,您应该使用write()方法将内容写入文件,然后调用newLine()方法,以确保所有内容都没有写入同一行。如果我的原始响应解决了您的问题,请将其标记为正确答案。这确实解决了并非所有内容都写入文件的问题,但会造成文件上的所有内容都在一起而不是新行的问题。我已尝试修复这是在out.println(此处为Printed stuff)的末尾加上“\n”的结果,但我得到一个错误,即不允许使用void类型。实际上,您不需要在BufferedWriter周围包装一个PrintWriter,BufferedWriter单独在您的情况下就可以了。只需调用BufferedWriter对象上的write()方法,然后调用newLine()我将构造函数更改为BufferedWriter out=new BufferedWriter((new FileWriter(“Output.html”));但现在程序找不到out.println的println方法BufferedW没有println()方法