Help needed Saving and Loading .txt files (ANDROID)

TL;DR saving text files with "data/ex" paths gives an error that apparently can be solved with FileInputStream, but that class does not work/exist in processing 3.5.3.

I am using text files for saving an individual top score in a game between sessions.

My solution for this in java is fine, with a simplified version of everything having to do with it below, but it doesn’t work with Android.

My path is declared as:
String path = "data/example.txt";

The file is read like this (excluding my file not found case)

void initializeText() {
  String[] scoreList = new String[1];
  scoreList = loadStrings(path);
  highscore = Integer.valueOf(score[0]);
}

And finally I save the file like this

  if (score>highscore){
    newHigh = true;
    println("New High!");
    highscore = score;
    String[] sclst = {""+score};
    saveStrings(path, sclst);
  }

This works fine in Java mode, but gives me the following error in android:
java.lang.IllegalArgumentException: File data/example.txt contains a path separator

I looked this problem up and found this solution, but it is either for an older version of processing or requires a library I don’t know about, as the commonly referenced FileInputStream does not work in processing 3.5.3

@F53 === there are a lot of errors here; as for android mode and saving things like scores the best way is to use sharedPreferences API - Anyway there are a lot of other solutions, one of them is using FileOutputStream/FileInputStream which of course works with android (what have you tried saying that “it does not work”???) and in all cases you have to use getFilesdir in order to get or save your files (if they are private to your app and not in the ExternalStorageDirectory) - You have to understand that compiling android put your files in a dir (called Files) which has nothing to see at the end with your data folder…Last point: the error you get is a very common one and i have explained 20 times here that a file cannot be called “mypath/anotherfolder/myfile.txt” and opened with openFileInput ()…

As I stated in the original post, FileInputStream/FileOutputStream doesnt seem to work in java, all of the suggested things in the link give me errors of the class not existing

@F53 === You are wrong -FileInputStream or FileoutputStream works with android.

How then, all of the examples in the link don’t compile.
I can’t find documentation on the InputStreams either

@F53 ===Put here thecode you have tried with InputStream and i 'll show you that it works

@akenaton
I just broke up your initial reply to me, sorry for not doing this in the first place:

Noted, I will put research into this tonight
.

I tried this solution https://stackoverflow.com/a/10553772 in my initial link, as well as most of the other answers with examples.
All of the solutions gave me errors like this:

The name "context" cannot be recognized
Error on "mkdir" Consider adding "Identifier"
The class "RandomAccessFileOutputStream" does not exist

.

If android saves and loads to/from a different path than java, how come my current build works with code like this on android:

die = new SoundFile(this, "audio/die.wav");
tutorial = loadImage("tutorial.png");

@F53 ===

  • yes for saving something like a score the best way is SharedPreferences

  • have you imported Context and created an instance of it? If yes how have you done that???

  • you are in fragment, dont forget!

  • the files created (at the end) by P5 are put in the assets folder (or some of its subfolder) - They can be opened but not modified and you cannot use this folder directly ( you cannot get a file from it and its path)- When you open you are using getAssets();

what do you mean imported context? is it getContext();?
.

what do you mean in a fragment?
.
.
I didnt notice your reply until now and I have been playing around with FilePaths, here is my current code that I am experimenting with for getting the FilePath, is this right?
RandomAccessFile HSPath = new RandomAccessFile(new File (getContext().getFilesDir() + "/sc.txt"), "rw");

if that is actually correct, how do I read an (int/string to convert into an int) from this file

what do I do to read out the string?

void initHighscore() {
  highscore = 0;
  try{
    
    HSPath = new RandomAccessFile(getContext().getFilesDir()+"example.txt","rw");
    println(HSPath);
    
  } catch (FileNotFoundException e) { 
    String[] aaa = {"0"};
    saveStrings("example.txt", aaa);
    println("something has gone wrong " + e);
    
    try {
      HSPath = new RandomAccessFile(getContext().getFilesDir()+"example.txt","rw");
    } catch(FileNotFoundException a) {
      println("something has gone wrong " + e);
    }
  }
}

this error means that you have not imported Context or not created an instance of it called “context”
fragment is not an activity but something like a sub-activity; it is created by P5 on runtime by the main activity you cannot see but to which you must refer in a lot of cases. If you want to understand a) export your app as android app b) go to the created folder c) look at src folder; as for the other questions yes, filesDir return a path to your private internal storage but assets folder is not the same… Anyway it s difficult to help you without having the complete code you are using now , with imports and var.

Yeah, sharedpreferences is much easier, this is an example of it’s implementation