Files & data
Reading and writing plain text, bytes, JSON, XML, and CSV — all on Sketch, mirroring Processing's load*/save* globals.
On this page · 13 entries
LoadStrings · SaveStringsRequestText · RequestStrings · RequestBytes · RequestJSONObject · RequestJSONArray · RequestXML · RequestTable · RequestPImage · RequestGeoJSONLoadBytes · SaveBytesLoadJSONObject · SaveJSONObject · LoadJSONArray · SaveJSONArrayParseJSONObject · ParseJSONArray · ParseXMLLoadXML · SaveXMLLoadTable · SaveTableCreateWriter · CreateReaderCreateOutput · CreateInputSaveStreamclass PrintWriter · class BufferedReaderPrintArray- Runnable example
LoadStrings · SaveStrings
LoadStrings(string path) → string[]SaveStrings(path, lines)
Reads/writes a text file as an array of lines.
Every Load*() on this page (and LoadImage) also takes a URL, like in Processing: LoadStrings("https://...") downloads it and returns the lines. It waits for the download, so do it in Setup() — or use the Request*() versions below.
RequestText · RequestStrings · RequestBytes · RequestJSONObject · RequestJSONArray · RequestXML · RequestTable · RequestPImage · RequestGeoJSON
RequestText(pathOrUrl, cacheFor)RequestStringsRequestBytesRequestJSONObjectRequestJSONArrayRequestXMLRequestTableRequestPImageRequestGeoJSON → Request<T>
Loads in the background and returns at once, so Draw() never waits for the network. Check .Ready before using .Value; .Progress (0..1, or -1 when the server doesn't say the size), .BytesRead and .Status ("loading 40%", "from cache", "failed: HTTP 404") are there for a loading screen, .Failed/.Error when it can't. Pass cacheFor: TimeSpan.FromDays(30) to keep the download on disk: the next run reads it instantly, and if the network is down later, the older copy is used (.Stale says so). Every free API in public-apis without a key works this way.
LoadBytes · SaveBytes
LoadBytes(string path) → byte[]SaveBytes(path, data)
Reads/writes a file's raw bytes.
LoadJSONObject · SaveJSONObject · LoadJSONArray · SaveJSONArray
LoadJSONObject(path) → JSONObjectSaveJSONObject(json, path)LoadJSONArray(path) → JSONArraySaveJSONArray(json, path)
Reads/writes JSON files. JSONObject/JSONArray wrap System.Text.Json with Processing's Get*/Set*/Append* naming (GetString, GetInt, GetFloat, GetBoolean, GetJSONObject, GetJSONArray, and their Set*/Append* counterparts).
ParseJSONObject · ParseJSONArray · ParseXML
ParseJSONObject(string json) → JSONObjectParseJSONArray(json) → JSONArrayParseXML(string xml) → XML
Parses an already-in-memory string instead of reading from a file — use these instead of LoadJSONObject/LoadJSONArray/LoadXML when the data came from somewhere other than disk: a network response, a string built by hand, text pasted into the sketch.
LoadXML · SaveXML
LoadXML(string path) → XMLSaveXML(xml, path)
Reads/writes an XML file. XML wraps System.Xml.Linq with GetString/GetInt/GetFloat attribute accessors, GetContent() for text, and GetChildren()/GetChild(name)/AddChild(name) for structure.
LoadTable · SaveTable
LoadTable(path, options = "") → TableSaveTable(table, path)
Reads/writes a CSV file as a Table. Pass "header" for a first line naming the columns. Cell values are stored as text and parsed on demand via TableRow.GetInt/GetFloat/GetString, by index or column name.
Table also reads TSV and its own binary .tbl format; only CSV is implemented here. The parser handles quoted fields with embedded commas, but not escaped double-quotes inside a quoted field — enough for CSV a spreadsheet actually produces, not a full RFC 4180 implementation.CreateWriter · CreateReader
CreateWriter(string path) → PrintWriterCreateReader(path) → BufferedReader
Incremental text I/O — for a long-running log file, or a large input read a line at a time — where LoadStrings/SaveStrings already cover the one-shot case. CreateWriter creates the containing directory if needed. Remember to Flush()/Close() the PrintWriter when done.
CreateOutput · CreateInput
CreateOutput(string path)CreateInput(path) → Stream
Raw byte streams to/from a file, like Processing's createOutput()/createInput(). The caller owns the returned Stream and should dispose it when finished.
SaveStream
SaveStream(string path) → StreamSaveStream(path, Stream input)
The single-argument overload is an alias for CreateOutput(), under Processing's other name for the same thing. The two-argument overload copies the entire contents of input to a new file at path and closes both — handy when you already have an input stream (from CreateInput() on another file, say) and just want it saved whole.
class PrintWriter · class BufferedReader
class PrintWriterclass BufferedReader
PrintWriter exposes Print(value)/Println(value?)/Flush()/Close(). BufferedReader exposes ReadLine(), which returns null once the file is exhausted.
PrintArray
PrintArray(Array array)
Prints every element on its own line, prefixed with its index in square brackets. Works with any array type, matching Processing's own printArray() (which needs a separate overload per type).
Runnable example
A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.
// Files & data: JSON in memory, then saved and loaded back from disk. public class Data : Sketch { JSONArray scores; public override void Setup() { Size(500, 300); scores = ParseJSONArray("[{\"name\":\"Ana\",\"score\":42},{\"name\":\"Leo\",\"score\":77},{\"name\":\"Mia\",\"score\":63}]"); var best = new JSONObject(); best.SetString("name", "Leo"); best.SetInt("score", 77); SaveJSONObject(best, "best.json"); // next to the sketch Println("Saved: " + LoadJSONObject("best.json").ToString()); SaveStrings("names.txt", new[] { "Ana", "Leo", "Mia" }); Println(string.Join(", ", LoadStrings("names.txt"))); } public override void Draw() { Background(250); for (int i = 0; i < scores.Size(); i++) { var row = scores.GetJSONObject(i); float w = row.GetInt("score") * 4; Fill(90, 140, 220); NoStroke(); Rect(100, 40 + i * 70, w, 40); Fill(30); Text(row.GetString("name"), 20, 66 + i * 70); Text(Str(row.GetInt("score")), 110 + w, 66 + i * 70); } } }