This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ragtime
Ragtime wrote ...

2013/4/7

Writing lines in a textfile

Ragtime Ragtime

2013/4/7

#
Hi, I'm trying to write some integers in a text file using the following code: out = new FileWriter("data.txt"); out.write(""+health); out.write(""+punkte); out.write(""+refx); out.write(""+getRotation()); out.write(""+speed); The problem is that this code writes them in a single line and when I try to read them it shows me one single number istead of five. I would be thankful if anyone could tell me how to write the integers each in a separate line. The code I use for reading is the following: while((line = reader.readLine()) != null) { value = Integer.parseInt(line); }
Gevater_Tod4711 Gevater_Tod4711

2013/4/7

#
If you want to start a new line you need the escape sequence '\n'. That will start a new line. Probably you'll see a quadrat instead of a new line in the textfile but if you read it the computer will interprete it as a new line.
Ragtime Ragtime

2013/4/7

#
Thank you.
davmac davmac

2013/4/7

#
Technically you should use '\r\n' for Windows text files and just '\n' for Mac / unix style text files. However, as Gevater_Tod says, it will usually work just fine if you use '\n'.
You need to login to post a reply.