1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public class filestream { public static void main(String[] args) throws IOException { File f=new File("D:\\Project\\Java\\test.txt"); FileOutputStream fop=new FileOutputStream(f, true); OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8"); writer.append("中文测试"); writer.append("\n\r"); writer.append("English"); writer.close(); fop.close(); FileInputStream fip = new FileInputStream(f); InputStreamReader reader = new InputStreamReader(fip, "UTF-8"); StringBuffer sb = new StringBuffer(); while (reader.ready()){ sb.append((char) reader.read()); } System.out.println(sb.toString()); reader.close(); fip.close(); } }
|