How to write to file

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.

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

  save = new String[20];
  writer = createWriter("eggs.txt");
  for(int i = 0; i < eggs.size(); i++){
    Egg onegg = eggs.get(i);
    // String txt = Convert to string
    String txt2;
    if(onegg.hatch.eve == null){
      // txt2 = Convert to string
      // 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.

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:

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


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.

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.

1 Like

-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

that might need more explanation.

i would understand a “not created”

Here’s how to save strings in Android.

Be sure to give permission WRITE_EXTERNAL_STORAGE

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