[help] method to efficiently append a new line to a text file

oatoat
edited March 2015 in Programming Questions

I have the following function to append a new line to an existing text file. But it doesn't seem to be efficient as it re-reads and re-writes every line each time I add a new line, especially when I need to append a lot of new lines. Can you advise an efficient way to do this? Thank you!

// general function of appending  a new line of text to an existing text file
void appendNewLine (String _fileName, String _newLine) {
  // http://forum.processing.org/two/discussion/comment/33698

  // method B:
  // text file can be put in any folder relative to the sketch folder

  String[] lines = loadStrings(_fileName);

  // create a copy of the existing file
  PrintWriter output = createWriter(_fileName);

  // write the contents of the existing file to the new one
  for (int i = 0; i < lines.length; i++) {
    output.println(lines[i]); 
  }

  // append new contents to the new file
  //output.println("\n"); 

  output.println(_newLine);
  //output.flush(); // might be redundant
  output.close();
}

Answers

  • edited March 2015

    You should look at PrintWriter class definition and instantiate 1 by yourself w/o autoFlush:
    https://docs.oracle.com/javase/8/docs/api/index.html

    And yes, flush() method is redundant when it's just before close() b/c close() calls flush()! :-\"

  • Answer ✓

    Use the search, Luke: https://www.google.fr/search?as_sitesearch=processing.org&as_q=append+to+text+file

    (simple query from the main site...)

  • oatoat
    edited March 2015

    Thanks, GoToLoop and PhiLho!

    The following code is based on your early post on the same question.

    However, I couldn't get it work when I call the appendNewLine() function from places other than inside the fileSelected() function

    It seems while the program is waiting for input for file selection, the process has moved forward to the appendNewLine() function which doesn't have the inputs yet ...

    What I want is that once the file is selected, I can add new lines to it anytime I want later.

    Can you kindly tell me what I'm doing wrong over here?

    Thanks!

    // reference:
    // http://forum.processing.org/two/discussion/561/easiest-way-to-append-to-a-file-in-processing
    
    import java.io.*;
    
    String filePathName = "";
    String newLine;
    
    BufferedWriter bw = null;
    
    void setup()
    {
      selectInput("select a file", "fileSelected");
      newLine = "test ...";  
      appendNewLine(filePathName, newLine);
    }
    
    void draw() {} 
    
    void fileSelected (File selection) {
      if (selection == null) {
        println("no file is selected");
      } else {
        filePathName = selection.getAbsolutePath();
        println("file " + filePathName + " is selected.");
        //appendNewLine(filePathName, newLine);
      }
    }
    
    void appendNewLine (String filePathName, String newLine) 
    {
      // method 3 ----------------------------------------
      try {
        FileWriter fw = new FileWriter(filePathName, true);
        bw = new BufferedWriter(fw);
        bw.write(newLine);
      } 
      catch (IOException e) {
        println("D'oh...!");
        e.printStackTrace();    
      } 
      finally {
        if (bw != null) {
          try { bw.close(); } 
          catch (IOException e) { println("Error while closing the writer"); }
        }    
      }  
    
    }
    
  • edited March 2015
    // forum.processing.org/two/discussion/9664/
    // help-method-to-efficiently-append-a-new-line-to-a-text-file
    
    File f;
    
    void setup() {
      size(800, 200, JAVA2D);
      smooth(4);
      frameRate(2);
    
      fill(-1);
      textSize(18);
      textAlign(CENTER, CENTER);
    }
    
    void draw() {
      background((color)random(#000000));
    
      text(f == null? "Awaiting for user to pick file" : f.toString()
        , width>>1, height>>1);
    }
    
    void mouseClicked() {
      selectInput("Select File", "getFile");
    }
    
    void getFile(File selection) {
      println(selection == null
        ? "Window was closed or the user hit cancel."
        : "User selected: \"" + (f = selection) + "\"");
    }
    
  • oatoat
    edited March 2015

    Thanks, GoToLoop!

    I found out that selectInput() will run on a separate thread which might be causing the problem.

    The following also put the appendNewLine() function inside an event triggered function

    // reference:
    // http://forum.processing.org/two/discussion/561/easiest-way-to-append-to-a-file-in-processing
    
    import java.io.*;
    
    String filePathName;
    String newLine;
    
    BufferedWriter bw = null;
    
    Boolean newDataLoaded = false;
    
    void setup()
    {
      //filePathName = dataPath("test.idf");
      newLine = "test ...";  
    }
    
    void draw() {} 
    
    void keyPressed () {
      if ( key == 'f' ) {
        selectInput("select a file", "doSthToFile");
      }
    }
    
    void doSthToFile (File selection) {
      if (selection == null) {
        println("no file is selected");
      } else {
        filePathName = selection.getAbsolutePath();
        println("file " + filePathName + " is selected.");
        appendNewLine(filePathName, newLine);
      }
    
      newDataLoaded = true;
    }
    
    void appendNewLine (String filePathName, String newLine) 
    {
      if ( filePathName != null ) {
        // method 3 ----------------------------------------
        try {
          FileWriter fw = new FileWriter(filePathName, true);
          bw = new BufferedWriter(fw);
          bw.write(newLine);
        } 
        catch (IOException e) {
          println("D'oh...!");
          e.printStackTrace();    
        } 
        finally {
          if (bw != null) {
            try { bw.close(); } 
            catch (IOException e) { println("Error while closing the writer"); }
          }    
        }
      }  
    
    }
    
Sign In or Register to comment.