Home  >  Article  >  How to solve the objectoutputstream garbled problem

How to solve the objectoutputstream garbled problem

藏色散人
藏色散人Original
2021-06-26 11:12:5435907browse

objectoutputstream乱码的解决办法:首先找到出现乱码的代码部分;然后通过“new PrintStream(new FileOutputStream("c:\\cc.txt"));”写字符串即可。

How to solve the objectoutputstream garbled problem

本文操作环境:windows7系统、Dell G3电脑。

如何解决objectoutputstream乱码问题?

记录ObjectOutputStream输出乱码问题

issue:

FileOutputStream file = new FileOutputStream("D:/test.txt");
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject("test01\r\n");
output.writeObject("test02\r\n");
output.close();
file.close();

输出的结果如下:

 �t test01
t test02

解决方案

writeObject写到文件里的是String类的序列化内容,当然不能正确查看了,写字符串用PrintStream比较好。而且输出“\n”和“\r\n”都不是很好的方法,因为不同的平台回车符是不一样,如下解决跨平台问题的换行:

PrintStream file =  new PrintStream(new FileOutputStream("c:\\cc.txt"));
file.println("test01");
file.println("test02");

更多相关知识,请访问常见问题栏目!

The above is the detailed content of How to solve the objectoutputstream garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn