• Create Account

[Java] Problems saving to file.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1Cuajin  Members

155
Like
0Likes
Like

Posted 14 February 2012 - 05:01 AM

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.

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.

#2RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 14 February 2012 - 05:34 AM

Well, it would probably help if you said what the actual output was as well.

#3Rene Z  Members

605
Like
0Likes
Like

Posted 14 February 2012 - 06:14 AM

Scanner.nextLine removes line separators.

#4Cuajin  Members

155
Like
0Likes
Like

Posted 14 February 2012 - 02:14 PM

If Scanner.nextLine removes line separators, what would fix it?

#5rip-off  Moderators

10730
Like
0Likes
Like

Posted 14 February 2012 - 02:29 PM

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


#6Cuajin  Members

155
Like
0Likes
Like

Posted 14 February 2012 - 02:35 PM

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

#7Cuajin  Members

155
Like
0Likes
Like

Posted 14 February 2012 - 02:49 PM

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.

#8rip-off  Moderators

10730
Like
0Likes
Like

Posted 15 February 2012 - 03:43 AM

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: saveToFile

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.

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:~$

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.