How to read and write a file using javascript?

How to read and write a file using javascript?

Files can be read and written by using java script functions – fopen(),fread() and fwrite().

The function fopen() takes two parameters – 1. Path and 2. Mode (0 for reading and 3 for writing). The fopen() function returns -1, if the file is successfully opened.

The function fread() is used for reading the file content.

str = fread(file,flength(file) ;
The function fwrite() is used to write the contents to the file.

file = fopen("c:\MyFile.txt", 3);// opens the file for writing
fwrite(file, str);// str is the content that is to be written into the file.

How to read and write a file using javascript?

There are two ways to do it:

1. Using JavaScript extensions (runs from JavaScript Editor), or

2. Using a web page and ActiveX objects (Internet Explorer only)

In JavaScript Extensions, you can use
fh = fopen(getScriptPath(), 0); to open a file

Using ActiveX objects, following should be included in your code to read a file:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile("C:\\example.txt", 1, true);
How do you create a new object in JavaScript?
In order to generate dynamic content, JSP provides for creating, modifying and interacting with Java objects...
How to create arrays in JavaScript?
Although you can create the arrays using β€˜new’ (var myArray = new myArray[10];), it is recommended that you create it in the following way: var myArray = [];...
What is a software design pattern?
What is a software design pattern? - A reusable software design solution in general for the problems that are recurrently occuring....
Post your comment