PrintWriter只打印出文本文件的第一行。

huangapple go评论74阅读模式
英文:

PrintWriter is only printing out the first line of the text file

问题

// 创建名为htmltext的File类型
File htmltext = new File(filepath);
Scanner file = new Scanner(htmltext);

PrintWriter out = new PrintWriter("updated.txt");

while (file.hasNext()) {
    String s = file.nextLine();
    out.println(s);
}
out.close();

运行代码后,out.println(s) 只打印了文本文件的第一行。我查阅了如何将字符串打印到文本文件中,发现应该使用 PrintWriter。我期望程序基本上会使用 PrintWriter 将文本文档的内容“重新打印”到 "updated.txt" 文件中。
然而,似乎它只是将文本文件的第一行“重新打印”到 "updated.txt" 中。

我认为 while 循环有问题,因此我尝试使用 System.out.println(s) 定期将其打印到控制台,但那样却正常工作。

我对 while 循环的理解是,在文件有标记的情况下,它将进行迭代,s 将存储一行,并且(应该)打印该字符串 s。

我错过了什么?我是否误解了 while 循环的工作原理?或者是如何使用 nextLine()?又或者是 PrintWriter 的工作原理。(可能是以上所有原因...)

我希望能得到反馈!


<details>
<summary>英文:</summary>

I want the program to save each line of the text file i have into String s and print String s with a PrintWriter into another text file. 

```//creates creates type File with name htmltext
		File htmltext = new File(filepath);
		Scanner file = new Scanner(htmltext);
		
		PrintWriter out = new PrintWriter(&quot;updated.txt&quot;);
				
		while (file.hasNext()) {
			String s = file.nextLine();	
			out.println(s);
			out.close();

I've run the code and out.println(s), only printed out the first time of the text file.
I looked up how to print string into a text file and I found that I should use PrintWriter.
I was expecting the program to basically "re-print" the content of the text document into the "updated.txt" file using PrintWriter.
However, it only seems to be "re-printing" out the first line of the text file into "updated.txt".

I thought something was wrong with my while loop so I tried printing it out regularly into the console using System.out.println(s), but that worked fine.

What I understand of the while-loop is that while the file has tokens, it will iterate and s will store one line and (it should) print said string s.

What am I missing? Am I misunderstanding how the while-loop works? or how nextLine() works? or how PrintWriter works.(probably all of the above...)

I would love feedbacks!

答案1

得分: 0

你正在告诉它在写入该行后立即关闭。

你想要将 out.close 移动到 while 循环的外部,你也应该可能需要刷新流。

你的代码,用英语表达,基本上是这样的:

从标准输入打开一个扫描器
为文件打开一个打印写入器

当扫描器有新行时
    将新行写入打印写入器
    **关闭打印写入器**

打印写入器关闭后,就无法再写入新数据,因为它已经关闭。

英文:

You're telling it to close as soon as it writes the line.

You want to move out.close to outside of your while loop, you should probably flush the stream as well.

Your code, in english, basically says:

open a scanner from stdin
open a printwriter for a file

while the scanner has a new line
    write the new line to the printwriter
    **close the printwriter**

After the printwriter is closed, you can't write new data, because it's closed.

答案2

得分: 0

不要在循环内关闭PrintWriter。在while循环之后关闭。

英文:

Don't close the PrintWriter inside the loop. Do that after the while loop.

答案3

得分: 0

不要在完成读取整个内容之前关闭write对象

File htmltext = new File(filepath);
Scanner file = new Scanner(htmltext);
PrintWriter out = new PrintWriter("updated.txt");
while (file.hasNext()) 
{
    String s = file.nextLine(); 
    out.println(s);
}

**out.close();**
英文:

Don't close the write object before it is finished reading the whole content

File htmltext = new File(filepath);
Scanner file = new Scanner(htmltext);
PrintWriter out = new PrintWriter(&quot;updated.txt&quot;);
while (file.hasNext()) 
{
String s = file.nextLine(); 
out.println(s);
}

**out.close();**

huangapple
  • 本文由 发表于 2020年9月27日 06:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定