ファイルを読み込むには
各言語での基本形。
テキスト読み込みを中心に。
公開:
Python
with open("a.txt", encoding="utf-8") as f:
s = f.read()with で自動クローズ。
encoding は明示推奨。
JavaScript
import { readFile } from "node:fs/promises";
const s = await readFile("a.txt", "utf8");Node.js。
ブラウザ不可。
TypeScript
import { readFile } from "node:fs/promises";
const s: string = await readFile("a.txt", "utf8");型注釈以外は JS と同じ。
つまずき
文字化けはエンコーディングの明示で回避(詳細は各言語の「うまくいかない時」リンク)。