0

Im currently trying to save some values in a text file in Processing Android (APDE). I want to later use this in another context, so it's important to use a complete file path. From Processing documentation for loadStrings():

... Alternatively, the file maybe be loaded from anywhere on the local computer using an absolute path (something that starts with / on Unix and Linux, or a drive letter on Windows)

So it must be possible. I already searched for a answer, but never found something for Processing. So my code is:

String[] saveData;
int score;

void setup(){
  saveData=loadStrings("/storage/emulated/0/dataP/hi.txt");
  score=parseInt(saveData[0]);
  fullScreen();
  frameRate(60);
  noStroke();
  noSmooth();
  textAlign(CENTER);
  textSize(height/20);
}

void draw(){
  background(0);
  fill(255) ;
  text(score, width/2,height/2);
}

void mousePressed(){
  score--;
  saveData[0]=str(score);
  println(saveData[0]);
  saveStrings("/storage/emulated/0/hi.txt" ,saveData);

}

and I get the following error:

java.lang.IllegalArgumentException: File 
/storage/emulated/0/dataP/hi.txt contains a path separator
vs97
  • 5,765
  • 3
  • 28
  • 41
MrX123
  • 1
  • 1
  • Did you mean to write `dataP` rather than `data`? Are you sure this is a valid file path by the way? – PPartisan Jul 23 '19 at 19:36
  • that is a directory i already created (there already was a `data` folder and I wanted to have one folder specifically for this use) – MrX123 Jul 23 '19 at 19:37
  • and no I'm not sure it's a valid filepath. If that's the case, maybe someone could tell me the right one? – MrX123 Jul 23 '19 at 19:40
  • Sounds like the api method call is only expecting a filename but you are passing a filepath – Blundell Jul 23 '19 at 19:45
  • Possible duplicate of [java.lang.IllegalArgumentException: contains a path separator](https://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-contains-a-path-separator) – Zohaib Amir Jul 23 '19 at 19:50
  • 1
    @Zohaib Amir I already saw this post, but it doesn't help me with my problem in Processing, because their website and some other posts on other websites tell that it's possible to use the loadStrings /saveStrings functions with absolute file paths. – MrX123 Jul 23 '19 at 19:54

2 Answers2

1

I believe the confusion stems from the fact that loadStrings() method works differently for Java mode and Android mode. In Java mode, it is definitely possible to give loadStrings() an absolute Path with included separators, but in Android mode, loadStrings() will only work if you only specify a name without any separator (assumes by default to be looking into the data folder). Therefore, having any separator inside loadStrings() will throw the error.

One simple workaround you can try is to first create a separate path variable:

  String path = "/storage/emulated/0/dataP/hi.txt";

And then give that as parameter to the loadStrings() method:

  saveData = loadStrings(path);

If you were to use an SD card for storage, for example, you could do something like:

  String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();  
  File file = new File(SDCARD + File.separator + "mytext.txt"); 
  String[] s = loadStrings(file.getPath());
vs97
  • 5,765
  • 3
  • 28
  • 41
0

As explained in the link in the comment I posted, loadStrings() and saveStrings() does not take absolute path as argument. What it means is that it can only access files with path "name.txt" and not "folder/name.txt". You have to do it using a FileInputStream and FileOutputStream if you must use absolute path. There are many examples of both these files on StackOverflow.

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29