Javascript File Read Method of textstream object provided by the FileSystemObject (FSO) performs the action of file reading. Textstream object’s File Read method reads only specified number of characters starting from the current position of the pointer of textstream. Initially it starts reading from the first character of first column of first line of specified text file. Read method reads the specified number of characters and returns the result in the form of string value.
Syntax for Javascript FSO textstream File Read method
textStreamObj.Read( Ncharacters );
Read method of textstream object accepts only 1 parameter Ncharacters of integer type. Ncharacters accepts the integer value that specifies the number of characters to be read from the textstream of text specified file.
Example of FSO textstream File Read 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 the first 10 characters of 1st line and returns the result as string
var fText = txtFile.Read(10);
document.write(fText);
// close the open textstream object file
txtFile.Close();
fso = null
// Output:
// Hello this
</script>
Continue to next tutorial: Javascript FSO textstream File ReadAll Method to learn how to read all the lines loaded in the textstream.