0

hello everyone im trying to save data in the notepad but i dont know how to save many lines. with this code i just can save once the data.

package Vista;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class notepad_Data {

    public void escribir(String nombreArchivo) { 

            File f; 
            f = new File("save_data");                    
                  try {            
                    FileWriter w = new FileWriter(f);
                    BufferedWriter bw = new BufferedWriter(w);
                    PrintWriter wr = new PrintWriter(bw);
                    wr.append(nombreArchivo+" ");               
                    bw.close();

                } catch (IOException e) {
                };                

    }
    public static void main(String[] args){    
        notepad_Data obj = new notepad_Data();
        obj.escribir("writing in the notepad");
    }
}

i tried with this code in the escribir method but doesnt work

 for(int i=0; i<1000; ++i){           
                  try {            
                    FileWriter w = new FileWriter(f);
                    BufferedWriter bw = new BufferedWriter(w);
                    PrintWriter wr = new PrintWriter(bw);
                    wr.append(nombreArchivo+" ");               
                    bw.close();

                } catch (IOException e) {
                };
              }
  • Possible duplicate of [How to append text to an existing file in Java](https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Michael Markidis May 31 '17 at 03:26
  • 1
    Never write an empty catch clause `catch (Exception ex) { }`; if an exception occurs you won't ever know. At least put `ex.printStackTrace();` inside the empty braces so you will see if an exception happened. – Kevin Anderson May 31 '17 at 03:26

2 Answers2

1

Every time you execute the program, you create a new sava_data file that replaces the previous file with the same name, so your new content is not added.

public class Notepad_Data {
 public void escribir(String nombreArchivo) {
     FileWriter fw = null;
     try{
         File f = new File("save_data");
         fw = new FileWriter(f, true);
     }catch(Exception e){
         e.printStackTrace();
     }

     PrintWriter pw = new PrintWriter(fw);
     pw.println(nombreArchivo);
     pw.flush();

     try{
         fw.flush();
         pw.close();
         fw.close();

     }catch(Exception e){
         e.printStackTrace();
     }

 }
 public static void main(String[] args){    
     Notepad_Data obj = new Notepad_Data();
     obj.escribir("writing in the notepad11");
 }

}

G.y
  • 31
  • 3
  • This solution works. However, I recommend not closing the PrintWriter every time you want to write to the file. Instead, leave it open until you are done with it then close it once. – WIR3D May 31 '17 at 03:34
  • As it's written now, If an Exception is thrown while closing the PrintWriter, the FileWriter will not get closed. – Michael Markidis May 31 '17 at 03:36
  • I totally agree with your opinion – G.y May 31 '17 at 03:38
0

You should place your wr.append inside the loop using the arrayList.size as your condition, what you did here is you placed the whole block inside a for loop, which is not a good idea, one reason is you keep on creating an object of those 3 classes: FileWriter, BufferedWriter and PrintWriter, which is a potential java.lang.OutOfMemoryError: Java heap space error.

Anthony
  • 77
  • 1
  • 10