首页 > 编程语言 > 详细

java读取字符串,生成txt文件

时间:2019-06-27 14:34:14      阅读:152      评论:0      收藏:0      [点我收藏+]
/**
 * 读取字符串,生成txt 文件 已解决未设置编码时,在项目中直接打开文件,中文乱码问题
 * WriteText.writeToText(musicInfo,fileName)直接调用
 * 
 * @author ziggo
 *
 */
public class WriteText {
    public static void writeToText(String musicInfo, String fileName) throws IOException {
        // 生成的文件路径
        String path = "G:\\data\\" + fileName + ".txt";
        File file = new File(path);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        // write 解决中文乱码问题
        // FileWriter fw = new FileWriter(file, true);
        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(musicInfo);
        bw.flush();
        bw.close();
        fw.close();

    }

当调用方法执行时,会生成自定义文件名的txt文件,使用场景较少,效果如下

技术分享图片

 

java读取字符串,生成txt文件

原文:https://www.cnblogs.com/by-xu/p/11096680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!