File System for Browser JS / nw.js
download www/gen/FS.js (Using require.js is recommended)
FS.get method returns a File Object which can access to localStorage
<script src="path/to/FS.js"></script>
<script>
var mydir=FS.get("/mydir/");// Directory in localStorage
var myfile=mydir.rel("myfile.txt"); // File in localStorage (/mydir/myfile.txt)
myfile.text("my content"); // write file
alert(myfile.text()); // read file
mydir.each(function (f) { // ls mydir
alert(f.name());
});
</script>
Almost same as running in Browser, but FS.get returns a File Object which can access to local files
var mydir=FS.get("/mydir/");// Directory in local file(Linux/Mac)
var mydir=FS.get("C:/mydir/");// Directory in local file(Windows)
##Quick Reference for File Object
File Object cab be get by calling FS.get(path)
. f
and d
is a File Object which represents a file and directory respectively.
fd
can be either file or directory.
f.text(str)
writestr
to the filef.text()
returns file content of the file in stringf.obj(o)
write objecto
in JSON to the filef.obj()
returns the file content as object. The content of the file should be written in JSONf.bytes(b)
write ArrayBuffer/Buffer(node) to the filef.bytes()
returns file content as ArrayBuffer(browser)/Buffer(node)f.getBytes({binType:ArrayBuffer})
returns file content as ArrayBuffer(both in browser and node)d.each(func)
iterates over the directory by passing each File Object tofunc
d.recursive(func)
iterates over all files in the directory and its subdirectory by passing each File Object tofunc
d.listFiles()
returns array of File Objects which represents all files in the directoryd.ls()
returns array of names of all files in the directoryd.rel(relPath)
returns new File Object specified by the relative pathd.relPath(base)
returns the relative path of the file from thebase
File Object.fd.up()
returns new File Object which is parent of the file/directory.fd.path()
returns the full path of the file/directoryfd.name()
returns the name of the file/directoryfd.ext()
returns the extention of the file/directoryfd.truncExt(e)
returns the name removinge
(whene
is ommited, equals tofd.ext()
)f.lastUpdate()
return the file's time stamp as the number corrensponding to the value of Date.getTimef.rm()
removes the filed.rm()
removes the directory if it is emptyd.rm({r:true})
removes the directory and all subfiles/subdirectories.fd.exists()
checks whether the file/directory existsfd.isDir()
checks whether the File Object is directoryf.copyFrom(src)
copies from src(File Object) to the filef.copyTo(dst)
copies the file to dst(File Object)f.moveFrom(src)
moves from src(File Object) to the filef.moveTo(dst)
moves the file to dst(File Object)d.mkdir()
creates the directory