reading bytes from a text file

V

vijayanand85

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.
 
C

Chase Preuninger

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top