简体   繁体   English

从StringWriter写入txt文件

[英]Writing to txt file from StringWriter

I have a StringWriter variable, sw , which is populated by a FreeMarker template. 我有一个StringWriter变量sw ,由FreeMarker模板填充。 Once I have populated the sw , how can I print it to a text file? 填充sw ,如何将其打印到文本文件?

I have a for loop as follows: 我有一个for循环,如下所示:

for(2 times)
{
    template.process(data, sw);
    out.println(sw.toString());
}

Right now, I am just outputting to the screen only. 现在,我只输出到屏幕上。 How do I do this for a file? 我该如何处理文件? I imagine that with each loop, my sw will get changed, but I want the data from each loop appended together in the file. 我想象每个循环都会更改sw ,但是我希望每个循环的数据都附加到文件中。

Edit: I tried the code below. 编辑:我尝试下面的代码。 When it runs, it does show that the file.txt has been changed, but when it reloads, the file still has nothing in it. 当它运行时,它确实表明file.txt已被更改,但是当它重新加载时,该文件仍然没有任何内容。

sw.append("CheckText");
PrintWriter out = new PrintWriter("file.txt");
out.println(sw.toString());

How about 怎么样

FileWriter fw = new FileWriter("file.txt");
StringWriter sw = new StringWriter();
sw.write("some content...");
fw.write(sw.toString());
fw.close();

and also you could consider using an output stream which you can directly pass to template.process(data, os); 并且您还可以考虑使用可直接传递给template.process(data, os);的输出流template.process(data, os); instead of first writing to a StringWriter then to a file. 而不是先写入StringWriter,然后写入文件。

Look at the API-doc for the template.process(...) to find out if such a facility is available. 查看template.process(...)的API文档以了解是否有这样的工具。

Reply 2 回复2

template.process(Object, Writer) can also take a FileWriter object, witch is a subclass of Writer, as parameter, so you probably can do something like that: template.process(Object, Writer)也可以使用FileWriter对象,witch是Writer的子类,作为参数,因此您可以执行以下操作:

FileWriter fw = new FileWriter("file.txt");    
for(2 times)
{
    template.process(data, fw);
}
fw.close();

You can use many different streams to write to file. 您可以使用许多不同的流来写入文件。

I personally like to work with PrintWriter here You can flag to append in the FileWriter (the true in the following example): 我个人喜欢在这里使用PrintWriter 您可以标记为在FileWriter中追加(在以下示例中为true):

try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
    out.println(sw.toString());
    out.close();
} catch (IOException e) {
    // Do something
}

Why not use a FileWriter ? 为什么不使用FileWriter

Open it before you loop and generate your required output. 在循环之前将其打开并生成所需的输出。 As you write to the FileWriter it'll append to the buffer and write out your accumulated output upon a close() 当您写入FileWriter时,它将添加到缓冲区中,并在close()写出累积的输出

Note that you can open a FileWriter in overwrite or append mode, so you can append to existing files. 请注意,您可以以覆盖追加模式打开FileWriter,因此可以追加到现有文件。

Here's a simple tutorial . 这是一个简单的教程

如果您不介意使用Apache commons IO:

FileUtils.write(new File("file.txt"), sw.toString(), /*append:*/ true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM