# Android Mode saving and loading txt file

**URL:** <https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504>\
**Category:** Processing for Android\
**Created:** [November 13, 2018, 5:37pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504 "2018-11-13T17:37:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 13, 2018, 5:37pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/1 "2018-11-13T17:37:07Z")

</div>

I’m having trouble saving and loading .txt files on android mode sketch. I have looked at past discussions about how to resolve the problem but it makes no sense to me.  
What’s the difference in saving and loading .txt files on java mode and android mode? And how do you resolve the problem?

Any info helps @imaginativeCODE.

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 13, 2018, 9:40pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/2 "2018-11-13T21:40:55Z")

</div>

No problem guys I think I found a way to do it.

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 17, 2018, 4:51pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/3 "2018-11-17T16:51:01Z")

</div>

I found that if you put your “example.txt” inside a folder called data and put the data folder inside of your sketch folder then it loads up just fine.

p.s. this works with images and all other things you want to save and load. Also don’t write the “loadStrings” or “loadImage” like this - “loadStrings(“data/Example.txt”)”. The way I found to load and save is like this - “loadStrings(“Example.txt”)”. I don’t know why this works? But it does!

> Here’s some example code below and images

```auto
PImage ExampleImage;
String[] ExampleText = new String[1];

void setup() {
  
  fullScreen();
  
  ExampleImage= loadImage("TestImage.png");
  ExampleText = loadStrings("TestStrings.txt");
  
}

void draw() {
  
  image(ExampleImage, 0, 0, width, height);
  fill(0);
  textSize(33);
  text(ExampleText [0], width/2 - textWidth(ExampleText [0])/2, height/2);
  
}

```

> Inside of sketch folder  
> ![InSketchFolder](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0d12b04568ca540bb520d19e87036cba4577d3f7.png)

> Inside of data folder  
> ![InsideDataFolder](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/abc14cd9cdd52808a72ede1e7c6000b3b9092905.png)

Keep in mind that in future processing updates they might do it differently. I spent lots of time going threw old posts that didn’t work any more. I just had to figure it out by myself. Hope this helps some of you people trying to solve this.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 18, 2018, 3:51am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/4 "2018-11-18T03:51:38Z")

</div>

sorry, i not understand why you say

> Also don’t write the “loadStrings” or “loadImage” like this - “loadStrings(“data/Example.txt”)”.

as i would think it is actually the good way?

```auto
// processing 3.4 / Java Mode
// https://processing.org/reference/loadStrings_.html
// https://processing.org/reference/saveStrings_.html

// pls make a subdir /data/
// and have the file there as
// /data/HalloWorld.txt
// if the file is not found you get that red warning
//
// The file "HalloWorld.txt" is missing or inaccessible, make sure the URL is valid
// or that the file has been added to your sketch and is readable.

// String filename1="HalloWorld.txt";
// you see, no path info given here, so the /data/ dir is assumed as default and works 
// but

String filename1="data/HalloWorld.txt";

// is actually a good way to do it.

// for saving a file we read:
// By default, this file is saved to the sketch's folder.
// but we don't want that there!
// to be more compatible to the loadStrings command
// the example should be changed to also using the /data/ dir.

String filename2="data/HalloWorld_copy.txt";

// funny thing on win7: the copy file is 2 bytes longer.

String[] lines;

// ______________________________________________________
void get_it () {
  println("read from: "+filename1);
  lines = loadStrings(filename1);
  println("there are " + lines.length + " lines in "+filename1);
  for (int i = 0; i < lines.length; i++) {
    println(lines[i]); // println((i+1)+"_ "+lines[i]);
  }
}

// ______________________________________________________
void save_it() {
  println("save to: "+filename2);
  saveStrings(filename2, lines);
}
// ______________________________________________________
void setup() {
  get_it();
  save_it();
}

```

but as i state in the code, i only play processing 3.4 Java mode…  
and not “Processing for Android”

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 18, 2018, 2:09pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/5 "2018-11-18T14:09:22Z")

</div>

Yeah I thought the same but in Processing Android Mode it works this way “loadStrings(“TestStrings.txt”)”, for some reason not sure why? The correct way that we do in Java Mode like this “loadStrings(“data/TestStrings”)”, will give you an error like this.

```auto
FATAL EXCEPTION: Animation Thread
Process: processing.test.examplesketch, PID: 5528
java.lang.IllegalArgumentException: File data/TestImage.png contains a path separator
	at android.app.ContextImpl.makeFilename(ContextImpl.java:1944)
	at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:549)
	at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:193)
	at processing.core.PSurfaceNone.getFileStreamPath(PSurfaceNone.java:317)
	at processing.core.PApplet.sketchPath(PApplet.java:5574)
	at processing.core.PApplet.createInputRaw(PApplet.java:5099)
	at processing.core.PApplet.createInput(PApplet.java:4931)
	at processing.core.PApplet.loadImage(PApplet.java:4026)
	at processing.test.examplesketch.ExampleSketch.setup(ExampleSketch.java:26)
	at processing.core.PApplet.handleDraw(PApplet.java:1846)
	at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)

```

Basically it means that there was a path separator and it couldn’t load the image.

Keep in mind all this that I have only just started getting into Processing Android Mode now. Probably someone more experienced knows away to load up files with path separators. I think it might have something to do with “dataPath”, function call? But don’t trust my “dataPath”, theory.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 18, 2018, 2:23pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/6 "2018-11-18T14:23:37Z")

</div>

> [@imaginativeCODE](#):
>
> java.lang.IllegalArgumentException: File data/TestImage.png contains a path separator

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

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/112190/#Comment_112190)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 18, 2018, 2:25pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/7 "2018-11-18T14:25:21Z")

</div>

Also if you put a folder named TestFolder inside of data and put TestImage inside of TestFolder and run this code “loadImage(“TestFolder/TestImage”)” to load up the image it works just fine. It must have something to do with the fact that all files and folders must be inside of data but don’t bet on it.

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [November 18, 2018, 2:34pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/8 "2018-11-18T14:34:50Z")

</div>

Thanks for the extra info this will help me in future projects.

---

<div class="post-metadata">

**Author:** ![John56](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/john56/32/4192_2.png) [@John56](https://discourse.processing.org/u/John56)\
**Post date:** [March 18, 2019, 9:06pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/9 "2019-03-18T21:06:22Z")

</div>

> [@imaginativeCODE](#):
>
> String ExampleText = new String[1]; void setup() { fullScreen(); ExampleImage= loadImage(“TestImage.png”); ExampleText = loadStrings(“TestStrings.txt”); } void draw() { image(ExampleImage, 0, 0, width, height); fill(0); textSize(33); text(ExampleText [0], width/2 - textWidth(ExampleText [0])/2, height/2); }

Thank you “imaginativeCODE”, this was a very useful information for me. I was unaware of the fact that in android mode absolute paths for data files are not allowed and I have to admit that I still do not really understand the philosophy behind. However, Thanks to you I now know how to create an androide app using input and output files. I presume, saveStrings follows the same way?

---

<div class="post-metadata">

**Author:** ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)\
**Post date:** [March 20, 2019, 6:01pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/11 "2019-03-20T18:01:55Z")

</div>

I have found something out about saving files it works the same as loading but if the file was made before you run “Run on device” it won’t work. More details below.

> loadStrings -  
> 1: loading files made before “Run on device” - works  
> 2: loading files made by the app - works

> saveStrings -  
> 1: saving onto files made before “Run on device” - error  
> 2: saving onto files made by the app - works

> Some code below for you to test run.

```auto
String[] SaveData = new String[1];
String[] LoadData = new String[1];
String[] FileData = {"My text is in here"};
String FileName = "File1.txt";

void setup() {
  fullScreen();
  
  SaveData[0] = "No data saved yet";
  LoadData[0] = "No data loaded yet";
  
}

void draw() {
  background(255);
  
  //Save data section and graphics
  fill(255, 0, 0);
  stroke(0);
  strokeWeight(2);
  rect(0, 0, width, height/2);
  fill(0);
  textSize(25);
  text("Click here to save", width/2 - textWidth("Click here to save")/2, height/4 - 25);
  text(SaveData[0], width/2 - textWidth(SaveData[0])/2, height/4 + 25);
  
  //Save if mouse is pressed below the half of the screen height
  if (mousePressed && mouseY < height/2) {
    //save to the name FileName or make a new txt called FileName inside of the data folder
    
    //comment this out to save the text in the string you set above
    FileData[0] = str(round(random(10)));
    SaveData = FileData;
    saveStrings(FileName, FileData);
    background(0);
    println("Saved");
    
  }
  
  //Load data section and graphics
  fill(0, 255, 0);
  stroke(0);
  strokeWeight(2);
  rect(0, height/2, width, height/2);
  fill(0);
  textSize(25);
  text("Click here to load", width/2 - textWidth("Click here to load")/2, height/2 + height/4 - 25);
  text(LoadData[0], width/2 - textWidth(LoadData[0])/2, height/2 + height/4 + 25);
  
  //Load if mouse is Pressed above half of the screen height
  if (mousePressed && mouseY > height/2) {
    //load the file from the directory of FileName which is inside of the data folder
    LoadData = loadStrings(FileName);
    background(0);
    println("Loaded");
    
  }
  
}

```

> First run the code and see how it works when you understand try this below.  
> Make a folder called data inside the sketch folder.  
> Then make a .txt inside of the folder “data” and name it the same as the “String FileName” that is inside of the code above. Inside of the app load the file first then try and save it.  
> This helps visualize what I am saying for your self.

> I suggest that if you wan’t to load image say for your character to draw in your app this works. But for storing data like where the character is and how much money he’s made, you have to make the file from inside the app like show in the example above. Hope this helps @imaginativeCODE

> If you have any Problems understanding what I just said then let me know so it will help others understand better in the future too.

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 10, 2019, 7:04pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/12 "2019-05-10T19:04:52Z")

</div>

Hi,  
I’m Hans (German), living in France, brand new user, my 1st post.  
Installed on Win 10, processing 3.5.3 using the Android mode and the SDK downloaded through Processing IDE.  
I’m trying to port a Java processing SW running on Raspberry to Android. OK, challenging for a Newby, but I’ve at least “some” programming background…🕶  
Question1:  
switching into Android mode makes the debugger option unavailable; there’s also no “butterfly”. Does this mean, debugging Android APPs must be done in Android Studio?  
Question 2:  
The following code lines seems not to work, although the file exists in the sketch’s data directory.  
static final String CONFIG\_FILE\_VS = “settingsVS.txt”;  
String[] lines = loadStrings(dataFile(CONFIG\_FILE\_VS));  
Does this mean that the dataFile directive doesn’t work in Android mode?  
Rgds  
hk

---

<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:** [May 11, 2019, 9:41am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/13 "2019-05-11T09:41:00Z")

</div>

@lve0200 ===  
-answer 1= yes

-answer 2= yes (but anyway dataPath(CONFIG\_FILE\_VS) should work

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 11, 2019, 9:57am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/14 "2019-05-11T09:57:16Z")

</div>

> Block quote  
> but anyway dataPath(CONFIG\_FILE\_VS) should work

No, it doesn’t " File not found"  
Omitting dataPath (…) is the cure.  
Thanks  
hk

---

<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:** [May 11, 2019, 10:50am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/15 "2019-05-11T10:50:29Z")

</div>

@Ive0200 ===  
your file is probably not in the data folder…

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 13, 2019, 5:43pm UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/16 "2019-05-13T17:43:06Z")

</div>

> [@lve0200](#):
>
> Question 2:  
> The following code lines seems not to work, although the file exists in the sketch’s data directory.

That’s what I wrote in my initial post  
hk

---

<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:** [May 14, 2019, 8:31am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/17 "2019-05-14T08:31:09Z")

</div>

@lve0200 ===  
weird; i have answered without testing but i am quite sure… i ll test!

---

<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:** [May 14, 2019, 10:33am UTC](https://discourse.processing.org/t/android-mode-saving-and-loading-txt-file/5504/18 "2019-05-14T10:33:34Z")

</div>

@lve0200 ====

so i tested and it works

- myfile.txt is in the data folder, nothing more

```auto
void setup(){
 size(800,600); 
 String[] lines = loadStrings(dataPath("myFile.txt"));
 println (lines[1]);
///this println my first item without error
 
}

```
