# How to write to file

**URL:** <https://discourse.processing.org/t/how-to-write-to-file/16096>\
**Category:** Processing for Android\
**Created:** [December 4, 2019, 2:17am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096 "2019-12-04T02:17:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asra](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Asra](https://discourse.processing.org/u/Asra)\
**Post date:** [December 4, 2019, 2:17am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096/1 "2019-12-04T02:17:16Z")

</div>

I have two ArrayLists and I am writing them to a file, though it is not working and the file is not changed. here is my code for writing the list to a file.

```auto
void save(){
  String[] save = new String[10];
  for(int i = 0; i < creatures.size(); i++){
    Creature oncreature = creatures.get(i);
    println(i);
    String txt;
    if(oncreature.eve == null){
      // txt = Convert to string
    }
    else{
      // txt = Convert to string
    }
    println(txt);
    save[i] = txt;
  }
  saveStrings("creatures.txt", save);

  save = new String[20];
  writer = createWriter("eggs.txt");
  for(int i = 0; i < eggs.size(); i++){
    println(i);
    Egg onegg = eggs.get(i);
    // String txt = Convert to string
    String txt2;
    writer.println(txt);
    if(onegg.hatch.eve == null){
      // txt2 = Convert to string
    }
    else{
      // txt2 = Convert to string
    }
    save[i*2] = txt;
    save[(i*2)+1] = txt2;

  }
  saveStrings("eggs.txt", save);

  
}

```

I do not know how to fix this, any help would be appreciated.

---

<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:** [December 4, 2019, 2:51am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096/2 "2019-12-04T02:51:21Z")

</div>

hi Asra,

your code might make sense for your project,  
but please understand that you should post code here what is complete,can run,  
and is just about the problem.

so i try:

```auto
String outfile = "data/text.txt";
String[] vals = {"line1","line2"};

saveStrings(outfile,vals);

```

from the PDE under windows 10 / processing 3.5.3  
and the directory “data” was created automatically and the file with 2 lines saved.

so, now you try that 3 lines in your Android environment, i can NOT answer about that.

---

<div class="post-metadata">

**Author:** ![Asra](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Asra](https://discourse.processing.org/u/Asra)\
**Post date:** [December 4, 2019, 2:59am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096/3 "2019-12-04T02:59:39Z")

</div>

> [@kll](#):
>
> String outfile = “data/text.txt”; String vals = {“line1”,“line2”}; saveStrings(outfile,vals);

I Tried that exact code in a new project, but the file did not change. For more information, I am loading this on my android phone, which may have something to do with it.

---

<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:** [December 4, 2019, 5:59am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096/4 "2019-12-04T05:59:35Z")

</div>

-a- as i say, i NOT talk about Android,

-b- did you search the forum here already, because  
file write Android seems to be a often question  
possibly not covered by a good example / tutorial  
BUT many times solved / explained here by the experts.

-c- now you use two times the wording

> [@Asra](#):
>
> the file is not changed

that might need more explanation.

i would understand a “not created”

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 4, 2019, 8:16am UTC](https://discourse.processing.org/t/how-to-write-to-file/16096/5 "2019-12-04T08:16:25Z")

</div>

Here’s how to save strings in Android.

Be sure to give permission WRITE\_EXTERNAL\_STORAGE

```auto
import android.os.Environment;

void setup() {
  String[] holder = new String[3]; 
  holder[0] = "Just";
  holder[1] = "an";
  holder[2] = "example";
  // Folowing file name is the folder you save your text file in.
  // If it does not exist it will be created automatically
  String stringFolder = "MyStrings";
  // Folowing string is the name of your text file
  // Be sure to include extension.
  String textFile = "MyText.txt";
  saveMyText(stringFolder, textFile, holder);
}

void saveMyText(String sf, String tf, String[] sh) { 
  try { 
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 
    File file = new File(absolute_path+"/"+sf); 
    println(file);
    if (!file.exists()) { 
      boolean success = true; 
      success = file.mkdirs();
    } 
    saveStrings(file+"/"+tf, sh);
    println("File saved successfully.");
  } 
  catch (Exception e) { 
    println("Error while saving file: " + e);
  }
}  

```
