Created
June 1, 2018 11:12
-
-
Save veekram/f78ab4bcdf1d1d6284e3b44ffda10640 to your computer and use it in GitHub Desktop.
Read Write to file with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// write to file | |
var txtFile = "c:/test.txt"; | |
var file = new File(txtFile); | |
var str = "My string of text"; | |
file.open("w"); // open file with write access | |
file.writeln("First line of text"); | |
file.writeln("Second line of text " + str); | |
file.write(str); | |
file.close(); | |
/// read from file | |
var txtFile = "c:/test.txt" | |
var file = new File(txtFile); | |
file.open("r"); // open file with read access | |
var str = ""; | |
while (!file.eof) { | |
// read each line of text | |
str += file.readln() + "\n"; | |
} | |
file.close(); | |
alert(str); | |
//////////////////////////////////////////////////// | |
/** | |
* writeTextFile write data to file on hard drive | |
* @param string filepath Path to file on hard drive | |
* @param sring output Data to be written | |
*/ | |
function writeTextFile(filepath, output) { | |
var txtFile = new File(filepath); | |
txtFile.open("w"); // | |
txtFile.writeln(output); | |
txtFile.close(); | |
} | |
//////////////////////////////////////////////////// | |
/** | |
* readTextFile read data from file | |
* @param string filepath Path to file on hard drive | |
* @return string String with file data | |
*/ | |
function readTextFile(filepath) { | |
var str = ""; | |
var txtFile = new File(filepath); | |
txtFile.open("r"); | |
while (!txtFile.eof) { | |
// read each line of text | |
str += txtFile.readln() + "\n"; | |
} | |
return str; | |
} | |
//////////////////////////////////////////////////// | |
// Great parse examples with result to process file | |
// http://www.w3schools.com/jsref/jsref_parseint.asp | |
// var a = parseInt("10"); // 10 | |
// var b = parseInt("10.00"); // 10 | |
// var c = parseInt("10.33"); // 10 | |
// var d = parseInt("34 45 66"); // 34 | |
// var e = parseInt(" 60 "); // 60 | |
// var f = parseInt("40 years"); // 40 | |
// var g = parseInt("He was 40"); // NaN | |
// | |
// var h = parseInt("10",10); // 10 | |
// var i = parseInt("010"); // 10 | |
// var j = parseInt("10",8); // 8 | |
// var k = parseInt("0x10"); // 16 | |
// var l = parseInt("10",16); // 16 | |
// http://www.w3schools.com/jsref/jsref_parsefloat.asp | |
// var a = parseFloat("10"); // 10 | |
// var b = parseFloat("10.00"); // 10 | |
// var c = parseFloat("10.33"); // 10.33 | |
// var d = parseFloat("34 45 66"); // 34 | |
// var e = parseFloat(" 60 "); // 60 | |
// var f = parseFloat("40 years"); // 40 | |
// var g = parseFloat("He was 40"); // NaN |
you are wrong, this doesn't work
I did try your way but unfortunately, the new File doesn't work. how did you make it work?
I'm curious, can I use this code for reading and writing files on a website like my Github site?
The problem it doesn't work for local files is that Google Chrome and other browsers, for security options, don't want to run that code.
I'm curious, can I use this code for reading and writing files on a website like my Github site? The problem it doesn't work for local files is that Google Chrome and other browsers, for security options, don't want to run that code.
I am not sure what i used this script actually for. Sorry about that -_-
Ok, thanks 😀
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there Veekram,
I'm trying to write a file to a linux system. I tried your code (modified) and it doesnt' seem to do anything. Any Idea how I can get it working?
Here's what I used: