How to replace something in the data folder in the code?

String[] lines; int[] linesint; void setup() { size(500,500); String[] line = loadStrings("list.txt"); lines = line; linesint = int(split(line[0], ENTER)); } void draw() { background(255); fill(0); for(int i = 0; i<lines.length; i++) { text(lines[i],(i*50)+50,50); } } void mouseClicked() { if (mouseY<450) { linesint[0]++; lines[0] = ""+linesint[0]; } if (mouseY>450) { saveStrings("list.txt", lines); } } I'm testing out using text files and I ran into a problem, that being when I run saveStrings() it doesnt place the file in the data folder. This means that it doesnt use the saved file next time I run the program. I want to make a kind of save system being that you use a txt file to save all the variables, but that cant happen when it makes an entirely new txt file every time. Basically, how do you replace files in the data folders?

Tagged:

Answers

  • Answer ✓

    saveStrings("data/list.txt", lines); ?

  • My attempt below.

    Kf

    String[] lines;
    int[] linesint;
    void setup() {
      size(500, 500);
      String folder=dataPath("");
      String fn=folder+"list.txt";
    
      lines = loadStrings(fn);
    
      //Next creates the file and loads it in case file doesn't exist
      while (lines==null) {
        createFile(fn);
        lines = loadStrings(fn);
        delay(200);
      }
    
      if (lines.length>0) {  
        linesint = int(split(lines[0], ENTER));
      }
      else{
         exit();   //FATAL failure
      }
    }
    void draw() {
      background(255);
      fill(0);
      for (int i = 0; i<lines.length; i++) {
        text(lines[i], (i*50)+50, 50);
      }
    }
    void mouseClicked() {
      if (mouseY<450) {
        linesint[0]++;
        lines[0] = ""+linesint[0];
      } 
      if (mouseY>450) {
        saveStrings("list.txt", lines);
      }
    }
    
    
    void createFile(String aFileName) {
    
      //REFERENCE: https://processing.org/reference/saveStrings_.html
      String numbers = "1 2 3 4";
      String[] list = split(numbers, ' ');
    
      // Writes the strings to a file, each on a separate line
      saveStrings(aFileName, list);
    }
    
Sign In or Register to comment.