# Modifying a text file in Android

**URL:** <https://discourse.processing.org/t/modifying-a-text-file-in-android/39797>\
**Category:** Processing for Android\
**Created:** [November 20, 2022, 4:00am UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797 "2022-11-20T04:00:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![a\_random\_cat](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@a\_random\_cat](https://discourse.processing.org/u/a_random_cat)\
**Post date:** [November 20, 2022, 4:00am UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/1 "2022-11-20T04:00:52Z")

</div>

HI. Im making an app to control some LEDs, and it works with bluetooth so it needs to be able to allow the user to change the MAC direction of the controller. the thing is that there are 2 problems; When Converting the String lines into a normal string with .toString(); it returns something like this: [Ljava.lang.String;@1b4163d. And the second problem, when I write saveStrings(“mac.txt”, lines);, it doesnt change anything. here is the code:

```auto
PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage img5;
PImage img6;
PImage img7;
PImage img8;
PImage img9;
PImage img10;
PImage img11;
String typing = "";
String saved = "";
boolean sett = false;
import java.io.File;
int si = 0;
int igri = 0;
String elstr="";
boolean keyboard = false;
boolean interm = false;
boolean error = false;
import ketai.net.bluetooth.*;
import android.os.Bundle;
String device_name = "ESP32";
String device_mac = "";
KetaiBluetooth bt;
void setup() {
  String[] lines = loadStrings("mac.txt");
  device_mac = lines.toString();
  img1 = loadImage("img1.png");
  img2 = loadImage("img2.png");
  img3 = loadImage("img3.png");
  img4 = loadImage("img4.png");
  img5 = loadImage("img5.png");
  img6 = loadImage("img6.png");
  img7 = loadImage("img7.png");
  img8 = loadImage("img8.png");
  img9 = loadImage("img9.png");
  img10 = loadImage("img10.png");
  img11 = loadImage("img11.png");
  fullScreen();
  orientation(LANDSCAPE); 
  noStroke();
  fill(0);
  bt = new KetaiBluetooth(this);
  bt.getPairedDeviceNames();
  bt.start();
  bt.connectToDeviceByName(device_name);
  String awewo = lines.toString();
  boolean porque = awewo.contains(":");
  print(lines);
  print(porque);
  print(awewo);
}
/*String str = "igri";
      byte[] data = str.getBytes();
      bt.writeToDeviceName(device_name, data);
      delay(1000);
      background(255,0,0);
      image(led1, 670, 100,130,100);
*/

void draw(){
  if(!sett){
  drawmenu();}
  else{setting();}
  int indent = 25;
  if(mousePressed){
  si = mouseX;
  igri = mouseY;
  print(si," ",igri);
  matense(si,igri);
}
  }
void drawmenu(){
  if(error){
    background(255,0,0);
    text("No hay una direccion MAC puesta!",100,300);
    setting();
  }
  else{
  fill(255,255,255);
  background(75,59,59);
  textSize(60);
text("Control", 70, 70);
text("Paleta de colores", 500, 70);
text("Extras", 1200, 70);
  image(img1,100,80,130,130);
  image(img2,100,230,130,130);
  if(interm){
    image(img7,100,370,130,130);
  image(img8,100,510,130,130);
  }
  else{
  image(img3,100,370,130,130);
  image(img4,100,510,130,130);
  }
  image(img5,1200,80,130,130);
  image(img6,1200,230,130,130);
  image(img9,1200,370,130,130);
  image(img10,1200,510,130,130);
  image(img11,1400,30,100,100);
  fill(255,0,0);
  rect(350,80,130,130);
  fill(0,255,0);
  rect(350,230,130,130);
  fill(0,0,255);
  rect(350,370,130,130);
  fill(255,255,255);
  rect(350,510,130,130);
  //a
  fill(247,105,10);
  rect(490,80,130,130);
  fill(31,240,81);
  rect(490,230,130,130);
  fill(31,85,240);
  rect(490,370,130,130);

  //b
  fill(240,167,31);
  rect(630,80,130,130);
  fill(31,172,240);
  rect(630,230,130,130);
  fill(165,23,116);
  rect(630,370,130,130);
  //c
  fill(255,175,0);
  rect(775,80,130,130);
  fill(8,188,206);
  rect(775,230,130,130);
  fill(232,12,133);
  rect(775,370,130,130);

  //d
  fill(234,255,15);
  rect(915,80,130,130);
  fill(9,107,180);
  rect(915,230,130,130);
  fill(255,3,167);
  rect(915,370,130,130);
}
}

void matense(int x, int y){
  String str = "";
  if(x>=100 && x<=230){
  if(y>=80 && y<=220){str="a";}
  if(y>=230 && y<=360){str="b";}
  if(y>=370 && y<=500){if(interm){str="5";}else{str="c";}}
  if(y>=510 && y<=640){if(interm){str="7";}else{str="d";}}
}
  if(x>=490 && x<=620){
    interm = false;
  if(y>=80 && y<=220){str="e";}
  if(y>=230 && y<=360){str="f";}
  if(y>=370 && y<=500){str="g";}
  if(y>=510 && y<=640){str="h";}
}
  if(x>=630 && x<=760){
    interm = false;
  if(y>=80 && y<=220){str="i";}
  if(y>=230 && y<=360){str="j";}
  if(y>=370 && y<=500){str="k";}
  if(y>=510 && y<=640){str="l";}
}
  if(x>=775 && x<=905){
    interm = false;
  if(y>=80 && y<=220){str="m";}
  if(y>=230 && y<=360){str="n";}
  if(y>=370 && y<=500){str="o";}
  if(y>=510 && y<=640){str="p";}
}
  if(x>=915 && x<=1045){
    interm = false;
  if(y>=80 && y<=220){str="q";}
  if(y>=230 && y<=360){str="r";}
  if(y>=370 && y<=500){str="s";}
  if(y>=510 && y<=640){str="t";}
}
  if(x>=350 && x<=480){
    interm = false;
  if(y>=80 && y<=220){str="v";}
  if(y>=230 && y<=360){str="w";}
  if(y>=370 && y<=500){str="x";}
  if(y>=510 && y<=640){str="y";}
}
  if(x>=1200 && x<=1330){
    interm = true;
  if(y>=80 && y<=220){str="2";}
  if(y>=230 && y<=360){str="3";}
  if(y>=370 && y<=500){str="4";}
  if(y>=510 && y<=640){str="8";}
}
if(x>=1400 && x<=1500){
if(y>=30 && y<=130){
  sett = true;
}}
print(str);

      byte[] data = str.getBytes();
      bt.writeToDeviceName(device_name, data);
      delay(100);
}
void setting(){
  int indent = 25;
  fill(0,0,255);
  rect(230,80,815,560);
  fill(255,255,255);
  fill(255,255,0);
  rect(250,200,50,50);
  fill(0,255,0);
  rect(250,150,50,50);
  fill(255,0,0);
  rect(995,150,50,50);
  text("Configuracion",230,140);
  textSize(40);
  fill(0,255,0);
  text("Introduce la nueva Direccion MAC en \n minuscula para conectarse al ESP32. \n La MAC puede ser vista en el \n menu de la pantalla en 'CON' ",230,470);
  textSize(60);
  fill(255,255,255);
  text("Atras",900,600);
  text("Nueva mac: "+ saved,230,400);
  text("<-- Abrir teclado",300,250);
  text("<-- Borrar Guardar -->",300,200);
  textSize(60);
  text("MAC:" + typing,230,300);
    if(mousePressed){
  si = mouseX;
  igri = mouseY;
}
if(mouseX >= 995 && mouseX <= 1040 && mouseY >= 150 && mouseY <= 200){
  String[] lines = loadStrings("mac.txt");
if(lines.length>1)
  lines[1] = saved;
  saveStrings("mac.txt", lines);
  print("Guardado: "+saved);
}
if (mouseX >= 250 && mouseX <= 300){
  if(mouseY >= 200 && mouseY <= 250){
    if (!keyboard) {
    openKeyboard();
    keyboard = true;   
  }
  }
  if(mouseY >= 150 && mouseY <= 200){
    typing = "";
    saved = "";
    mouseY = mouseY + 100;
  }
}
else {
    closeKeyboard();
    keyboard = false;
  }
if(si>=900&&si<=1045){
  if(igri>=580&&igri<=600){
    sett=false;
  }
}

}

void keyPressed() {
  // If the return key is pressed, save the String and clear it
  if (key == '\n' ) {
    String my_new_str = typing.replace(",", ":");
    saved = my_new_str;
    // A String can be cleared by setting it equal to ""
    typing = ""; 
  } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key; 
  }
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [November 20, 2022, 5:21am UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/2 "2022-11-20T05:21:45Z")

</div>

Hi

Make simple sketch to make sure that you can save and load from folder

---

<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:** [November 20, 2022, 8:09am UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/3 "2022-11-20T08:09:23Z")

</div>

Hi @a_random_cat,

This shows the string representation of your array ([What!?](https://www.geeksforgeeks.org/object-tostring-method-in-java/)) …

> [@a\_random\_cat](#):
>
> `String awewo = lines.toString();`

Use this:

> **[join() / Reference](https://processing.org/reference/join_.html)**
>
> Combines an array of Strings into one String, each separated by the character(s) used for the separator parameter. To join arrays of ints or floats, it's necessary to first convert them to Stri…

Cheers  
— mnse

PS: do it like @jafal suggested. Write small code to test the single steps…  
PPS: And think about refactoring your code …

> **[Code refactoring](https://en.wikipedia.org/wiki/Code_refactoring?wprov=sfla1)**
>
> In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the software (its non-functional attributes), while preserving its functionality. Potential advantages of refactoring may include improved code readability and reduced complexity; these can improve the source code's maintainability and cr...

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 20, 2022, 12:44pm UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/4 "2022-11-20T12:44:12Z")

</div>

Hello @a_random_cat,

This will help us help you:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

Working example to extract string from array:

```auto
String [] lines = loadStrings("mac.txt");

printArray(lines);
println();

String awewo = lines[0].toString();
println(awewo);

println();

```

mac.txt file contents:

```auto
aa:bb:cc:dd:ee
https://www.processing.org
https://discourse.processing.org/t/guidelines-asking-questions/2147
https://stackoverflow.com/help/minimal-reproducible-example
500.7
12

```

Console output on Processing JAVA:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/0/600b1746aed2a30d62753a41714fd6870212d083.png)

References:  
[https://processing.org/reference/loadStrings\_.html](https://processing.org/reference/loadStrings_.html)  
[https://stackoverflow.com/help/minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) \< This is in the Guidelines

`:)`

---

<div class="post-metadata">

**Author:** ![a\_random\_cat](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@a\_random\_cat](https://discourse.processing.org/u/a_random_cat)\
**Post date:** [November 25, 2022, 11:29pm UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/5 "2022-11-25T23:29:38Z")

</div>

As you suggested, I write a simple code to check that I could save files.

```auto
void setup(){
  String[] lines = loadStrings("mac.txt");
  print(lines[0]);
  String words = "jenfokhugihrufgtr";
String[] list = split(words, ' ');

// Writes the strings to a file, each on a separate line
saveStrings("mac.txt", list);
String[] non = loadStrings("mac.txt");
  print(non[0]);
}
void draw(){
}

```

I executed it in java and it worked, but I executed it in android and it didn’t work. Maybe the “data” folder is only read?

---

<div class="post-metadata">

**Author:** ![a\_random\_cat](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@a\_random\_cat](https://discourse.processing.org/u/a_random_cat)\
**Post date:** [November 26, 2022, 12:54am UTC](https://discourse.processing.org/t/modifying-a-text-file-in-android/39797/6 "2022-11-26T00:54:13Z")

</div>

I just did a small research and, yes, the data folder is only read, so is there a way to use the normal phone storage to save my text file there?
