[Java] Problems saving to file.

Started by
6 comments, last by rip-off 12 years, 2 months ago

String data = "Looking for: " + color + ", has been found. \nItem was moved.\n"


But when I save this using this code below I don't actually save my output formated like I want. I want a new line after color has been found and a new line after item was moved.

Why is it not saving like I want? Please help.


private void saveFile(String data) throws IOException
{
Scanner infile = new Scanner(new File("Output.txt")) ;

String savedD = "" ;

while (infile.hasNext())
{
savedD += infile.nextLine() ;
System.out.println(savedD) ;
}
infile.close() ;

PrintWriter toFile = new PrintWriter("Output.txt") ;

toFile.println(savedD + data) ;

toFile.close() ;
}


Sameple desired output:
Looking for: red, has been found.
Item was moved.
Looking for: blue, has been found.
Item was moved.
Looking for: green, has been found.
Item was moved.
Advertisement
Well, it would probably help if you said what the actual output was as well.
Scanner.nextLine removes line separators.
If Scanner.nextLine removes line separators, what would fix it?
You could add it back in? Alternatively use a form of input that doesn't try to split the data by newline boundaries.

Assuming the printing of the data is for debugging purposes, you could instead write to the file in append mode.

Something like this (untested):


private void saveFile(String data) throws IOException {
PrintWriter writer = new PrintWriter(new FileOutputStream("Output.txt", true));
try {
writer.println(data);
} finally {
writer.close();
}
}

You could add it back in? Alternatively use a form of input that doesn't try to split the data by newline boundaries.

Assuming the printing of the data is for debugging purposes, you could instead write to the file in append mode.

Something like this (untested):


private void saveFile(String data) throws IOException {
PrintWriter writer = new PrintWriter(new FileOutputStream("Output.txt", true));
try {
writer.println(data);
} finally {
writer.close();
}
}



Hello I tried your code and it gave me this error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: saveToFile
The biggest problem is here I think:
toFile.println(data + "\n" + "testing") ;


When I try that the line named testing never jumps to a new line in the txt file.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: saveToFile
[/quote]
As I warned, the source wasn't tested. The given error sounds like you tried to run code that didn't compile. That code (suitably wrapped in a class, with the necessary import statements), compiles fine for me. Perhaps you forgot to import FileOutputStream?

Some IDEs will allow you to run non-compiling code. You shouldn't do this unless you are sure that is what you want (e.g. you have an unfinished class but you want to test something else, perhaps running unit tests against other classes to ensure you aren't breaking them).


When I try that the line named testing never jumps to a new line in the txt file.
[/quote]
You are not giving us enough to go on. You should include the current source you are using, the current state of the file, the expected output and the actual output. You should also make a minimal example, so the current source should just be related to writing stuff to a file - and should work in isolation (i.e. have a main() method). The current state of the file should be simple. The minimal program should write something that is easy to verify to the file - this way the expected output is obvious. Finally, due to the above conditions, the actual output should also be simple.

Consider the following minimal example:



import java.util.Date;

import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

public static void main(String [] args) throws IOException {
Test test = new Test();
String data = "Hello, World: " + new Date();
System.out.println("Going to write " + data + " to the file");
test.saveFile(data);
System.out.println("Done");
}

private void saveFile(String data) throws IOException {
PrintWriter writer = new PrintWriter(new FileOutputStream("Output.txt", true));
try {
writer.println(data);
} finally {
writer.close();
}
}

}


Let us create an simple file for Output.txt:

Existing line 1
Existing line 2
Existing line 3


When finished running the program, the data in the file should be:


Existing line 1
Existing line 2
Existing line 3
Hello, World: <the current date>


Then we verify:


user@host:~$ cat Output.txt
Existing line 1
Existing line 2
Existing line 3
user@host:~$ javac Test.java && java Test
Going to write Hello, World: Wed Feb 15 09:37:15 GMT 2012 to the file
Done
user@host:~$ cat Output.txt
Existing line 1
Existing line 2
Existing line 3
Hello, World: Wed Feb 15 09:37:15 GMT 2012
user@host:~$
[/quote]
Now, if your implementation is behaving differently/oddly, I suggest you follow the same steps. The main thing that these steps do is to eliminate other code as the source of any errors or unexpected behaviour.

This topic is closed to new replies.

Advertisement