-3

What is the practical use of the below code for binary files- convert bytes streams to character streams:

InputStream in = new FileInputStream("E:\\Users\\17496382.WUDIP\\Desktop\\jio.doc");
InputStreamReader ir = new InputStreamReader(in,"UTF8");
BufferedReader br=new BufferedReader(ir);
String s;
while((s=br.readLine())!= null){
System.out.println(s);  
Senthil
  • 35
  • 1
  • 4
  • Well, here is a fact: the data storage format for a Word document is full of metadata that describes the document to the Word software. In order to fully decode a Word document, you need to do one of the following: 1) use a library that can decode the storage format and present it to you in a form that is easier for you to work with; or 2) find documentation on the storage format and write your own custom file stream. – scottb Dec 30 '15 at 14:16
  • There is no documentation on MS Word format. This is closed source protected by copyright laws. – Sharon Ben Asher Dec 30 '15 at 14:19
  • tx. what is the practical usage of InputStreamReader ir = new InputStreamReader(in,"UTF8"); (what exactly bytes stream to character streams is done here) – Senthil Dec 30 '15 at 14:23
  • @Senthil You can read text files using that approach. Try using Windows Notepad to save a text file using UTF-8 format. You should be able to decode it with your code snippet above. However, the code snippet cannot decode files which are not text files. It can for example not decode `.jpg` files or `.doc` files. – Alderath Dec 30 '15 at 14:39
  • tx for the response, If it is just text files, I can directly use FileReader to read the files (if I want to use character set, then use NIO Buffered reader to mention the character set). Not able to understand under what scnerio I should use - InputStreamReader ir = new InputStreamReader(in,"UTF8"); – Senthil Dec 30 '15 at 14:48

2 Answers2

0

word document consists of binary meta data in addition to the text. You can plainly see this is you try to open the file with notepad. You need a library like Apache POI for this. See here how to correctly read MS Word document

Community
  • 1
  • 1
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

You are correct when you say that there is little value in opening a FileInputStream and using a InputStreamReader on it to allow it to be used as an instance of Reader. You can just as easily open the file using FileReader.

If I understand your question, you are asking "what is the use of InputStreamReader?" The short answer is that there are many, many other instances of classes that extend InputStream besides FileInputStream. If you needed a Reader and all you had was an instance of InputStream, then you would wrap it with an InputStreamReader.

See http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html for some classes that extend InputStreamReader.

(None of these have any bearing at all on the ability to read a Microsoft Word file, however.)

Mike Harris
  • 1,487
  • 13
  • 20
  • ok, you mean...for ex- when we have urls as inputstream and want to read it on screen , we can use inputstreamreader and read it using readers – Senthil Dec 30 '15 at 15:14
  • Some things work with byte streams, some work with character streams. Sometimes you need to convert from one to the other. That's pretty much all there is to say about it. – Mike Harris Dec 30 '15 at 15:22