mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
added documentation with sample for std/io mod (#6106)
This commit is contained in:
parent
ed5aedc6b4
commit
ee192b0758
1 changed files with 125 additions and 0 deletions
125
std/io/README.md
Normal file
125
std/io/README.md
Normal file
|
@ -0,0 +1,125 @@
|
|||
# std/io
|
||||
|
||||
## Bufio
|
||||
|
||||
**Uses:**
|
||||
|
||||
### readLines
|
||||
|
||||
Read reader[like file], line by line
|
||||
|
||||
```ts title="readLines"
|
||||
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:**
|
||||
|
||||
````text
|
||||
# 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.
|
||||
|
||||
```ts title="readStringDelim"
|
||||
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:**
|
||||
|
||||
````text
|
||||
# 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`.
|
||||
|
||||
```ts
|
||||
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:**
|
||||
|
||||
```text
|
||||
6
|
||||
null
|
||||
abcdef
|
||||
```
|
||||
|
||||
## Writer
|
||||
|
||||
### StringWriter
|
||||
|
||||
Create a `Writer` object for `string`.
|
||||
|
||||
```ts
|
||||
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:**
|
||||
|
||||
```text
|
||||
base0123
|
||||
base0123456789
|
||||
```
|
Loading…
Add table
Reference in a new issue