正确的Java类读取和写入文件? [英] Proper Java classes for reading and writing files?

查看:120
本文介绍了正确的Java类读取和写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读一些关于Java文件I / O管理的资料,我知道有很多输入和输出操作的替代方法。



这些是: / p>


  • BufferedReader BufferedWriter

  • FileReader FileWriter

  • FileInputStream FileOutputStream

  • InputStreamReader OutputStreamWriter

  • 扫描仪


这些是管理文本文件的最佳选择吗?什么是序列化的最佳选择? Java NIO对此有何看法? 两种数据

一般而言,有两个世界:

$ b $二进制数据

  • 文本数据



  • 当它是一个文件(或者一个套接字,或者一个DB中的BLOB,或者...)时,二进制数据 first

    一些二进制数据可以作为文本数据处理 编码或字符编码)。

    二进制数据



    无论何时您想要处理二进制数据,都需要使用 InputStream / 的类) >流在它的名字)。

    这就是为什么有一个 FileInputStream FileOutputStream :从 处理二进制数据。



    文本数据



    无论何时处理文本数据,那么您需要使用 Reader / Writer 类。

    无论什么时候你需要将二进制数据转换为文本(反之亦然),那么你需要某种编码是UTF-8,UTF-16,ISO-8859-1(以及相关的)和美国旧式的美国ASCII码)。 幸运的是Java平台也有一种叫做默认平台编码的东西,只要它需要它就会使用,但是代码没有指定一个。



    平台默认编码是双面剑,但是:


    • 它使得编写代码更容易,因为您不必指定编码每个操作

    • 它可能与您拥有的数据不匹配:如果平台默认编码为ISO-8859-1,并且您读取的文件实际上是UTF -8,那么你会得到一个混乱的输出!



    对于阅读,我们还应该提及 BufferedReader ,它可以被其他任何 Reader 并增加了一次处理整行的功能。



    Scanner 是一个特殊的类,将文本输入解析为令牌。对结构化文本最为有用,但经常用于 System.in 来提供一种非常简单的方法来从标准输入读取数据(即从用户在键盘上的输入)。

    布里奇的差距



    现在,混淆足够的有类,使桥在这些世界中,它们的名字中通常包含两部分


    • an InputStreamReader 消耗 a InputStream 并且本身 a Reader code>

    • OutputStreamWriter 是一个 Writer code>和写入 OutputStream



    然后有一些快捷类,它们基本上结合了其他两个经常组合在一起的类。


    • a FileReader 基本上是一个 FileInputStream 和一个 InputStreamReader

    • a FileWriter 基本上是 FileOutputStream OutputStreamWriter

    • 的组合注意, FileReader FileWriter 有一个主要的缺点与他们更复杂的手工构建替代方案相比,他们总是使用平台的默认编码,这可能不是你想要做的!



      序列化怎么样?



      ObjectOutputStream ObjectInputStream 是用于序列化的特殊流。



      这些类意味着序列化仅涉及二进制数据(即使序列化 String 对象),所以您需要使用 * Stream 专门的课程。只要你避免任何 Reader / Writer 类,你应该没问题。



      其他资源




      Reading some sources about Java file I/O managing, I get to know that there are more than 1 alternative for input and output operations.

      These are:

      • BufferedReader and BufferedWriter
      • FileReader and FileWriter
      • FileInputStream and FileOutputStream
      • InputStreamReader and OutputStreamWriter
      • Scanner class

      What of these is best alternative for text files managing? What's best alternative for serialization? What does Java NIO say about it?

      解决方案

      Two kinds of data

      Generally speaking there are two "worlds":

      • binary data
      • text data

      When it's a file (or a socket, or a BLOB in a DB, or ...), then it's always binary data first.

      Some of that binary data can be treated as text data (which involves something called an "encoding" or "character encoding").

      Binary Data

      Whenever you want to handle the binary data then you need to use the InputStream/OutputStream classes (generally, everything that contains Stream in its name).

      That's why there's a FileInputStream and a FileOutputStream: those read from and write to files and they handle binary data.

      Text Data

      Whenever you want to handle text data, then you need to use the Reader/Writer classes.

      Whenever you need to convert binary data to text (or vice versa), then you need some kind of encoding (common ones are UTF-8, UTF-16, ISO-8859-1 (and related ones) and the good old US-ASCII). "Luckily" the Java platform also has something called the "default platform encoding" which it will use whenever it needs one but the code doesn't specify one.

      The platform default encoding is a two-sided sword, however:

      • it makes writing code easier, because you don't have to specify an encoding for each operation but
      • it might not match the data you have: If the platform-default encoding is ISO-8859-1 and the file you read is actually UTF-8, then you will get a scrambled output!

      For reading, we should also mention the BufferedReader which can be wrapped around any other Reader and adds the ability to handle whole lines at once.

      Scanner is a special class that's meant to parse text input into tokens. It's most useful for structured text but often used on System.in to provide a very simple way to read data from stdin (i.e. from what the user inputs on the keyboard).

      Bridgin the gap

      Now, confusingly enough there are classes that make the bridge between those worlds, which generally have both parts in their names:

      • an InputStreamReader consumes a InputStream and is itself a Reader.
      • an OutputStreamWriter is a Writer and writes to an OutputStream.

      And then there are "shortcut classes" that basically combine two other classes that are often combined.

      • a FileReader is basically a combination of a FileInputStream with an InputStreamReader
      • a FileWriter is basically a combination of a FileOutputStream with an OutputStreamWriter

      Note that FileReader and FileWriter have a major drawback compared to their more complicated "hand-built" alternative: they always use the platform default encoding, which might not be what you're trying to do!

      What about serialization?

      ObjectOutputStream and ObjectInputStream are special streams used for serialization.

      As the name of the classes implies serializing involves only binary data (even if serializing String objects), so you'll want to use *Stream classes exclusively. As long as you avoid any Reader/Writer classes, you should be fine.

      Further resources

      这篇关于正确的Java类读取和写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆