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.
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.
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
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.
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");
}
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.