簡體   English   中英

如何在 Deno 中讀取本地文件?

[英]How do I read a local file in Deno?

我正在編寫一個用 TypeScript 編寫的字數統計程序,我試圖在 Deno 中運行該程序。 我不帶參數調用它,只是deno ./word_count.ts ,所以它應該具有默認的只讀文件系統訪問權限。 我希望我可以使用標准瀏覽器fetch() APIfile: URL 方案從文件系統中讀取,如下所示:

word_count.ts
const countWords = (s: string): number =>
s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const main = async () => {
    const text = await (await fetch("file:///./input.txt")).text();
    const count = countWords(text);
    console.log(`I read ${count} words.`);
};

main();
input.txt
The quick brown fox jumps over the lazy dog.

但是當我嘗試時,我看到fetch不支持file URL:

Error: an error occurred trying to connect: invalid URL, scheme must be http
    at FetchResponse.onMsg (deno/js/fetch.ts:121:21)
    at FetchRequest.onMsg (deno/js/fetch.ts:152:19)
    at onFetchRes (deno/js/fetch.ts:28:8)
    at onMessage$1 (deno/js/main.ts:30:7)

如何在 Deno 中讀取本地文件的內容?

更新:不再需要在async function main() { ... }包裝代碼,因為 Deno 現在支持頂級await

使用內置的Deno.readTextFile

const countWords = (s: string): number =>
  s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const text = await Deno.readTextFile('input.txt');
const count = countWords(text);
console.log(`I read ${count} words.`);

請參閱以下文檔: https : //doc.deno.land/builtin/stable#Deno.readTextFile

使用內置的Deno.readFile

const countWords = (s: string): number =>
  s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const decoder = new TextDecoder('utf-8');

const text = decoder.decode(await Deno.readFile('input.txt'));
const count = countWords(text);
console.log(`I read ${count} words.`);

請注意,您需要將數據顯式解碼為 UTF-8。

請參閱以下文檔: https : //deno.land/typedoc/index.html#readfile

使用阻塞讀取

接受的答案使用readFileSync() ,它是一個阻塞函數,因此不需要asyncmain()更新:非阻塞await也不再需要它)。 一個簡化(和工作)的例子是:

const countWords = (s: string): number =>
  s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const decoder = new TextDecoder('utf-8');

const text = decoder.decode(Deno.readFileSync('input.txt'));
const count = countWords(text);
console.log(`I read ${count} words.`);

請注意,任何地方都沒有await ,因此代碼稍微簡單一些(更新:在 Deno 支持頂級await之前,簡單性的差異更大)但Deno.readFileSync()將阻塞線程,直到讀取文件 - 對於一個執行本示例中的一系列步驟的簡單腳本很好,但是如果它位於服務器中的請求處理程序中,那么對於性能來說將是一場災難。

使用較低級別的Deno.openDeno.readAll

const countWords = (s: string): number =>
  s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const decoder = new TextDecoder('utf-8');

const file = await Deno.open('input.txt');
const text = decoder.decode(await Deno.readAll(file));
const count = countWords(text);
console.log(`I read ${count} words.`);

您可以將main()的前兩行放在一行中:

const text = decoder.decode(await Deno.readAll(await Deno.open('input.txt')));

但它的可讀性會降低。

請參閱以下文檔: https : //deno.land/typedoc/index.html#readall

甚至更低級別的Deno.openDeno.read

你甚至可以使用低級別的Deno.read但你還必須分配緩沖區

請參閱以下文檔: https : //deno.land/typedoc/index.html#read

使用new File()抽象

還有一個用於讀取和寫入文件的類樣式抽象。

請參閱以下文檔: https : //deno.land/typedoc/classes/deno.file.html

過時:這個答案已被去除的壞'deno'贊成的模塊Deno在全球發布v0.3.4

readFileSync(filename: string): Uint8Array在其標准庫中包含一個readFileSync(filename: string): Uint8Array函數,該函數可在特殊導入路徑'deno'

import {readFileSync} from 'deno';

const bytes = readFileSync('./input.txt'); 

這會將文件的內容作為字節數組讀取。 如果文件包含您想要作為字符串的文本,如您的示例中所示,您可以使用標准的 ECMAScript TextDecoder類( MDN 文檔)。

const utf8Decoder = new TextDecoder('utf-8');
const string = utf8Decoder.decode(bytes);

對於原始示例,這給了我們:

word_count.ts
import {readFileSync} from 'deno';

const countWords = (s: string): number =>
s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length;

const main = async () => {
    const decoder = new TextDecoder("utf-8");
    const bytes = readFileSync("./input.txt");
    const text = decoder.decode(bytes);
    const count = countWords(text);
    console.log(`I read ${count} words.`);
};

main();

產生所需的輸出:

$ deno ./word_count.ts
I read 9 words.

標准0.62.0 / 德諾1.2.1 +

由於std v0.62.0readFileStrreadFileStrSync從標准庫中刪除

Deno.readTextFileDeno.readTextFileSync具有相同的 API,現在是要走的路。

(相同的writeFileStr / writeFileStrSync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM