「ファイルの読み書き」の編集履歴(バックアップ)一覧はこちら
「ファイルの読み書き」(2014/11/17 (月) 09:38:22) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
#contents()
*ローカルファイルをUTF-8で読み込み(非同期)
-[[JavaScript OS.File | MDN>https://developer.mozilla.org/en-US/docs/JavaScript_OS.File]]
-[[Promise - JavaScript | MDN>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise]]
非同期の読み込みでパフォーマンスが良いので普通はこれで。
非同期の処理にES6のPromiseを使用しているので、Promiseの機能も理解している必要が有る。
#highlight(javascript){{
Cu.import("resource://gre/modules/osfile.jsm");
const filePath = "C:\\temp\\sample.txt";
let promise = OS.File.read(filePath, { encoding: "utf-8" });
promise.then(text => {
console.log('テキストファイルを読み込みました。:' + text);
}, e => {
console.error('ファイルの読み込みが出来ませんでした。:' + e);
});}}
*ローカルファイルをUTF-8で書き込み(非同期)
#highlight(javascript){{
Cu.import("resource://gre/modules/osfile.jsm");
let filePath = "C:\\temp\\sample.txt";
let promise = OS.File.writeAtomic(filePath, "sample text",
{ encoding: "utf-8", tmpPath: filePath + ".temp" });
promise.then(text => {
console.log('テキストファイルを書き込みました。');
}, e => {
console.error('ファイルの書き込みが出来ませんでした。:' + e);
}); }}
*ローカルファイルをUTF-8で読み込み(同期)
-[[File I/O - Mozilla | MDN>https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O]]
-[[FileUtils.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm]]
-[[NetUtil.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/NetUtil.jsm]]
どうしても同期読み込みをしたければこちらを使用。
#highlight(javascript){{
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
let file = new FileUtils.File("C:\\temp\\sample.txt");
if (!file.exists()) {
console.error('ファイルが存在しません。');
return;
}
let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fis.init(file, -1, 0, 0);
let text = NetUtil.readInputStreamToString(fis, fis.available(), {charset:"UTF-8"});
console.log('テキストファイルを読み込みました。:' + text); }}
*chromeファイルをUTF-8で読み込み(非同期)
-[[NetUtil.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/NetUtil.jsm]]
#highlight(javascript){{
Cu.import("resource://gre/modules/NetUtil.jsm");
let file = "chrome://sample/content/sample.txt";
NetUtil.asyncFetch(file, function(aInputStream, aResult) {
if (!Components.isSuccessCode(aResult)) {
console.error('ファイルの読み込みが出来ませんでした。');
return;
}
let text = NetUtil.readInputStreamToString(aInputStream, aInputStream.available(), {charset:"UTF-8"});
console.log('テキストファイルを読み込みました。:' + text);
} }}
#contents()
*書き込み
**ローカルファイルをUTF-8で書き込み(非同期)
-[[JavaScript OS.File | MDN>https://developer.mozilla.org/en-US/docs/JavaScript_OS.File]]
-[[Promise - JavaScript | MDN>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise]]
非同期の処理にES6のPromiseを使用しているので、Promiseの機能も理解している必要が有る。
#highlight(javascript){{
Cu.import("resource://gre/modules/osfile.jsm");
let filePath = "C:\\temp\\sample.txt";
let promise = OS.File.writeAtomic(filePath, "sample text",
{ encoding: "utf-8", tmpPath: filePath + ".temp" });
promise.then(text => {
console.log('テキストファイルを書き込みました。');
}, e => {
console.error('ファイルの書き込みが出来ませんでした。:' + e);
}); }}
フォルダを作ったりはしてくれないので、フォルダは事前に用意する。
*読み込み
**ローカルファイルをUTF-8で読み込み(非同期)
非同期の読み込みでパフォーマンスが良いので普通はこれで。
#highlight(javascript){{
Cu.import("resource://gre/modules/osfile.jsm");
const filePath = "C:\\temp\\sample.txt";
let promise = OS.File.read(filePath, { encoding: "utf-8" });
promise.then(text => {
console.log('テキストファイルを読み込みました。:' + text);
}, e => {
console.error('ファイルの読み込みが出来ませんでした。:' + e);
});}}
**ローカルファイルをUTF-8で読み込み(同期)
-[[File I/O - Mozilla | MDN>https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O]]
-[[FileUtils.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm]]
-[[NetUtil.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/NetUtil.jsm]]
どうしても同期読み込みをしたければこちらを使用。
#highlight(javascript){{
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
let file = new FileUtils.File("C:\\temp\\sample.txt");
if (!file.exists()) {
console.error('ファイルが存在しません。');
return;
}
let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fis.init(file, -1, 0, 0);
let text = NetUtil.readInputStreamToString(fis, fis.available(), {charset:"UTF-8"});
console.log('テキストファイルを読み込みました。:' + text); }}
**chromeファイルをUTF-8で読み込み(非同期)
-[[NetUtil.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/NetUtil.jsm]]
#highlight(javascript){{
Cu.import("resource://gre/modules/NetUtil.jsm");
let file = "chrome://sample/content/sample.txt";
NetUtil.asyncFetch(file, function(aInputStream, aResult) {
if (!Components.isSuccessCode(aResult)) {
console.error('ファイルの読み込みが出来ませんでした。');
return;
}
let text = NetUtil.readInputStreamToString(aInputStream, aInputStream.available(), {charset:"UTF-8"});
console.log('テキストファイルを読み込みました。:' + text);
} }}