Javascript File ReadLine Method of textstream object provided by the FileSystemObject (FSO) also performs the action of file reading. Textstream object’s File ReadLine method reads a whole line at a time from the specified text file for the OpenTextStream method. ReadLine method reads a single line to the end and returns the result in the form of string value. After reading a line it moves the cursor position to the starting column of next line and reads the next line to its end when ReadLine method is called next time.
Syntax for Javascript FSO textstream File ReadLine method
textStreamObj.ReadLine();
ReadLine method of textstream object does not take any parameter to perform the action as shown in the above syntax.
Example of FSO textstream File ReadLine 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 1 line at a time from the specified file and returns the result as string
var fText = txtFile.ReadLine();
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.
</script>
Continue to next tutorial: Javascript FSO textstream File Skip Method to learn how to skip the specified number of characters in textstream.