Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

reading bytes from a text file

Thread view: 
vijayanand85@gmail.com - 06 Apr 2008 05:13 GMT
hi,
i want to read bytes from a text file
Filereader and BufferedReader reads each letter in a text file as
character which is 2 bytes in java.
But i need to read the letters as bytes, which is 1 byte, since each
printable letter can be represented in 1 byte.

Thanks in advance,
Vijay Anand.
Zig - 06 Apr 2008 06:33 GMT
> i want to read bytes from a text file
> Filereader and BufferedReader reads each letter in a text file as
> character which is 2 bytes in java.

This is possible, but what is your reason for doing this? If you site your  
reason, the answer will probably become clear. If you are doing something  
like a MD-5 digest, you should recognize that digesters and other file  
functions expect to work on data files, without knowing whether or not the  
file is text.

On the other hand, if you need to write the content of a file to an  
external data stream, you will need to address the issue of whether or not  
the file's encoding will be different from the external stream's encoding.  
If you are uploading a local file to a server, the server might represent  
a character with a different byte than was used for the same character in  
the local file.

> But i need to read the letters as bytes, which is 1 byte, since each
> printable letter can be represented in 1 byte.

Only around 100 out of some 60,000 printable characters are representable  
as 1 byte in UTF-8. So, your reason should guide your search for a  
solution.

HTH,

-Zig
Peter Duniho - 06 Apr 2008 06:34 GMT
> hi,
> i want to read bytes from a text file
> Filereader and BufferedReader reads each letter in a text file as
> character which is 2 bytes in java.
> But i need to read the letters as bytes, which is 1 byte, since each
> printable letter can be represented in 1 byte.

Instead of the convenience class FileReader, use the InputStreamReader  
specifying an appropriate charset and of course an actual stream  
representing the file.

You're not specific about what encoding you want to use, but given your  
claim that in the file each character is one byte, it's likely that  
"US-ASCII" (7-bit) or "ISO-8859-1" (8-bit) would be appropriate (actually,  
you could probably get away with specifying "UTF-8" for that matter,  
though I don't feel that'd strictly speaking be correct :) ).

Pete
Roedy Green - 06 Apr 2008 09:21 GMT
On Sat, 5 Apr 2008 21:13:06 -0700 (PDT), "vijayanand85@gmail.com"
<vijayanand85@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>i want to read bytes from a text file
>Filereader and BufferedReader reads each letter in a text file as
>character which is 2 bytes in java.
>But i need to read the letters as bytes, which is 1 byte, since each
>printable letter can be represented in 1 byte.

It is much more complicated than that.  see
http://mindprod.com/jgloss/encoding.html

For sample code, see http://mindprod.com/applet/fileio.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chase Preuninger - 06 Apr 2008 13:02 GMT
Just use the InputStream class, about all it can do is read bytes.

InputStream in = new FileInputStream("myFile.txt");  //works for any
file
int b;  //need to use an in to get unsigned bytes because a byte in
java goes from -128 to +128
while((b = in.read()) != -1) //returns -1 at end of stream
{
   //do something with the byte
}
Lew - 06 Apr 2008 15:08 GMT
> Just use the InputStream class, about all it can do is read bytes.
>
[quoted text clipped - 6 lines]
>     //do something with the byte
> }

The OP referred to the file containing "printable letters".  That means they
are dealing with chars whether they like it or not.  Bringing in the file as
bytes will not let them treat the contents reliably as character sequences or
Strings, since matters of character encoding will always be relevant there.
Treating the data as raw bytes *without regard for their interpretation as
characters* is fine, but as soon as one is interested in the values as
characters that all changes.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.