This repository was archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
WorkingWithFileSystem
Minkyu Lee edited this page Oct 9, 2015
·
7 revisions
In this chapter, we're going to learn how to read local files and write text data to a local file. Note that only UTF-8 encoded text files are supported.
To read a text file, use FileSystem and FileUtils modules.
var FileSystem = app.getModule("filesystem/FileSystem"),
FileUtils = app.getModule("file/FileUtils");First, you need to create a file object with a full pathname of an existing file. And then, you can read the text using FileUtils.readAsText function. It returns JQuery's Promise object, so you need to pass callback functions to get the text data in the file or error message. Following is an example reading a local text file.
var file = FileSystem.getFileForPath("/Users/niklaus/my-file.txt");
FileUtils.readAsText(file)
.done(function (data) {
console.log(data);
})
.fail(function (err) {
console.error(err);
});var file = FileSystem.getFileForPath("/Users/niklaus/my-new-file.txt");
var text = "First line\nSecond line\nThird line...";
FileUtils.writeText(file, text, true)
.done(function () {
console.log("File saved.");
})
.fail(function (err) {
console.error(err);
});