std/io
2022-05-04 19:34:37 +09: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: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
buffer.ts chore: Enable "noImplicitOverride" compiler option and fix errors (#1940) 2022-02-20 17:35:40 +01:00
bufio.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
files_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
files.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
ioutil.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
mod.ts docs: add module level docs (#2190) 2022-05-04 19:34:37 +09: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 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
README.md feat: streams utilities (#1141) 2021-10-12 11:51:48 +02:00
streams.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
types.d.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
util_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
util.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
writers_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
writers.ts refactor: mark modules as browser compatible (#1972) 2022-03-01 13:25:50 +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