Writer class append 方法,第一次追加到新行,然后追加到上一句的下一个。为什么?
Writer class append method, appends in the new line the first time, but then appends next to the previous sentence. Why?
我有一个名为 my-training.train
的文件。 (.train 是 open-nlp
中训练文件的扩展名)。该文件已经填充了一些数据:
Refund What is the refund status for my order #342? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Is there any new offer on buying worth 5000?
Refund What is the refund status for my order #343? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
我想以编程方式在紧接着的下一行中附加一个句子。这是我写的代码:
Writer output;
output = new BufferedWriter(new FileWriter("my-training.train",true));
output.append("NewOffers Please inform me of the new offers");
output.close();
句子按预期插入。显示训练文件的最后三行:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
但是当我再次 运行 代码时,该句子被插入到前一个句子的下一个而不是新行。 (最后三行):
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offersNewOffers Please inform me of the new offers
在上面的代码中,我将行 output.append("NewOffers Please inform me of the new offers");
替换为 output.append("\nNewOffers Please inform me of the new offers");
,但是使用它会在第一次追加时产生问题。
获取初始文件my-training.train
,最后三行变成这样:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
插入空行。这使得训练文件无效。不能有空行,每句都要换行。
为什么第一行和其他行之间存在这种歧视行为。?我做错了什么?
第二行 as 在末尾插入一个新行。 close()
通过在缓冲区完全写入之前关闭来禁止将新行放置在 "Please Inform me of new offers" 的末尾。
解决此问题的最佳方法是在 append()
和 close()
之间插入一个 flush()
。这样,新行会在文件关闭前写入文件。
我不是 100% 熟悉 API。我不确定 Writer
是否有 flush 方法,因此您可能必须在调用 flush.[=19= 之前将输出类型更改为 BufferedWriter
或将输出转换回 BufferedWriter
]
如果 flush 没有改变任何东西,那么 append()
不会向文件发送新行,因此将文本更改为 "Please Inform me of new offers\n"
即可。
我有一个名为 my-training.train
的文件。 (.train 是 open-nlp
中训练文件的扩展名)。该文件已经填充了一些数据:
Refund What is the refund status for my order #342? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Is there any new offer on buying worth 5000?
Refund What is the refund status for my order #343? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
我想以编程方式在紧接着的下一行中附加一个句子。这是我写的代码:
Writer output;
output = new BufferedWriter(new FileWriter("my-training.train",true));
output.append("NewOffers Please inform me of the new offers");
output.close();
句子按预期插入。显示训练文件的最后三行:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
但是当我再次 运行 代码时,该句子被插入到前一个句子的下一个而不是新行。 (最后三行):
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offersNewOffers Please inform me of the new offers
在上面的代码中,我将行 output.append("NewOffers Please inform me of the new offers");
替换为 output.append("\nNewOffers Please inform me of the new offers");
,但是使用它会在第一次追加时产生问题。
获取初始文件my-training.train
,最后三行变成这样:
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?
NewOffers Please inform me of the new offers
插入空行。这使得训练文件无效。不能有空行,每句都要换行。
为什么第一行和其他行之间存在这种歧视行为。?我做错了什么?
第二行 as 在末尾插入一个新行。 close()
通过在缓冲区完全写入之前关闭来禁止将新行放置在 "Please Inform me of new offers" 的末尾。
解决此问题的最佳方法是在 append()
和 close()
之间插入一个 flush()
。这样,新行会在文件关闭前写入文件。
我不是 100% 熟悉 API。我不确定 Writer
是否有 flush 方法,因此您可能必须在调用 flush.[=19= 之前将输出类型更改为 BufferedWriter
或将输出转换回 BufferedWriter
]
如果 flush 没有改变任何东西,那么 append()
不会向文件发送新行,因此将文本更改为 "Please Inform me of new offers\n"
即可。