[英]How do I read a local file in Deno?
我正在編寫一個用 TypeScript 編寫的字數統計程序,我試圖在 Deno 中運行該程序。 我不帶參數調用它,只是deno ./word_count.ts
,所以它應該具有默認的只讀文件系統訪問權限。 我希望我可以使用標准瀏覽器fetch()
API和file:
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()
,它是一個阻塞函數,因此不需要async
的main()
(更新:非阻塞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.open
和Deno.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.open
和Deno.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.0
, readFileStr
和readFileStrSync
從標准庫中刪除。
Deno.readTextFile
和Deno.readTextFileSync
具有相同的 API,現在是要走的路。
(相同的writeFileStr
/ writeFileStrSync
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.