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

/**
 * 讀取字符串,生成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文件,使用場景較少,效果以下編碼

相關文章
相關標籤/搜索