Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read the complete contents of a file in a String?!  RSS feed

 
Sam Sum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I want to read the whole contents of a file in a String variable - it should be without problems not depending on whether this file is plain text or binary.
I tried using java.io.BufferedReader.readLine() but it ignores the line termination characters. The problem there is that I want to have those line termination characters included whatever they are!
I tried then using java.io.FileReader.read() to read the contents of the file character by character. My problem there is that the read method of FileReader returns an integer representation (and not char) of the characters. When I convert the int to char type I am concerned about loosing some information needed for retrieving binary data, because int is 4 byte and char only 2 byte.
So does anybody have a suggestion how to read all contents of a file of any type without loosing line termination characters and with no loss of information in case of binary data? Thanks for any help!
Part of code to read character by character, but with unwanted type conversion:
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fname lname,
Welcome to the Ranch. We do not have many rules around here, but we do have our Naming Policy, which prohibits obviously fictitious names, such as the one you are using.
Please go into your your profile, and change your display name to conform to our policy.
Thanks!
 
Sam Sum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jason adam,
I corrected it as you can see. What about answering my question now ?
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ranch Rover" is still not a valid name, as it falls in the "obviously fictitious" category. Please change it to something that's not, err, obviously fictitious.
As for the question - Trent's answer is good in general. However the question mentions binary data. The problem is, if a file represents something other than text data, it's an usually a bad idea to try to render it as a String. At best you get gibberish; at worst, you lose data. Unless perhaps you're using Base64 or a simple hexadecimal rendition, or something similar. But I general if you're dealing with non-textual data I'd recommend either using a DataInputStream, or just copying everything into a byte[] array rather than a String:

Also, for Trent's text solution, it's often necessary to specify a file encoding, e.g.:
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Y,
Thanks for the tip, i will amend the code above

[ January 25, 2004: Message edited by: Trent DiBacco ]
 
Sam Sum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Trent! Hello Jim!
Thank you very much for answering to my post! Jim has perfectly understood that I wanted to point out the reading of the contents of non-textual (binary) data. So I do hope that the byte array is indeed a working method to do that. What about the mentioned DataInputStream: can you please give a specific example concerning that?! I do not use any encoding principle, but I think it should be good to consider the 2-byte Unicode instead of UTF-8.

What about making the size of this byte array dynamic, depending on the size of the file? I considered using:

... but then I have a similar type conversion problem again, because an array requires an int type:

Can I somehow specify a Unicode encoding of the byte array elements in order to be sure that there will be no loss of information needed for the exact representation of the entire file contents?! And can you please explain why do you need the CharsetDecoder?!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!