# SaveString() overwrite text in .txt file

**URL:** <https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996>\
**Category:** Coding Questions\
**Created:** [November 27, 2018, 3:34pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996 "2018-11-27T15:34:27Z")\
**Posts on this page:** 9\
**Page:** 2

<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 27, 2018, 6:11pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/21 "2018-11-27T18:11:36Z")

</div>

-a- well you can start from a empty file,  
-b- still new words should be checked for have no double  
( i tell you that arrays are not funny, like deleting a double later is tricky )  
-c- but reading a not existing file gives error ( so you could create one )

actually that is a big problem, easy we can exchange code here at the forum  
but as soon files ( and subdir ) are involved the sharing is not so easy  
( i can give you a zip, but would you want download a zip from a unknown source? )

---

<div class="post-metadata">

**Author:** ![PascalAudet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pascalaudet/32/1903_2.png) [@PascalAudet](https://discourse.processing.org/u/PascalAudet)\
**Post date:** [November 27, 2018, 6:13pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/22 "2018-11-27T18:13:45Z")

</div>

> [@kll](#):
>
> append?

No it does not append

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 27, 2018, 6:17pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/23 "2018-11-27T18:17:32Z")

</div>

I think this code is a good challenge for me to learn, I will follow your indication like starting from an empty file.  
I think is ok to have double so that the code can know witch one is the most “used” word otherwise they will be all the same size, no?

Yes reading a empty file give error but i was thinking to solve that problem later…

Yes I would download the zip from you. But I don’t want you to do the code for me I would like to learn how to develop it by my own.

---

<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 27, 2018, 6:33pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/24 "2018-11-27T18:33:59Z")

</div>

actually i think there might come much more functionality to this,  
so i doubt that on long terms you will be happy with that text file.  
a database? how is your SQL?  
processing works great with sqlite3, and same easy about file handling  
like that text file you use now.  
( can not compare with the install and use of MySQL 8 )

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 27, 2018, 6:48pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/25 "2018-11-27T18:48:57Z")

</div>

Well the code I’m trying to develop is for an installation. I’ll use only once for one evening. I’m not an expert and so I trust what you say but creating a DB it seams to me not necessary for the use I will do of it. No?

Moreover I use SQL only once…

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 27, 2018, 7:16pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/26 "2018-11-27T19:16:00Z")

</div>

Not sure if this fits the case, but maybe there’s no need to call `expand` nor `append`. Maybe `concat` is enough? `concat` returns a new array that’s the combination of two arrays.

```auto
  String filename ="data/MyText.txt";
  String[] oldlines = loadStrings(filename); // load old words into a String array

  String answer = "bicycle flower"; // simulated answer
  String[] newlines = split(answer, ' '); // split answer into words (a String array)
  
  saveStrings(filename, concat(oldlines, newlines)); // save both arrays to the file

```

But if you know that the answer only contains one word, then there’s no reason to split it by spaces, and it’s even shorter:

```auto
  String filename ="data/MyText.txt";
  String[] lines = loadStrings(filename);

  String answer = "bicycle"; 
  
  saveStrings(filename, append(lines, answer)); // save the old array + the new word

```

Which just adds one word to the file.

Now you should be able to create a function that adds a word to a file. It would look like this:

```auto
void addWordToFile(String word, String filename) {
  // I leave this as an exercise :)
}

```

You could call it like this:

```
addWordToFile("orangutan", "data/MyText.txt");

```

🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 27, 2018, 7:20pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/27 "2018-11-27T19:20:57Z")

</div>

> [@Save variable in a loop](https://discourse.processing.org/t/save-variable-in-a-loop/4654/4):
>
> /\*\* \* Load & Save Variable (v1.0.1) \* GoToLoop (2018/Oct/20) \* https://Discourse.Processing.org/t/save-variable-in-a-loop/4654/4 \*/ int a; void setup() { size(300, 200); noLoop(); fill(-1); textAlign(CENTER, CENTER); textSize(0100); final String[] aFile = loadStrings("a.txt"); if (aFile != null) a = int(aFile[0]); } void draw() { background((color) random(#000000)); text(a, width\>\>1, height\>\>1); } void mousePressed() { ++a; redraw = true; } @Override void exit()…

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 27, 2018, 9:32pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/28 "2018-11-27T21:32:23Z")

</div>

Hey Hamoid  
thanks for the elegant hint! It work out! 🙂

this is the code

```auto
String wordFile = "data/H-Farm.txt";
String[] msg;

void webSocketServerEvent(String msg) { //listen the input from microphone and add it in the string call msg
  println(msg); // print the line with voice input
  addWordToFile(msg, "data/H-Farm.txt"); // call addWordToFile
}

void addWordToFile(String msg, String wordFile) { // create two strings: msg and wordFile
    String[] oldlines = loadStrings(wordFile); // create a string (empty) call old.. and i fill it with what there's already in the text file
    String answer = msg ; // not clear what this line does.. 
    String[] newlines = split(msg, ' '); // create string and fill it with string "msg" splitted every empty space
    saveStrings(wordFile, concat(oldlines, newlines)); //save all 
}

```

I don’t understand why I am creating so many strings (msg, wordFile, oldlines, answer, newlines) those are all string right?  
I comment each line with what i guess the code does, would you mind please to check my comment and see if they are right?  
thank you a lot  
as well for the exercise 😉

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 27, 2018, 10:02pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/29 "2018-11-27T22:02:32Z")

</div>

Hi 🙂 Good that it works 🙂

You have indeed a few extra variables.

- You could delete the first two lines, because you are not using them anywhere. They are global variables, and they get overwritten by local variables with the same names.
- After `void addWordToFile` I would not write “create two strings”. They are actually [arguments / parameters](https://funprogramming.org/31-Function-parameters-and-return-values.html) for the `addWordToFile` function. The values of those two variables are received from outside, from wherever you are calling the function.
- You can also delete `String answer = msg;` since you are not using `answer` anywhere.

@GoToLoop in the guidelines it recommends not to post links or code without explanations 🙂

[Previous page](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996.md?page=1)
