# 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:** 20\
**Page:** 1

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

</div>

Hi everyone.

I receive single words form a microphone input that I will like to save in a text file.  
I manage to save one word in the text file, but when i say a new word the old one get overwritten.

How can I add the new word after the one(s) already been saved?

thanks for help

```auto
String wordFile = "MyText.txt";

void webSocketServerEvent(String msg) {
  println(msg);
  String[] wordFile = split(msg, ' ');
  saveStrings("data/MyText.txt", wordFile);
  loadStrings("data/MyText.txt");
}

```

---

<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, 3:37pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/2 "2018-11-27T15:37:51Z")

</div>

> [@polispace](#):
>
> saveStrings(“data/H-Farm.txt”, wordFile);

I wonder if adding ### to the file name will help  
saveStrings(“data/H-Farm######.txt”, wordFile);

---

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

</div>

@PascalAudet I guess I’m not getting you. Can you please be more specific?

---

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

</div>

I dont think it works like saveFrame() does:

With saveFrame() you can specify the name of the sequence with the **filename** parameter, including hash marks (####), which will be replaced by the current **frameCount** value. (The number of hash marks is used to determine how many digits to include in the file names.)

---

<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, 4:20pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/5 "2018-11-27T16:20:38Z")

</div>

But you can do this if you do not what to overwrite your file

```auto
int name=0;  
void draw()
{
String words = "apple bear cat dog";
String[] list = split(words, ' ');

// Writes the strings to a file, each on a separate line
saveStrings("nouns-"+str(name++)+".txt", list);

}

```

---

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

</div>

> [@PascalAudet](#):
>
> saveStrings(“data/H-Farm######.txt”, wordFile);

Thank you for explaining that, I tried but what he did is creating another file named H-Farm######.txt in the data folder of the sketch and the behavior is the same, it jusr write one word and keep overwriting it.

I try now with the suggestion you give me! Thank you

---

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

</div>

I’m sorry, but is not clear to me in the exemple you give me where exactly do I declare in witch text file the code should save the word(s).

In the way I show below it dose not work.

```auto
int name=0;  

void webSocketServerEvent(String msg) {
  String[] wordFile = split(msg, ' ');
  saveStrings("nouns-"+"MyText"+".txt", wordFile); // Writes the strings to a file, each on a separate line

```

---

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

</div>

> [@polispace](#):
>
> ```auto
> int name=0;  
> 
> void webSocketServerEvent(String msg) {
> String[] wordFile = split(msg, ' ');
> saveStrings("nouns-"+"MyText"+".txt", wordFile); // Writes the strings to a file, each on a separate line
> 
> ```

Like this:

```auto
saveStrings("nouns-"+str(name++)+".txt", wordFile); 

```

this will give you a list of file like:  
nouns-0.txt  
nouns-1.txt  
nouns-2.txt  
nouns-3.txt  
nouns-4.txt  
nouns-5.txt  
nouns-6.txt  
nouns-7.txt  
nouns-8.txt  
nouns-9.txt  
nouns-10.txt

---

<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, 4:46pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/9 "2018-11-27T16:46:39Z")

</div>

Ah wait, maybe i wrote something that was misleading.  
I don’t want to create multiple text files. What I would like to get is only one text file with a list of the words that has been recorder from the microphone.  
sorry about that

---

<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, 4:49pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/10 "2018-11-27T16:49:26Z")

</div>

Something like:  
Funny  
Blue  
Dance  
… and so on…

what happen now with the code is that i save: “Funny” and then when I save “Blue” it overwrite the word funny instead of having something like:  
Funny  
Blue

---

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

</div>

OK in this case try this:  
[https://processing.org/reference/createWriter\_.html](https://processing.org/reference/createWriter_.html)

---

<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, 5:23pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/12 "2018-11-27T17:23:51Z")

</div>

sorry, i think that the point here might be  
-a- first need to read the already existing strings from the file  
-b- then need to extend that array  
-c- then need to add the new word from the new split array one by one  
-d- and write all to the same file again.  
? is that the function what you need?

---

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

</div>

Hi kll, yes you get it right to the point.  
I’m not able to do it by my own as I’m a beginner. Could you please provide me some documentation or  
example on how i could build such a function?  
Thank you

---

<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, 5:30pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/14 "2018-11-27T17:30:03Z")

</div>

like?

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

   lines = loadStrings(filename);

  String[] newlines = split(answer, ' ');
  for ( String l : newlines ) {
    expand(lines); 
    lines = append(lines, l); // add one line for each new word
  }
  saveStrings(filename, lines);

```

sorry raw copy out of my test

if the file not exists it is more tricky

---

<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, 5:32pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/15 "2018-11-27T17:32:55Z")

</div>

yes that definitely looks promising and more suitable for my needs. Thanks a lot.  
I will integrate it in my code and test it. I will let you know the result.

Thanks again for the help!

---

<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, 5:38pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/16 "2018-11-27T17:38:55Z")

</div>

sorry i not know that one, is that able to open existing file for write / 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, 5:43pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/17 "2018-11-27T17:43:57Z")

</div>

one more question where do I declare the variable “lines”?  
now I get the error " the variable lines does not exist"

---

<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, 5:48pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/18 "2018-11-27T17:48:10Z")

</div>

sorry, i edit, check again

---

<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, 5:54pm UTC](https://discourse.processing.org/t/savestring-overwrite-text-in-txt-file/5996/19 "2018-11-27T17:54:34Z")

</div>

but i just read what you actually try to do, the word-cloud thing?  
so you should first loop through the “existing” list and check if the word is there already?  
and only if NOT add it?

[https://processing.org/reference/String\_equals\_.html](https://processing.org/reference/String_equals_.html)

---

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

</div>

Hey, first of all **thank you al lot** for your help, now it works perfect. 🙂

Yes i’m try to do the word-cloud.  
Actually the text at the beginning is empty (no word) then people can speak to the microphone and the word they say will be displayed. The more the same word has been say then bigger it will appear.

I’m editing a code i’ve found online and i’m struggling to understand how it works. Originally it upload a text, he count the words and display the cloud.

I need that he start from 0 words and then start adding them and resize them live, while running

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