# Writing data to assets folder in p5.js sketch

**URL:** <https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709>\
**Category:** Coding Questions\
**Created:** [September 6, 2023, 9:11pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709 "2023-09-06T21:11:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2023, 9:11pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/1 "2023-09-06T21:11:12Z")

</div>

I have created an ‘assets’ folder in my p5.js sketch and enclosed a ‘xxxx.txt’ file. Does anyone know how to write data strings to the .txt file?

---

<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:** [September 6, 2023, 9:42pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/2 "2023-09-06T21:42:39Z")

</div>

1. Load your asset file using [loadStrings()](https://p5js.org/reference/#/p5/saveStrings), which will return a string array.
2. Make whatever changes to that array.
3. When finished, save it back via [saveStrings()](https://p5js.org/reference/#/p5/saveStrings).
4. However, the latter is gonna open a user file dialog!

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2023, 9:48pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/3 "2023-09-06T21:48:31Z")

</div>

I need to be able to write an array of strings repeatedly to the file without opening and editing it. Don’t want to have to use file dialog; just update the file (by code) every time I change the array.

---

<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:** [September 6, 2023, 9:53pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/4 "2023-09-06T21:53:13Z")

</div>

> [@svan](#):
>
> Don’t want to have to use file dialog;

For security reasons browsers don’t allow us to directly create or change local assets!

For that, we can request some server to save our files there via [httpPost()](https://p5js.org/reference/#/p5/httpPost).

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2023, 9:58pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/5 "2023-09-06T21:58:04Z")

</div>

> browsers don’t allow us to directly create or change local assets

Could explain why I’ve been having trouble. Thanks. Is there some public server where anyone can park data?

---

<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:** [September 6, 2023, 10:01pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/6 "2023-09-06T22:01:31Z")

</div>

Unfortunately, I dunno much about client/server communication.

If you find a public server, probably you’re gonna need to write a server side code which would accept your sketch’s requests, I guess.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2023, 10:07pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/7 "2023-09-06T22:07:22Z")

</div>

_Alternate solution:_  
Is there a way to change the save location if I use createWriter(). My mac (Chrome browser) saves the files to my Download folder which makes it more difficult to re-upload them, in addition to the fact that I don’t know how to get the data back into my p5.js sketch without doing it manually.

---

<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:** [September 6, 2023, 10:19pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/8 "2023-09-06T22:19:28Z")

</div>

> [@svan](#):
>
> Is there a way to change the save location…

I don’t think so. Not sure though.

> [@svan](#):
>
> … I don’t know how to get the data back into my p5.js sketch…

You can request the user to do it via [**createFileInput()**](https://p5js.org/reference/#/p5/createFileInput) or [**drop()**](https://p5js.org/reference/#/p5.Element/drop), which will get you a [p5.File](https://p5js.org/reference/#/p5.File).

Apart from that, if you don’t have much to save, you can use [**storeItem()**](https://p5js.org/reference/#/p5/storeItem).

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2023, 10:57pm UTC](https://discourse.processing.org/t/writing-data-to-assets-folder-in-p5-js-sketch/42709/9 "2023-09-06T22:57:02Z")

</div>

Thanks for your help.
