How do I print to a new line in java? - java

boolean valid = false;
String user = txtUser.getText();
String pass = txtPass.getText();
try {
PrintWriter writer = new PrintWriter("src/file");
writer.println("The line");
writer.println(user + "#" + pass);
JOptionPane.showMessageDialog(null,"Sign Up"complete",JOptionPane.INFORMATION_MESSAGE);
writer.close();
} catch(Exception e) {
}
I am making a sign up page and I already have made the login page. The # in the code is used to separate the username to the password. Everything works fine but the problem with this is that every time I sign up it replaces the sign up information I gave the previous time. So if I signed up the first time with the username "greg" and the password "877" it works fine but then if I go on program again and sign up another user with a different username and password, it replaces the first username and pass. I need it to go to new line after every time someone signs up.

Wrap your file with a FileWriter first:
PrintWriter writer = new PrintWriter(new FileWriter("src/file", true));
Here's the description for the constructor of FileWriter(String, boolean):
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
Parameters
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning

you are using public PrintWriter(File file) to write to the file
The javadoc says -
parameter specifies the file to use as the destination of this writer. If the file
exists then it will be truncated to zero size; otherwise, a new file will be created.
The output will be written to the file and is buffered.
So in your case you need to append text to the contents of the existing file so as Luiggi said FileWriter is your friend
FileWriter, a character stream to write characters to file. By default, it will
replace all the existing content with new content, however, when you specified a
true (boolean) value as the second argument in FileWriter constructor, it will keep
the existing content and append the new content in the end of the file.
try in this way
PrintWriter outputFile = new PrintWriter(new FileWriter("src/file", true));

Related

FileInputStream and FileOutputStream: Read and write to the same file

I created a text file with the content "Hello" and I was trying to read these characters from the file and write it back to the same file again.
Assumptions:
1. the file now has the content "Hello" (Overwritten)
2. the file now has the content "HelloHello" (Appended)
3. the file now has the content infinite "Hello" (or an exception gets thrown)
Actual result:
Original "Hello" characters gets deleted from the text file, and the file was left empty.
Actual test
#Test
public void testCopyStream() throws IOException {
File workingDir = new File(System.getProperty("user.dir"));
File testFile = new File(workingDir, "/test.txt");
FileReader fin = new FileReader(testFile);
FileWriter fos = new FileWriter(testFile);
copyStream(fin, fos);
fin.close();
fos.close();
}
I have created the following method for "copying" the data in the InputStream to the OutputStream:
private void copyStream(Reader in, Writer out) throws IOException {
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
}
I tried using debugger to find out the problem, and the debugger shows b = in.read() was assigned -1 at the first iteration of the while loop. Then I executed the code step by step while inspecting the file's content and found that "Hello" keyword got deleted from the file right after statementfinal FileWriter fos = new FileWriter(testFile); gets executed.
I first thought this was due to the InputStream and OutputStream were pointed to the same file so the file gets sort of "locked" by JVM for execution safety?
So I tried swapping those two lines:
FileWriter fos = new FileWriter(testFile);
FileReader fin = new FileReader(testFile);
And the result turned out the same: the file content got eliminated right after the statement FileWriter fos = new FileWriter(testFile);
My questions is: why the content gets cleaned out by FileWriter?. Is this some behavior related to FileDescriptor? Is there a way to read and write to the same file?
Just FYI,
copyStream() method is working fine, I have tested it with other tests.
It's not about using append() method instead of write()
The statement FileWriter fos = new FileWriter(testFile); truncates the existing file.
It does not make sense for you to use streaming access to read and write the same file, as this won't give reliable results. Use RandomAccessFile if you want to read / write the same file: this has calls to seek current position and perform read or writes at different positions of a file.
https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html
FileWriter actually deletes everything in a file before writing. To preserve the text, use
new FileWriter(file, true);
The true parameter is the append parameter of the filewriter. Otherwise it will just overwrite everything

Java create a new file, or, override the existing file

What I want to achieve is to create a file regardless of whether the file exists or not.
I tried using File.createNewFile() but that will only create the file if it does not already exists. Should I use File.delete() and then File.createNewFile()?
Or is there a clearer way of doing it?
FileWriter has a constructor that takes 2 parameters too: The file name and a boolean. The boolean indicates whether to append or overwrite an existing file. Here are two Java FileWriter examples showing that:
Writer fileWriter = new FileWriter("c:\\data\\output.txt", true); //appends to file
Writer fileWriter = new FileWriter("c:\\data\\output.txt", false); //overwrites file
You can use a suitable Writer:
BufferedWriter br = new BufferedWriter(new FileWriter(new File("abc.txt")));
br.write("some text");
It will create a file abc.txt if it doesn't exist. If it does, it will overwrite the file.
You can also open the file in append mode by using another constructor of FileWriter:
BufferedWriter br = new BufferedWriter(new FileWriter(new File("abc.txt"), true));
br.write("some text");
The documentation for above constructor says:
Constructs a FileWriter object given a File object. If the second
argument is true, then bytes will be written to the end of the file
rather than the beginning.
Calling File#createNewFile is safe, assuming the path is valid and you have write permissions on it. If a file already exists with that name, it will just return false:
File f = new File("myfile.txt");
if (f.createNewFile()) {
// If there wasn't a file there beforehand, there is one now.
} else {
// If there was, no harm, no foul
}
// And now you can use it.

how can i add data in to json file in java?

i want to add data inside of an existing json file..
this is my code:
JsonWriterWithGui(){
if(ae.getSource() == btn_submit){
String lname = (String)lbl_name.getText().toString();
String ladd = (String)lbl_add.getText().toString();
String lcontact = (String)lbl_contact.getText().toString();
FileWriter jsonFileWriter = new FileWriter( "E:\\"+tsave+".json");
jsonFileWriter.write(jsonObject.toJSONString());
jsonFileWriter.flush();
jsonFileWriter.close();
this code is already working. but I am trying to update a json file that already exists.
This is a bit of a guess because the problem you're having isn't entirely clear from the question, but if you're finding that your writes are overwriting instead of appending then try changing this line:
FileWriter jsonFileWriter = new FileWriter( "E:\\"+tsave+".json");
...to this:
FileWriter jsonFileWriter = new FileWriter("E:\\"+tsave+".json", true);
You see, FileWriter is backed by FileOutputStream, which takes a boolean argument saying whether new content should be appended or not. This boolean is false by default so if you want to append then you need to explicitly say so.

How to replace the contents of a file after processing

Let's say that I have a TreeMap and I want to take all values for a key, make a file with these, pass the file to another process loop and do this for every map key, so I always have to use the same file but its content must be replaced every time for each key.
What I do is:
PrintWriter writeRatings = new PrintWriter("ratings.txt", "UTF-8");
TreeMap<Integer, ArrayList<Rating>> ratings = new TreeMap<Integer, ArrayList<Rating>>();
-->
for(Integer clID:ratings.keySet()){
ArrayList<Rating> ratingGroup = ratings.get(clID);
for(Rating r:ratingGroup){
witer.println(r.toString());
}
}
writer.flush();
writer.close();
With this, I get a file with all the data for each map key. Can you suggest how can I get only the data from the current key each time in the file?
If you want to use the same file and just append lines to it, use a FileWriter and set the append mode to true
PrintWriter writer = new PrintWriter(new FileWriter("filename", true));
This will append lines to the file, instead of over writing the file. This way, different processes can use the same file and keep on adding new data to the file
So what worked for me was to include this line into the for loop in the position of the arrow in the main question post:
PrintWriter writer = new PrintWriter(new FileWriter("filename"));

FileWriter, How to write on the same document [duplicate]

This question already has answers here:
Java FileWriter with append mode
(4 answers)
Closed 9 years ago.
I have created a Java Game and when the game finishes, a method is executed that tells the user to enter his his/her name then their score will save in playscores.txt document. This is working fine. However, i want the more than just one person's score in this document. I want it so everyone that plays the game name and score will be saved in this document. Would really appreciate some help.
This is my gameComplete Method code:
public void gameComplete() throws IOException {
String name = (String) JOptionPane.showInputDialog(
frame,
"Enter your name: ",
"Save Score",
JOptionPane.PLAIN_MESSAGE);
Score score = new Score(player.getScore(), name);
FileWriter fstream = new FileWriter("playerscores.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Name : " + score.getName() + System.getProperty( "line.separator" ) );
out.write("Score : " + score.getScore());
out.close();
}
I have tried different stuff, such as Objectoutputstream but unfortunately cannot figure out how to do it and was wondering if it is even possible. Furthermore, i would like to know what Class i should be using to get this done.
If you're happy to just append the new score to the end of the file, replace:
FileWriter fstream = new FileWriter("playerscores.txt");
with:
FileWriter fstream = new FileWriter("playerscores.txt", true);
If you can have more than one user playing at the same time, you'll need to use file locking too, to avoid race conditions when accessing the file.
In order to do it for more than one person, you should open the file in append mode.
FileWriter fstream = new FileWriter("playerscores.txt",true);
Syntax: public FileWriter(File file, boolean append)
Parameters:
file - a File object to write to
append - if true, then bytes will be written to the end of the file
rather than the beginning.
By default, the append parameter is false. So , earlier, you were over-writing the score of the previous player with the current one.
Firstly, if you want the file to be added to rather than wiped and written to each time then make sure you add the second argument of true to have it append the text.
You could use a CSV file to store the answers in columns and then read them out parsing the data by using commas.
FileWriter fileW = new FileWriter("playerscores.txt", true);
Hope that helps.

Categories

Resources