简体   繁体   English

Java-字符和字节流

[英]Java - Character and Byte streams

I'm trying to create a program that allows me to get names from a local file and add them to a String array of names. 我正在尝试创建一个程序,该程序允许我从本地文件中获取名称并将其添加到名称的String数组中。 I don't completely understand the difference between character streams, binary streams, and buffer streams, and looking online didn't answer my question, which is: which type of stream would I use to read from a text file in order to create a String array? 我不完全了解字符流,二进制流和缓冲区流之间的区别,并且在线查看并不能回答我的问题,即:我将使用哪种类型的流来读取文本文件以创建文本流?字符串数组?

I don't completely understand the difference between character streams, binary streams, and buffer streams 我不完全了解字符流,二进制流和缓冲区流之间的区别

Buffers are a red herring here - that's just an implementation detail, usually to make things more efficient. 缓冲区在这里是一个红鲱鱼-只是实现细节,通常是为了使事情更有效率。 It's important to understand whether you're reading binary data or text data. 了解您要读取二进制数据还是文本数据很重要。 If you're reading a text file, you want a Reader of some description. 如果您正在阅读文本文件,则需要具有一些描述的Reader Your file contains binary data (all files are basically bytes) and you need to say how to convert that to text. 您的文件包含二进制数据(所有文件基本上都是字节),您需要说一下如何将其转换为文本。 You could use a FileReader , but I'd prefer to use a FileInputStream wrapped in an InputStreamReader , as then you can specify the encoding to convert between binary and text. 可以使用FileReader ,但是我更喜欢使用包装在InputStreamReaderFileInputStream ,因为这样您就可以指定在二进制和文本之间转换的编码 You'll need to know the encoding of your file, eg UTF-8. 您需要知道文件的编码,例如UTF-8。

Any InputStream returns just binary data; 任何InputStream返回二进制数据。 and Reader returns textual data. 然后Reader返回文本数据。

Either way, if you want to read line by line (it's unclear what your array would consist of) you'll want a BufferedReader to wrap your InputStreamReader or FileReader , as that provides a readLine() method. 无论哪种方式,如果要逐行读取(不清楚数组将由什么组成),则需要BufferedReader来包装InputStreamReaderFileReader ,因为它提供了readLine()方法。

Hmmm then why would people ever use byte streams, if we all use characters. 嗯,如果我们所有人都使用字符,为什么人们会使用字节流。

We don't. 我们没有。 Image files, music, video, compressed data, encrypted data etc aren't inherently textual data. 图像文件,音乐,视频,压缩数据,加密数据等并不是天生的文本数据。 If you read an image file with a Reader , you're almost bound to lose some data. 如果使用Reader读取图像文件,则几乎注定会丢失一些数据。

Think of text as just another file format - if you were trying to load an image to display it, you'd need something which understood that image file format; 可以将文本视为另一种文件格式-如果您试图加载图像以显示它,则需要能够理解该图像文件格式的内容。 if you were trying to load a music file to play it, you'd need something which understood that audio file format - with text, an InputStreamReader understands text. 如果您尝试加载音乐文件以播放音乐,则需要某种能够理解音频文件格式的内容-使用文本, InputStreamReader理解文本。

Even though in all cases we've got bytes at the file level, the class you use determines how those bytes are interpreted. 即使在所有情况下我们都在文件级别获得字节,但您使用的类都决定了如何解释这些字节。

The difference is simple. 区别很简单。 According to this tutorial 根据本教程

  1. Byte Streams handle I/O of raw binary data. 字节流处理原始二进制数据的I / O。
  2. Character Streams handle I/O of character data, automatically handling translation to and from the local character set. 字符流处理字符数据的I / O,自动处理与本地字符集之间的转换。
  3. Buffered Streams optimize input and output by reducing the number of calls to the native API. 缓冲流通过减少对本机API的调用次数来优化输入和输出。

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

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