2016-07-24
Reader
InputStreamReader
Writer
OutputStreamWriter
package com.java1995; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class TestOutputStreamWriter { public static void main(String[] args) { FileOutputStream fos = null; OutputStreamWriter osw = null; try { fos = new FileOutputStream("D:\\workspace\\java_io\\out.txt"); osw = new OutputStreamWriter(fos, "UTF-8"); String str = "大家好,很高兴见到各位。Hello ,nice to meet you!"; for (int i = 0; i < str.length(); i++) { osw.write(str.charAt(i)); } osw.flush();//这一步很重要 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭流 if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (osw != null) { try { osw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
【参考资料】
时间: 2024-11-04 15:41:59