1

I am trying to understand if I need to read data from different types of files (.properties file, json file, text file etc) or from console, which java class should I use and why exactly.

Some classes are reading data in the form of bytes (8 bits) and some are reading in the form of characters (16 bits unicode). So, how do I decide which class to use for reading data?

enter image description here

In the above sample code, I am trying to read a .properties file. So, how to decide if I need to use FileInputStream or any other class to read the file?

I tried looking for answers online but I am still not clear.

  • 1
    Please post your code as text not as an image. – vanje Feb 17 '23 at 09:24
  • Basically, you use a Reader if it is character data, and an InputStream if it is binary data, and otherwise you look at the API for a specific class for guidance (e.g. sometimes you can offer an InputStream + Charset, or you can offer a Reader, in that case you decide based on what you have available), but this is not something that is generally answerable. – Mark Rotteveel Feb 17 '23 at 14:39
  • Please provide enough code so others can better understand or reproduce the problem. – kerbermeister Feb 17 '23 at 21:51

1 Answers1

1

It depends on the individual case.

For property files, there is an overloaded Properties.load() method. You can pass either an InputStream or a Reader. Here it would be best to use the InputStream option (byte stream) because the load() function handles the correct character set on its own. If you use a Reader you are responsible to read the file with the correct encoding.

Same for parsing XML files. The encoding is part of the XML header (or UTF-8 as default) so it is the best option to let the parser read and handle it.

For JSON the default encoding is UTF-8. Other encodings are possible but I don't know whether it is possible to declare the encoding inside a JSON document like in XML.

So for other file types it depends on the use case. If you have a text file encoded as UTF-8 and you want to copy it to another location as is, you can simply treat the file as a byte block. But if you have to extract some words it is necessary to interpret the byte block as characters so you need a Reader and the right character encoding (in conjunction with an InputStreamReader).

Sometimes you get the right encoding via an API e.g. if you call a REST service via HTTP you can extract the encoding out of a HTTP header. Otherwise, you simply have to know the encoding.

vanje
  • 10,180
  • 2
  • 31
  • 47