从文件中读取字符
我想一次读取一个文件一个字符,并将第一个文件的内容一次一个字符写入另一个文件。
我之前也问过这个问题,但没有得到满意的答案...... 我能够读取文件并将其打印到 std o/p。但无法将相同的读取字符写入文件。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想一次读取一个文件一个字符,并将第一个文件的内容一次一个字符写入另一个文件。
我之前也问过这个问题,但没有得到满意的答案...... 我能够读取文件并将其打印到 std o/p。但无法将相同的读取字符写入文件。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
链接到您之前的问题以查看哪些方面不满意可能会很有用。这是一个基本示例:
需要考虑的其他事项:
It may have been useful to link to your previous question to see what was unsatisfactory. Here's a basic example:
Additional things to consider:
您可以使用
FileReader
(有一个read
方法,如果您愿意,可以一次执行一个字符),并且您可以使用FileWriter
(有一个字符-一次write
方法)。还有一些方法可以处理字符块而不是一次处理一个字符,但您似乎想要这些,所以...如果您不担心设置字符编码,那就太好了。如果是,请查看使用
FileInputStream< /code>
和
FileOutputStream
与InputStreamReader
和OutputStreamWriter
包装器(分别)。FileInputStream
和FileoutputStream
类使用字节,然后流读取器/写入器根据您选择的编码将字节转换为字符。You can read characters from a file by using a
FileReader
(there's aread
method that lets you do it one character at a time if you like), and you can write characters to a file using aFileWriter
(there's a one-character-at-a-timewrite
method). There are also methods to do blocks of characters rather than one character at a time, but you seemed to want those, so...That's great if you're not worried about setting the character encoding. If you are, look at using
FileInputStream
andFileOutputStream
withInputStreamReader
andOutputStreamWriter
wrappers (respectively). TheFileInputStream
andFileoutputStream
classes work with bytes, and then the stream reader/writers work with converting bytes to characters according to the encoding you choose.