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

2013/4/1

Writing a text file

Ragtime Ragtime

2013/4/1

#
Hi, I'm trying to save an integer called speed in a text file in order to read it afterwards. Therefore I've tried to use the following code: public void writefile() { FileWriter out = null; try { out = new FileWriter("data.txt"); BufferedWriter writer = new BufferedWriter(out); writer.write(speed); } catch (IOException e){ e.printStackTrace(); } finally { if(out != null) { try {out.close();} catch (IOException e) {e.printStackTrace();} } } } public void readfile() { String line = null; int value=0; FileReader file = null; try { file = new FileReader("data.txt"); BufferedReader reader = new BufferedReader(file); while((line = reader.readLine()) != null) { value = Integer.parseInt(line); } } catch (FileNotFoundException e) {throw new RuntimeException("Error");} catch (IOException e) {throw new RuntimeException("Error");} finally { if(file != null) try {file.close();} catch (IOException e) {e.printStackTrace();} } speed=value; } What happens is that at the end speed is 0,instead of what it should be. I'd be grateful for some help :)
Gevater_Tod4711 Gevater_Tod4711

2013/4/1

#
To me your code seems to be allright. Maybe you try to write the txt file before speed is initialised and so it's null. You should try to print the value of the variable on the console. If the variable has the riht value there something in your method is buggy. Otherwhise the bug is somewhere in the rest of the code. Or you could try to look up the txt file that has been writen. If it shows the right value your reading method is buggy. If it shows a wrong value it's the writing method.
Gevater_Tod4711 Gevater_Tod4711

2013/4/1

#
Another way would be to use a demo for reading and writing files like this one.
danpost danpost

2013/4/1

#
It may automatically convert to a String anyway, but you might try 'writer.write(""+speed);'
Ragtime Ragtime

2013/4/1

#
Thank you for the hints. I've tried printing the content of the file on the console and it shows the wrong value, so it must be the writing which I'll have to work on. Is it possible that the file has to be saved before it is closed? I don't know, but when I open the text document it is empty.
Gevater_Tod4711 Gevater_Tod4711

2013/4/1

#
Are there any exceptions thrown when you try to write the file? If not probably the variable speed is not initialised. That are the things I would look up first.
Ragtime Ragtime

2013/4/1

#
The variable is global. Besides that, I've tried to replace it by "Hello Wolrd!" in the writing method and it still displays null.
danpost danpost

2013/4/1

#
Try adding a 'writer.newLine();' statement after 'writer.write(speed);'
Ragtime Ragtime

2013/4/1

#
@danpost The same bug :( maybe I'll have to look for the mistake somewhere else. Thank you for your help guys.
danpost danpost

2013/4/1

#
I am working on something else at the moment; but, will attempt your code sometime a little later.
Ragtime Ragtime

2013/4/1

#
I'll be thankful for that, though I won't be able to post anything for the next couple of days. Have a nice week.
danpost danpost

2013/4/1

#
I found it works if you remove the BufferedWriter from the writefile method and write directly with the FileWriter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void writefile()
{
    FileWriter fw = null;
    try
    {
        fw = new FileWriter("data.txt");
        fw.write(""+speed);
    }
    catch (Exception e){ e.printStackTrace(); }
    finally
    {
        if(fw != null)
        {
            try { fw.close(); }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
}
Ragtime Ragtime

2013/4/1

#
That was it :) thanks.
You need to login to post a reply.