Followers 0

# [Java] Problems saving to file.

## 7 posts in this topic

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

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.

[CODE]
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() ;
}
[/CODE]

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.
0

##### Share on other sites
Well, it would probably help if you said what the actual output was as well.
0

##### Share on other sites
Scanner.nextLine removes line separators.
0

##### Share on other sites
If Scanner.nextLine removes line separators, what would fix it?
0

##### Share on other sites
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):
[code]

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

##### Share on other sites
[quote name='rip-off' timestamp='1329251342' post='4913102']
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):
[code]

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

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
0

##### Share on other sites
The biggest problem is here I think: [CODE]
toFile.println(data + "\n" + "testing") ;
[/CODE]

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

##### Share on other sites
[quote]
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).

[quote]
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:
[code]

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();
}
}

}
[/code]

Let us create an simple file for Output.txt:
[code]
Existing line 1
Existing line 2
Existing line 3
[/code]

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

Existing line 1
Existing line 2
Existing line 3
Hello, World: <the current date>
[/code]

Then we verify:
[quote]

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.
0

## Create an account

Register a new account