在這個程序中,每個命令行參數(shù)都是一個文件名,參數(shù)對應的文件將被依次打開,打印到標準輸出流。
const filenames = Deno.args;
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(file, Deno.stdout);
file.close();
}
除了內核到用戶空間再到內核的必要拷貝,這里的 copy() 函數(shù)不會產生額外的昂貴操作,從文件中讀到的數(shù)據會原樣寫入標準輸出流。這反映了 Deno I/O 流的通用設計目標。 嘗試一下:
deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd
更多建議: