如何将String写入文本文件? [英] How do you write a String to a text file?

查看:154
本文介绍了如何将String写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个String,用于存储几个文件的处理结果。如何将该String写入项目中的.txt文件?我有另一个String变量,它是.txt文件的所需名称。

I have a String that is storing the processed results for a few files. How do I write that String to a .txt file in my project? I have another String variable which is the desired name of the .txt file.

推荐答案

试试这个:

//Put this at the top of the file:
import java.io.*;
import java.util.*;

BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));

//Add this to write a string to a file
//
try {

    out.write("aString\nthis is a\nttest");  //Replace with the string 
                                             //you are trying to write
}
catch (IOException e)
{
    System.out.println("Exception ");

}
finally
{
    out.close();
}

这篇关于如何将String写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆