> 文章列表 > 关于java.io的学习记录(写入文本)

关于java.io的学习记录(写入文本)

关于java.io的学习记录(写入文本)

可以通过字节流(FileOutputStream)、字符流(OutputStreamWriter)、字符缓冲流(BufferedWriter)读取文本中的数据。

FileOutputStream写入文本

    public void write(){String path = "writeTest.txt";//File file = new File(path);FileOutputStream output = null;try {// 设置为true的时候,表示在文末追加output = new FileOutputStream(path, true);output.write("FileOutputStream write something\\r\\n".getBytes());output.write("FileOutputStream 写中文\\r\\n".getBytes());output.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(output != null){try {output.close();} catch (IOException e) {e.printStackTrace();}}}}

运行说明:

1)new FileOutputStream(path, true);设置为true的时候,表示在文末追加

2)换行一定要按顺序写\\r\\n,少一个或者顺序不对,查看文本的时候都不能正确显示换行

3)如果txt文件一开始没有内容,运行程序写入utf-8编码格式的数据,打开文本可以看到写入的中文正常显示,并且文本的编码变更为utf-8
4)如果一开始文本里面有内容,并且文本的编码格式为GBK,运行程序写入utf-8编码格式的数据,打开文本可以看到写入的中文显示乱码

5)没有append方法

OutputStreamWriter写入文本

    public void write2(){String path = "writeTest.txt";//File file = new File(path);FileOutputStream output = null;try {// 设置为true的时候,表示在文末追加output = new FileOutputStream(path, true);OutputStreamWriter writer = new OutputStreamWriter(output, "utf-8");writer.append("OutputStreamWriter append something\\r\\n").append("OutputStreamWriter 写中文\\r\\n").append("OutputStreamWriter append something\\r\\n");writer.write("OutputStreamWriter write something\\r\\n");writer.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(output != null){try {output.close();} catch (IOException e) {e.printStackTrace();}}}}public void write4(){String path = "G:\\\\学习类\\\\thread\\\\doc\\\\writeTest.txt";FileWriter writer;try {writer = new FileWriter(path, true);writer.append("FileWriter append something\\r\\n").append("FileWriter 写中文\\r\\n").append("FileWriter append something\\r\\n");writer.close();} catch (IOException e) {e.printStackTrace();}}

运行说明:

1)append返回writher,可以链式编程
2)write没有返回值,只能一句句执行

3)FileWriter简化字符流的写入,new FileWriter(path, true)等同于new OutputStreamWriter(new FileOutputStream(path, true))

BufferedWriter写入文本

    public void write3(){String path = "writeTest.txt";FileOutputStream output = null;OutputStreamWriter oWriter= null;try {output = new FileOutputStream(path, true);oWriter = new OutputStreamWriter(output, "utf-8");BufferedWriter writer = new BufferedWriter(oWriter);writer.append("BufferedWriter append something\\r\\n").append("BufferedWriter 写中文\\r\\n").append("BufferedWriter append something\\r\\n");writer.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(output != null){try {output.close();} catch (IOException e) {e.printStackTrace();}}if(oWriter != null){try {oWriter.close();} catch (IOException e) {e.printStackTrace();}}}}public void write5(){String path = "G:\\\\学习类\\\\thread\\\\doc\\\\writeTest.txt";FileWriter fWriter;try {fWriter = new FileWriter(path, true);BufferedWriter writer = new BufferedWriter(fWriter);writer.append("BufferedWriter append something\\r\\n").append("BufferedWriter 写中文\\r\\n").append("BufferedWriter append something\\r\\n");writer.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

运行说明:

比较快