简体   繁体   English

是否可以在Java中将Char流转换为Byte流?

[英]Is it possible to convert Char streams to Byte streams in Java?

If it is possible to convert character streams to byte streams into a file? 是否可以将字符流从字节流转换为文件? Would you help me explaining it by means of a program? 您能通过程序帮助我解释一下吗?

Converting characters to bytes can be done with an OutputStreamWriter (which writes to a FileOutputStream ) which handles translating the characters to bytes. 可以使用OutputStreamWriter (将其写入FileOutputStream )完成将字符转换为字节的操作,该处理程序将字符转换为字节。 Make sure you specify an encoding, otherwise it will use the platform default encoding and suddenly it won't work the same on all platforms anymore. 确保指定了一种编码,否则它将使用平台的默认编码,并且突然之间,它将不再在所有平台上都可以使用。

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("myfile.txt"), "UTF-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM