Javascript File ReadAll Method of textstream object provided by the FileSystemObject (FSO) also performs the action of file reading. Textstream object’s File ReadAll method reads all the lines present in the specified text file to the OpenTextStream method. But using the ReadAll method for large files is not efficient coz it consumes lots of memory to store the text data into the memory stream as textstream. ReadAll method reads all the line to the end of textstream and returns the result in the form of string value.
Syntax for Javascript FSO textstream File ReadAll method
textStreamObj.ReadAll();
ReadAll method of textstream object does not take any parameter to perform the action as shown in the above syntax.
Example of FSO textstream File ReadAll method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Open the text file at the specified location with write mode
var txtFile = fso.OpenTextFile("C:\\Temp\\myFolder\\file.txt", 2, false, 0);
txtFile.WriteLine("Hello this is the first line of text file.");
txtFile.WriteLine("Hello this is the second line of text file.");
txtFile.WriteLine("Hello this is the third line of text file.");
txtFile.Close();
// Open the text file at the specified location with read mode
txtFile = fso.OpenTextFile("C:\\Temp\\myFolder\\file.txt", 1, false, 0);
// reads all the lines from the specified file and returns the result as string
var fText = txtFile.ReadAll();
document.write("<pre>" + fText + "</pre>");
// close the open textstream object file
txtFile.Close();
fso = null
// Output:
// Hello this is the first line of text file.
// Hello this is the second line of text file.
// Hello this is the third line of text file.
</script>
Continue to next tutorial: Javascript FSO textstream File ReadLine Method to learn how to read a single line from textstream.