java FileInputStream 和 FileOutputStream 逐行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileInputStream and FileOutputStream line by line
提问by rover12
FileInputStream reads all bytes of a file and FileOutputStream writes allbytes to a file
FileInputStream 读取文件的所有字节,FileOutputStream 将所有字节写入文件
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
线 1 线 2
then bytes of line1 and line2 are read seperately
然后分别读取 line1 和 line2 的字节
same goes for FileOutputStream
FileOutputStream 也一样
回答by Jon Skeet
Fredrik is right about BufferedReader, but I'd disagree about PrintWriter- my problem with PrintWriteris that it swallows exceptions.
Fredrik 是对的BufferedReader,但我不同意PrintWriter- 我的问题PrintWriter是它吞下了异常。
It's worth understanding why FileInputStreamand FileOutputStreamdon't have any methods relating to lines though: the *Streamclasses are about streams of binary data. There's no such thing as a "line" in terms of binary data. The *Readerand *Writerclasses are about text, where the concept of a line makes a lot more sense... although a general Readerdoesn't have enough smarts to read a line (just a block of characters) so that's where BufferedReadercomes in.
值得理解为什么FileInputStream并且FileOutputStream没有任何与行相关的方法:这些*Stream类是关于二进制数据流的。就二进制数据而言,没有“行”这样的东西。在*Reader和*Writer类是有关文本,其中一条线的概念使很多更有意义......虽然一般Reader没有足够的智慧来读一本线(只是字符块),所以这就是BufferedReader用武之地。
InputStreamReaderand OutputStreamWriterare 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.
InputStreamReader和OutputStreamWriter是适配器类,将字符编码应用于字节流以将它们转换为字符,或将字符流应用于将它们转换为字节。
So, you probably want a BufferedReaderwrapping an InputStreamReaderwrapping a FileInputStreamfor reading - then call readLine(). For writing, use a BufferedWriterwrapping an OutputStreamWriterwrapping a FileOutputStream- then call write(String)and newLine(). (That will give you the platform default line separator - if you want a specific one, just write it as a string.)
所以,你可能想要一个BufferedReader包装一个InputStreamReader包装 aFileInputStream用于阅读 - 然后调用readLine(). 对于写作,使用一个BufferedWriter包装一个OutputStreamWriter包装 a FileOutputStream- 然后调用write(String)和newLine()。(这将为您提供平台默认行分隔符 - 如果您想要一个特定的分隔符,只需将其写为字符串。)
There's also the FileReaderclass which sort of combines FileInputStreamand InputStreamReader(and FileWriterdoes the equivalent) but these always use the platform default encoding, which is almost neverwhat you want. That makes them all but useless IMO.
还有一FileReader类结合了FileInputStream和InputStreamReader(并FileWriter做等效的),但这些总是使用平台默认编码,这几乎从来不是你想要的。这使得它们几乎毫无用处。
回答by Fredrik
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

