Can anyone show me how to read a  text file?

hi there :slight_smile:

i have installed java 1.4.2 and i cant figure out how to read from a text file that ill be using for a map in some rpg game…tried a lot of examples that i found on the net but they where all deprecated or something. i have programming experience in basic, pascal and c languages but im so new to java :-[ !
i already tried some tutorials but the io part seems complicated.

i want to read the text from a file into an array(in a simple way)

for example i would have numbers in the text file
1 3 2
2 1 1
1 3 4

would be and array of 9, each one represents a tile in the game

please help ???

Firstly, google for the “java tutorials” by “Sun” (also known as Trails or JFC etc) and do them all - these are the free official java tutorials.

You definitely don’t want to store your map like that, though. But you can worry about more sensible (and easier to do) ways of storing your map once you’ve learnt hte basics of htose tutorials.

(you will probably want to use either XML (DOM) or a binary-format file for maps - or just download something like Mappy (google for it) and let it take care of the work)

:slight_smile:

thx im finally getting somewhere with these tutorials

Install java 1.5 and use the new Scanner class. It’s very good for C++ style reading.

//Scanner s=new Scanner(System.in);
or
// Scanner s=new Scanner(new File(“input.txt”));

s.nextInt();
s.nextFloat();

and so on.