# How to create an IOBuffer backed by a String

**URL:** <https://discourse.julialang.org/t/how-to-create-an-iobuffer-backed-by-a-string/86657>\
**Category:** General Usage\
**Tags:** strings, io\
**Created:** [September 1, 2022, 7:16pm UTC](https://discourse.julialang.org/t/how-to-create-an-iobuffer-backed-by-a-string/86657 "2022-09-01T19:16:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Little](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ben_little/32/39270_2.png) [@Ben\_Little](https://discourse.julialang.org/u/Ben_Little)\
**Post date:** [September 1, 2022, 7:16pm UTC](https://discourse.julialang.org/t/how-to-create-an-iobuffer-backed-by-a-string/86657/1 "2022-09-01T19:16:05Z")

</div>

Probably unnecessary backstory: I need to read a bunch of files from a zip archive and then pass them to a Threads.@threads for loop. Unfortunately this doesn’t work. My guess is that the datatype created by ZipFile is not thread safe. So I would like to read each file into memory sequentially, create an IOBuffer for each of them, and then iterate over these using the @threads macro.

I can read each file to a String, but that’s where I get stuck. IOBuffer doesn’t seem to have any functions that allow you to create a buffer backed by a string.

Is there any way to create an IOBuffer backed by a String? Along the lines of

```julia
data = "my data"
io = IOBuffer(data) # no such constructor

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 1, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-create-an-iobuffer-backed-by-a-string/86657/2 "2022-09-01T19:30:46Z")

</div>

This code works and is the way you should do it:

```julia
julia> data = "my data"
"my data"

julia> io = IOBuffer(data)
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1)

```

---

<div class="post-metadata">

**Author:** ![Ben\_Little](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ben_little/32/39270_2.png) [@Ben\_Little](https://discourse.julialang.org/u/Ben_Little)\
**Post date:** [September 1, 2022, 8:10pm UTC](https://discourse.julialang.org/t/how-to-create-an-iobuffer-backed-by-a-string/86657/3 "2022-09-01T20:10:12Z")

</div>

Well I’ll be, that works and I cannot reproduce the issue I was having. No idea what was going on there.

EDIT:

Okay I had a typo in my for loop guard

```julia
for (name, data) in datasets

```

where datasets was a Vector and not a Dict. IOBuffer then couldn’t handle the variable `data` because it was a Char and not a String.
