Reading a character from a file

I am experimenting with all sorts of streams and I am way beyond confusion. I can't find any helpful tutorials. But anyway, I want to know If I am doing something wrong here. The output should be an 'A' but I get nothing but a newline character.

import java.io.*;

publicclass ReadText{

publicstaticvoid main(String[] blah){

try

{

InputStreamReader in =new InputStreamReader(new FileInputStream(new File("test.txt")));

System.out.println(Character.forDigit(in.read(),10));

}catch (Exception ex)

{

ex.printStackTrace();

}

//test.txt is a text file with nothing but the letter 'A' in it.

}

}

No errors, it just doesn't work.

I have a feeling it is the radix. What the heck is a radix anyway? I just put a random number in there.

[1466 byte] By [Galm_1a] at [2007-11-27 5:36:22]
# 1

According to the API documentation for read() it "Reads a single character" and "The character read, or -1 if the end of the stream has been reached". http://java.sun.com/javase/6/docs/api/java/io/InputStreamReader.html

Perhaps the following sheds some light on what's going on.import java.io.*;

public class ReadText{

public static void main(String[] blah) {

try

{

InputStreamReader in = new InputStreamReader(

new FileInputStream(new File("test.txt")));

// nb: ch is an *int* whose value will be that of a

// character unless the end of file has been reached.

int ch = in.read();

if(ch == -1) {

System.out.println("End of stream!");

System.exit(0);

}

System.out.println("The int value ch is " + ch);

System.out.println("As a char it is " + (char)ch);

//System.out.println(Character.forDigit(ch,10));

} catch (Exception ex)

{

ex.printStackTrace();

}

//test.txt is a text file with nothing but the letter 'A' in it.

}

}

The output isThe int value ch is 65

As a char it is AAgain you need to consult the API documentation for the forDigit() method. http://java.sun.com/javase/6/docs/api/java/lang/Character.html Basically it takes two aguments: the "radix" (an int) is the base of the number system eg

radix 2: for binary numerals written with the two characters '0' and '1'

radix 10: for decimal numerals written with the ten characters '0' -> '9'

radix 16: for hex numerals written with the 16 characters '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'e', 'f'

Notice that numerals are not numbers they are just strings like "5d" and they are made up (like other strings) of characters. We call the characters that make up numeral strings, digits.

The other argument for forDigit() is the digit (another int value). The idea is we send the method this digit as an int (a number) and we get back a character. So, eg, in base 10 we send forDigit() the digit 7 and we get back '7'forDigit(7, 10)=='7'

And using hex numerals we send the method 13 and we get back 'd'forDigit(13, 16)=='d'

As the documentation explains digit has to be less than radix, otherwise it is invalid and the null character is returned. This is what happens in your program because you send it 65 as the digit value. This is reasonable because in base 10 only the numbers 0->9 can be represented as a digit (a single character).

pbrockway2a at 2007-7-12 15:06:56 > top of Java-index,Java Essentials,New To Java...