# Saving and Loading .txt files (ANDROID)

**URL:** <https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247>\
**Category:** Processing for Android\
**Created:** [September 27, 2019, 7:55pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247 "2019-09-27T19:55:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [September 27, 2019, 7:55pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/1 "2019-09-27T19:55:07Z")

</div>

**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)

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

```

And finally I save the file like this

```auto
  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

> <https://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-contains-a-path-separator>

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 28, 2019, 11:22am UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/3 "2019-09-28T11:22:43Z")

</div>

@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 ()…

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [September 28, 2019, 4:44pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/4 "2019-09-28T16:44:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 29, 2019, 10:48am UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/5 "2019-09-29T10:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [September 30, 2019, 1:28pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/6 "2019-09-30T13:28:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 30, 2019, 1:33pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/7 "2019-09-30T13:33:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 1, 2019, 5:25pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/10 "2019-10-01T17:25:32Z")

</div>

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

> [@akenaton](#):
>
> for android mode and saving things like scores the best way is to use sharedPreferences API

Noted, I will put research into this tonight  
.

> [@akenaton](#):
>
> what have you tried saying that “it does not work”???

I tried this solution [android - java.lang.IllegalArgumentException: contains a path separator - Stack Overflow](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:

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

```

.

> [@akenaton](#):
>
> 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

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

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

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 1, 2019, 5:34pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/11 "2019-10-01T17:34:51Z")

</div>

@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();

-

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 1, 2019, 5:51pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/12 "2019-10-01T17:51:49Z")

</div>

> [@akenaton](#):
>
> have you imported Context and created an instance of it? If yes how have you done that???

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

> [@akenaton](#):
>
> you are in fragment, dont forget!

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?

```auto
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);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 2, 2019, 9:26am UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/13 "2019-10-02T09:26:35Z")

</div>

> [@F53](#):
>
> The name “context” cannot be recognized

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.

-

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 3, 2019, 10:00pm UTC](https://discourse.processing.org/t/saving-and-loading-txt-files-android/14247/14 "2019-10-03T22:00:59Z")

</div>

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

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/search?Search=SharedPreferences%2B)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.
