Buttons to save and open text file in processing 3

Hello!

That example below should get you started.

For a button see https://www.processing.org/examples/button.html

Additionally to open currentFile in NotePad (in Windows!) :


void openInNotepad() {
  // open in notepad

  try
  {
    ProcessBuilder pb = new ProcessBuilder("notepad.exe", currentFile, "");
    Process p = pb.start();
  }
  catch ( Exception /* IOException, URISyntaxException */ e )
  {
    e.printStackTrace();
  }//catch
}//
//

Regards, Chrisir

Example


// http://forum.processing.org/topic/beginner-s-question-an-application-software-with-source-code-made-with-processing
//
import java.io.File;

// current text 
String input = "";

// file name as String
String currentFile = ""; 

// cursor sign blinks 
boolean cursorBlinkFlag;  // on/off 
int timerCursorBlink;     // timer 

//---------------------------------------------------------------------------

void setup() {
  size(880, 800);
}

void draw() {
  background(111);

  text ("Type something"
    +"                                                           "
    +"Commands: F3 to open; F2 to save changes; F12 to save a new file; F10 New (deletes text); Alt-F4 to quit.", 10, 30 );
  text ("current file : " 
    + textOfCurrentFile(), 10, 50);

  text (input + strBlink(), 10, 70);
}

//---------------------------------------------------------------------------
// Inputs 

void keyPressed() {
  // Is the key CODED? 
  if (key==CODED) {
    // key is CODED --------------------------------------------
    if ( keyCode == java.awt.event.KeyEvent.VK_F3 ) {
      // open 
      load();
    } else if ( keyCode == java.awt.event.KeyEvent.VK_F2  ) {
      // save existing file
      if  ( !currentFile.equals("") ) {
        // save existing file
        saveExistingFileThatHasBeenChanged();
      } else { 
        // no file to save to, 
        // save new File 
        save();
      }
    } else if ( keyCode == java.awt.event.KeyEvent.VK_F10  ) {
      // delete
      input = "";
      currentFile = "";
    } else if ( keyCode == java.awt.event.KeyEvent.VK_F12  ) {
      // save new File 
      save();
    } else 
    {
      // do nothing
    }
    //
  } // if --------------------------------------------
  else  
  { // key is not CODED --------------------------------------------
    if (key==BACKSPACE) {
      // shorten
      if (input.length()>0)
        input=input.substring(0, input.length()-1);
    } else if (key==ESC) {
      // kill Esc
      key=0;
    } else {
      // add key (normal writing) 
      input+=key;
    } // else
    //
  } // else  // key is not CODED -----------------------------------------------
  //
} // func

// ------------------------------------------------------
// Load

void load() {
  File folderToStartFrom = new File( dataPath("") + "//*.*" ); 
  selectInput("Select a file to open", "fileSelectedOpen", folderToStartFrom );
}

void fileSelectedOpen(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    currentFile=selection.getAbsolutePath();
    input = join ( loadStrings( selection.getAbsolutePath() ), "\n" );
  }//else
}

// ---------------------------------------------------------
// Save As.... 

void save() {
  selectOutput("Select a file to write to:", "fileSelectedWrite");
}

void fileSelectedWrite(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    // Do we have a txt at the end? 
    if (selection.getName().length() < 4 || selection.getName().indexOf(".txt") != selection.getName().length()-4 ) {
      // problem missing ".txt"
      currentFile = selection.getAbsolutePath()+".txt"; // very rough approach...
    } else {
      currentFile = selection.getAbsolutePath();
    }//else 
    String[] lines = new String[0];  
    lines = append ( lines, input ); 
    // Writes the strings to a file, each on a separate line
    saveStrings( currentFile, lines);
  }
}
// ---------------------------------------------------------
// Save existing file

void saveExistingFileThatHasBeenChanged() {
  println("saveExistingFileThatHasBeenChanged");
  // Writes the strings to a file, each on a separate line (overwriting)
  String[] lines = new String[0];  
  lines = append ( lines, input ); 
  saveStrings( currentFile, lines );
}

// ---------------------------------------------------------
// Tools

String textOfCurrentFile() {
  if (currentFile.equals(""))
    return "<Not a file>"; 
  else 
  return currentFile;
}

String strBlink() {
  // manage and show the Blinking Cursor |

  // timer to toggle the flag cursorBlinkFlag
  if (millis()-timerCursorBlink > 330) {
    cursorBlinkFlag  = !cursorBlinkFlag; // toggle
    timerCursorBlink = millis(); // set timer
  }//if

  // return the line "|" or nothing 
  if (cursorBlinkFlag) 
    return"|";
  else return"";
}//func 
//
1 Like