I’m a newbie so the only ways to read a file in Java I know are:
DataInputStream dis = new DataInputStream(new FileInputStream(<FILE>));
and:
BufferedReader br = new BufferedReader(new FileReader(<FILE>));
And I know that while the first one serves the purpose of handling binary files, the latter is used for character/text files (for my current understanding: such that I can open them with, say, notepad and see something a human can actually read). The problem is: what if the problem does not state whether I’m to deal with a binary or text file? I mean: when I’m to read an object from a file, for example, I know it’s the first, but what when I’m only given info like “Your method is to read from the file XYZ which contains names of the students which you are to process (and so on and so on)”?
I’ve come across some such tasks and was never sure which one to use. Can I use whatever I want if the description doesn’t explicitly state the type of the file or is there some convention that if nothing is said, it’s safer to assume we’re dealing with X?
You do need to know which type you want to read.
If the spec is unclear, define (and document) it for yourself. If you already have an existing file, you can have a look at it to decide which approach to take – if it is line oriented, with no “unreadable” characters in it, e.g. one name at a line, you would take the text file approach. If it looks strange when opened in a text editor, you can assume a binary file, but in this case you need additional information anyways to be able to correctly read it.
On a file system level, there is no such thing like “binary” or “text” file – it depends on how you interpret it. You can read each file as a binary file (considering the individual bytes – this is what hex editors do), and you can read (almost) each file as text (and most likely end up with something which looks like garbage when you try to print it). (I said “almost” since there are some constraints with multibyte characters, where one character is built from more than one byte. But even these files could still be treated as “extended ASCII” files where each byte maps to exactly one character).
In any case, you should document the file format you expect (even “text” files can have different structures, like fixed line length, variable line length and so on).