Java-通过BufferedWriter写文件

Java-通过BufferedWriter写文件

Tags: Java

摘要

Java-通过BufferedWriter写文件,BufferedWriter字符流类。

BufferedWriter字符流类

BufferedWriter是用来处理字符数据的类,可以直接把字符串或者字符数据写入文件中。

例子

package cn.javacore.io.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class _07_WriteToFileExample {
    public static void main(String[] args) {
        try {

            String content = "This is the content to write into file";
            File file = new File("/home/zhu/filename.txt");
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

代码地址

https://coding.net/u/javacore/p/java-io-study/git