# Cannot write file to sketch space

**URL:** https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617
**Category:** Processing for Android
**Created:** [June 19, 2024, 4:36pm UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617 "2024-06-19T16:36:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [June 19, 2024, 4:36pm UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617/1 "2024-06-19T16:36:36Z")

</div>

Android 14 - Processing 4.3 - Set for Android mode.  
The following code sent to tablet Samsung S6 Lite. Reads into the string array but when appended will not write it back.  
I am expecting to write to the sketch’s own space.  
I have tried adding sketchPath and dataPath to no effect.  
I have set and unset the Read/Write to EXTERNAL\_STORAGE to no effect.  
It does not give any errors, just my printout shown on the listing comments.  
Any Help please.  
EDIT: The savedGames.txt is originally in the data folder.

```auto
import java.io.File;
String[] savedGames;

void setup(){
  size(585,585); //785
 
  savedGames = loadStrings("savedGames.txt");
  println(savedGames.length); //This prints 12 for strings in file & array
  
  savedGames=append(savedGames,"test");
  println(savedGames.length); //This prints 13 for strings in array
  
  saveStrings("savedGames.txt", savedGames);
  
  savedGames = loadStrings("savedGames.txt");
  println(savedGames.length); //This prints 12 for strings in file
}

void draw(){

}

```

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 20, 2024, 4:53am UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617/2 "2024-06-20T04:53:50Z")

</div>

Hi @enonod,

In general this is not possible on android, as the data you’ve rolled out to the device, resp. the folder where it is placed is read only. Unfortunately, the lightweight methods like saveStrings do not cause errors to see that.

You have several options, see two of it…

> Load the data (ie with loadStrings) and put it into SharedPreferences see.
> 
> [Save simple data with SharedPreferences &nbsp;|&nbsp; Android Developers](https://developer.android.com/training/data-storage/shared-preferences?hl=en#java)

> Use app specific directory see
> 
> [Access app-specific files &nbsp;|&nbsp; Android Developers](https://developer.android.com/training/data-storage/app-specific?hl=en#internal-access-files)

Hope that helps …

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [June 20, 2024, 6:23am UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617/3 "2024-06-20T06:23:05Z")

</div>

@mnse Thank you for your advice. I read what you pointed to with some difficulty due to small snippets of code which I have interpreted as below. I produced the following amendment and have ‘commented’ the result.  
I have not understood where that location is but it does not matter as long as I can read and write files and they remain there **until** I delete them or I uninstall the sketch.  
I have not understood if dirName now exists.  
I do not see how to save my file at the location if it does exist.  
Can you help with the last step please, creating my data file, saving it etc.  
My attempt produced the error “Read only file system”.

```auto
import java.io.File;
import android.os.Environment;

//Required data storage folder, will exist or be created in external emulated SDCard
String state = Environment.getExternalStorageState();
String dirName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/DataX";
String[] savedGames;

void setup(){
  size(585,585); //785
 println(state); //This prints "mounted"
 println(dirName); //This prints "/storage/emulated/0/DataX"
 
  savedGames = loadStrings("savedGames.txt");
  println(savedGames.length); //This prints 12 for strings in file & array
  
  savedGames=append(savedGames,"test");
  println(savedGames.length); //This prints 13 for strings in array
  
  File f=new File("savedGamesA.txt");
  saveStrings(f, savedGames);
  
  savedGames = loadStrings(f);
  println(savedGames.length); //This prints 12 for strings in file
}

void draw(){

}

```

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 20, 2024, 11:18am UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617/4 "2024-06-20T11:18:42Z")

</div>

Hi @enonod,

not having much time but guess this is enough for you as a starting point…

```auto
import android.os.Environment;
import android.content.Context;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.ArrayList;

void setup() {
  size(585, 585); // 785
  String infilename = "inSavedGames.txt"; // file you roll out with deployment
  String outfilename = "outSavedGames.txt"; // file which can be modified on device

  // Load strings from file
  String[] savedGames = loadStrings(infilename);
  if (savedGames == null) {
    println("Error: Input file not found");
    return;
  }
  // show content of input file
  println("----------------------------");
  println("content of source file :" + infilename);
  for (String s : savedGames) {
    println(s);
  }
  println("Number of lines in input file: " + savedGames.length); // This prints 4 for strings in file & array

  println("----------------------------");
  // write content of input file to device
  String absolutePath = getActivity().getFileStreamPath(outfilename).getAbsolutePath();
  try (PrintWriter p = new PrintWriter(getActivity().openFileOutput(outfilename, Context.MODE_PRIVATE))) {
    for (String s : savedGames) {
      println(String.format("writing (%s) to file: %s", s, absolutePath));
      p.println(s);
    }
  }
  catch (Exception e) {
    println("Error writing: " + e);
  }

  println("----------------------------");
  // read content of file on device
  ArrayList<String> content = new ArrayList<>();
  try (FileInputStream fis = getActivity().openFileInput(outfilename);
  InputStreamReader inputStreamReader = new InputStreamReader(fis);
  BufferedReader reader = new BufferedReader(inputStreamReader)) {

    String line = reader.readLine();
    while (line != null) {
      content.add(line);
      println(String.format("reading (%s) from file: %s", line, absolutePath));
      line = reader.readLine();
    }
  }
  catch (Exception e) {
    println("Error reading: " + e);
  }

  println("----------------------------");
  // show content after reading on device
  println("Number of lines from output file: " + content.size()); // This prints 4 for strings in file & array
  println("content after reading:");
  for (String s : content) {
    println(s);
  }
}

void draw() {
}

```

console output:

```auto
----------------------------
content of source file :inSavedGames.txt
Line1
Line2
Line3
Line4
Number of lines in input file: 4
----------------------------
writing (Line1) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line2) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line3) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line4) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
----------------------------
reading (Line1) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line2) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line3) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line4) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
----------------------------
Number of lines from output file: 4
content after reading:
Line1
Line2
Line3
Line4

```

Hope that helps …

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [June 20, 2024, 1:58pm UTC](https://discourse.processing.org/t/cannot-write-file-to-sketch-space/44617/5 "2024-06-20T13:58:30Z")

</div>

Hello again mnse. I cannot thank you enough after such a long time of attempting to solve this.  
It works perfectly and I must admit, I would _never_ have got there without your kind assistance.  
At last I can convert my original game.  
Thank you for your valuable time.  
Cheers  
enonod
