简体   繁体   English

FileInputStream和FileOutputStream逐行

[英]FileInputStream and FileOutputStream line by line

FileInputStream reads all bytes of a file and FileOutputStream writes allbytes to a file FileInputStream读取文件的所有字节,FileOutputStream将allbytes写入文件

which class do i use if i want to read all bytes of a file but line by line 如果我想读取文件的所有字节但逐行读取,我会使用哪个类

so that 以便

if fileA contains two lines 如果fileA包含两行

line1 line2 line1 line2

then bytes of line1 and line2 are read seperately 然后单独读取line1和line2的字节

same goes for FileOutputStream FileOutputStream也是如此

Fredrik is right about BufferedReader , but I'd disagree about PrintWriter - my problem with PrintWriter is that it swallows exceptions. 弗雷德里克是正确的约BufferedReader ,但我不同意PrintWriter -我有问题PrintWriter是,它燕子例外。

It's worth understanding why FileInputStream and FileOutputStream don't have any methods relating to lines though: the *Stream classes are about streams of binary data. 值得理解的是,为什么FileInputStreamFileOutputStream没有任何与行相关的方法: *Stream类是关于二进制数据流的。 There's no such thing as a "line" in terms of binary data. 就二进制数据而言,没有“线”这样的东西。 The *Reader and *Writer classes are about text , where the concept of a line makes a lot more sense... although a general Reader doesn't have enough smarts to read a line (just a block of characters) so that's where BufferedReader comes in. *Reader*Writer类是关于文本的 ,其中一行的概念更有意义......虽然一般Reader没有足够的智能来读取一行(只是一个字符块)所以这就是BufferedReader进来。

InputStreamReader and OutputStreamWriter are adapter classes, applying a character encoding to a stream of bytes to convert them into characters, or a stream of characters to turn them into bytes. InputStreamReaderOutputStreamWriter是适配器类,将字符编码应用于字节流以将其转换为字符,或将字符流转换为字节。

So, you probably want a BufferedReader wrapping an InputStreamReader wrapping a FileInputStream for reading - then call readLine() . 因此,您可能希望BufferedReader包装一个包含FileInputStreamInputStreamReader以进行读取 - 然后调用readLine() For writing, use a BufferedWriter wrapping an OutputStreamWriter wrapping a FileOutputStream - then call write(String) and newLine() . 对于写作,使用BufferedWriter包装的OutputStreamWriter包装一个FileOutputStream -然后调用write(String)newLine() (That will give you the platform default line separator - if you want a specific one, just write it as a string.) (这将为您提供平台默认行分隔符 - 如果您需要特定的分隔符,只需将其写为字符串。)

There's also the FileReader class which sort of combines FileInputStream and InputStreamReader (and FileWriter does the equivalent) but these always use the platform default encoding, which is almost never what you want. 还有FileReader类,它结合了FileInputStreamInputStreamReader (和FileWriter相同),但它们总是使用平台默认编码,这几乎不是你想要的。 That makes them all but useless IMO. 这使得他们几乎无用的IMO。

I think what you are looking for is a BufferedReader and a PrintWriter. 我认为你要找的是BufferedReader和PrintWriter。

Check out this one for a sample of the first: http://www.java2s.com/Tutorial/Java/0180__File/CreateBufferedReaderfromInputStreamReader.htm 看看这个是第一个样本: http//www.java2s.com/Tutorial/Java/0180__File/CreateBufferedReaderfromInputStreamReader.htm

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

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