Test public void TestObjectOutputStream throws IOException String name = John Smith; 创建文件输出流对象将数据写入nametxt FileOutputStream fos=new FileOutputStreamsrctest11nametxttrue;
可以使用字符流来避免乱码问题,同时需要注意编码格式。以下是修改后的代码:
@Test public void TestObjectOutputStream() throws IOException{ String name = "John Smith"; //创建文件输出流对象,将数据写入name.txt FileOutputStream fos = new FileOutputStream("src/test11/name.txt", true); //创建字符输出流对象,指定编码格式 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); //创建对象输出流输出对象 ObjectOutputStream oos = new ObjectOutputStream(osw); oos.writeObject(name); oos.flush(); oos.close(); }
原文地址: https://www.cveoy.top/t/topic/gFnc 著作权归作者所有。请勿转载和采集!