deno/std/io
2020-06-07 09:20:33 -04:00
..
_iotest.ts BREAKING(std): reorganization (#5087) 2020-05-09 08:34:47 -04:00
bufio.ts refactor(std): remove testing dependencies from non-test code (#5838) 2020-06-07 09:20:33 -04:00
bufio_test.ts fix(std/io): StringReader implementation (#6148) 2020-06-06 10:37:52 -04:00
ioutil.ts refactor(std): remove testing dependencies from non-test code (#5838) 2020-06-07 09:20:33 -04:00
ioutil_test.ts fix(std/io): StringReader implementation (#6148) 2020-06-06 10:37:52 -04:00
mod.ts feat: Add missing mod.ts files in std (#3509) 2019-12-20 15:21:30 -05:00
readers.ts fix(std/io): StringReader implementation (#6148) 2020-06-06 10:37:52 -04:00
readers_test.ts feat(std/io): add LimitedReader (#6026) 2020-06-01 18:37:59 -04:00
README.md added documentation with sample for std/io mod (#6106) 2020-06-05 23:43:42 -04:00
util.ts fix(std/io): StringReader implementation (#6148) 2020-06-06 10:37:52 -04:00
util_test.ts BREAKING: reorder std/io/utils copyBytes arguments (#5022) 2020-04-30 16:39:25 -04:00
writers.ts Move encode, decode helpers to /std/encoding/utf8.ts, delete /std/strings/ (#4565) 2020-04-01 15:23:39 -04:00
writers_test.ts BREAKING: remove overload of Deno.test() (#4951) 2020-04-28 12:33:09 +02:00

std/io

Bufio

Uses:

readLines

Read reader[like file], line by line

import { readLines } from "https://deno.land/std/io/mod.ts";
import * as path from "https://deno.land/std/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);
}

Output:

# std/io

## readLines

```ts
import * as path from "https://deno.land/std/path/mod.ts";

## Rest of the file

readStringDelim

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

import { readLines } from "https://deno.land/std/io/mod.ts";
import * as path from "https://deno.land/std/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);
}

Output:

# std/io

## readLines

```ts
import * as path from "https://deno.land/std/path/mod.ts";

## Rest of the file

Reader

StringReader

Create a Reader object for string.

import { StringReader } from "https://deno.land/std/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

Writer

StringWriter

Create a Writer object for string.

import {
  StringWriter,
  StringReader,
  copyN,
} from "https://deno.land/std/io/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 Deno.copy(r, w); // copy all
console.log(w.toString()); // base0123456789

Output:

base0123
base0123456789