std/io
2022-10-10 10:07:24 -04:00
..
testdata feat(io/bufio): add encoding options to readLines and readStringDelim (#921) 2021-05-18 12:39:02 +09:00
_iotest.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
buffer_test.ts chore: Avoid using rid-based filesystem functions (#2763) 2022-10-10 10:07:24 -04:00
buffer.ts refactor(io): remove obsolete check in readLine (#2574) 2022-08-30 13:00:15 +09:00
files_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
files.ts docs: Using absolute paths in jsdoc import statements (#2762) 2022-10-10 10:05:56 -04:00
mod.ts chore(io): remove deprecated APIs (#2713) 2022-09-29 11:29:34 -04:00
readers_test.ts BREAKING(io/readers): use an array as a MultiReader constructor parameter to avoid Maximum call stack size exceeded (#2016) 2022-04-08 15:03:51 +09:00
readers.ts refactor: replace TS private with private identifiers (#2272) 2022-05-27 21:27:13 +09:00
README.md docs(io): remove title attribute (#2496) 2022-08-04 17:29:02 +09:00
test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
types.d.ts docs: Using absolute paths in jsdoc import statements (#2762) 2022-10-10 10:05:56 -04:00
util_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
util.ts chore(io): remove deprecated APIs (#2713) 2022-09-29 11:29:34 -04:00
writers_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
writers.ts fix(io): StringWriter retaining references after write (#2456) 2022-07-25 15:21:13 +09:00

std/io

readLines

Read reader[like file], line by line:

import { readLines } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readLines(fileReader)) {
  console.log(line);
}

readStringDelim

Read reader[like file] chunk by chunk, splitting based on delimiter.

import { readStringDelim } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readStringDelim(fileReader, "\n")) {
  console.log(line);
}

StringReader

Create a Reader object for string.

import { StringReader } from "https://deno.land/std@$STD_VERSION/io/mod.ts";

const data = new Uint8Array(6);
const r = new StringReader("abcdef");
const res0 = await r.read(data);
const res1 = await r.read(new Uint8Array(6));

// Number of bytes read
console.log(res0); // 6
console.log(res1); // null, no byte left to read. EOL

// text

console.log(new TextDecoder().decode(data)); // abcdef

Output:

6
null
abcdef

StringWriter

Create a Writer object for string.

import {
  copyN,
  StringReader,
  StringWriter,
} from "https://deno.land/std@$STD_VERSION/io/mod.ts";
import { copy } from "https://deno.land/std@$STD_VERSION/streams/mod.ts";

const w = new StringWriter("base");
const r = new StringReader("0123456789");
await copyN(r, w, 4); // copy 4 bytes

// Number of bytes read
console.log(w.toString()); //base0123

await copy(r, w); // copy all
console.log(w.toString()); // base0123456789

Output:

base0123
base0123456789