1

I know the correct approach is

BufferedReader br = new BufferedReader(FileReader(file));

but I checked in the Java API the methods of FileReader class and it has a read() method that return the character read. But it's not working for me. I'm reading a file that contains this line of text: "I'm reading a file", and printing it to the screen, but when I run program it prints the equivalent ASCII code for each character.

This is my code:

String lectura = "";
try{
    lectura = String.valueOf(fr.read());
} catch(IOException ioex){
    ioex.printStackTrace();
}

while((Integer.parseInt(lectura)) != -1){
    System.out.print(lectura);
    try{
        lectura = String.valueOf(fr.read());
    } catch(IOException ioex){
        ioex.printStackTrace();
    }
}

3 Answers 3

5

What is happening is this -

fr.read() is returning an integer and not a character, although that integer returned is the equivalent ASCII code for that character.

When you call String.valueOf() on that integer, the String.valueOf(int i) method gets called, instead of the String.valueOf(char c) method which you are expecting to get called, and therefore it returns the ASCII value as a string, instead of the character value as a string.

So instead, try

    lectura = String.valueOf( (char) (fr.read()) );
Sign up to request clarification or add additional context in comments.

7 Comments

Almost -- the ints are not ASCII values, FileReader uses the platform's default encoding.
Well, if the file was written with normal 7-bit ASCII characters there would be no difference. But if a text editor wrote it using latin1 for example, fr.read() would return some int's which weren't valid ASCII codes. (Or it might return nonsense, if Charset.defaultCharset() is something like UTF-8.)
Ok, but now I'm having troubles to stop the reading cicle because I do it with while(Integer.parseInt(lectura) != -1) and I get a NumberFormatException when the value of lectura is not a number and that only happens when the end of the file is reach.
@Aikanáro you need to check that fr.read() did not return -1, and if it did, stop without changing lectura.
Well, this is what I did: <br/> while(true){if(fr.read() == -1) break; else lectura = String.valueOf((char)fr.read())}
|
2

This is expected behavior, text is stored as bytes and each byte has the ASCII value of the character. You can collect these bytes into a byte array and then use new String(array); to convert them into a string. Or you can cast to char to convert individual characters.

1 Comment

An InputStream reads bytes, a Reader reads characters. (What makes it confusing is that both are encoded as ints.) This is why InputStreamReader needs a Charset; it would be incorrect to just treat the bytes as chars, although it would work for ASCII. You wouldn't want to build a byte array from a Reader and call new String(byte[]); you would build a character array and call new String(char[]).
1

FileReader's read returns an int. You just need to cast this to a char.

The reason Reader's read doesn't just return a char is that using an int lets it use -1 to represent end-of-file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.