!Good!
I'm doing a little test on files in Java.
Try to write words until the user enters "EXIT". Everything that has been written must be saved in a file and created.
It happens that it only writes the last word in the file and ignores the others. I think it's overwriting every word I type. This is the code:
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Parte1 {
public static void main(String[] args) throws IOException {
String palabra;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Introduce una palabra (SALIR para salir): ");
palabra = sc.nextLine();
EscribirFichero(palabra);
}while(!palabra.equals("SALIR"));
System.out.println("Fichero creado");
sc.close();
}//FIN MAIN.
public static void EscribirFichero(String palabra) throws IOException {
FileWriter fichero = null;
BufferedWriter buffer = null;
try {
fichero = new FileWriter("C:/Users/Daniel/Desktop/Parte1.txt");
buffer = new BufferedWriter(fichero);
buffer.write(palabra);
} catch (FileNotFoundException e) {
System.out.println("Archivo no encontrado. Cambia la ruta");
}
if(palabra.equalsIgnoreCase("salir")) {
buffer.close();
}
}
}
The problem is mainly because every time you try to write to the file, you create a new
FileWriter
, this class by the way does not allow "append" mode (if you are not using Java 11) to be able to add more information.In this case, it seems to me that the appropriate thing would be to use BufferedWriter to enable the file in "append" mode and to always be able to add more content to the file.
Example:
In another case you can use FileWriter and enable "append" mode to add information but using Java 11 as indicated by @gbianchi
You're doing almost everything right. It's not like you're overwriting every word. It is that you are simply opening and closing the file for each word. But you never tell it that you are going to add data to the file.
That function by default always starts writing at the beginning of the file. And you want (following your logic) to write at the end. So what you need is another version of FileWriter
The one you need has this signature public FileWriter(File file, boolean append) throws IOException
What you have to do is simply within the WriteFile method, replace the way to open the file by
Beware, this leads to another problem. If the file exists when you start your program, it will write to the end of the file that already existed....
So now you have another problem to solve ;)
Your other problem is the whole function you write. you open a file, you open a buffer to save to the file... but you never flush its contents. According to the documentation, when you do write it happens immediately, but from what you say, it would not be doing that.
I would recommend you to do several things...
Or do you have a function that opens the file, another that closes it, and another that writes only...
Or in the function you have, you always do it
buffer.close();
because after all you are always opening the file.What's more, when the user types exit, you wouldn't even have to enter this function.