开发者

Is there a limit on a JS variable that holds the contents of a file that is returned from ReadAll method?

开发者 https://www.devze.com 2023-01-30 07:49 出处:网络
I am reading a file using JS like this: var fso = new ActiveXObject(\"Scripting.FileSystemObject\"); var s = fso.OpenTextFile(\"C:\\\\file.txt\", 1, true);

I am reading a file using JS like this:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile("C:\\file.txt", 1, true);
rows = s.ReadAll(); //also we can use s.ReadAll() to read all the lines;
s.Close();

Is there a limit to the variable 'rows'? If I have a file with 1 million rows, will that be okay, or could it开发者_如何学编程 exceed the memory capacity of the variable?

Thanks.


From MS docs:

For large files, using the ReadAll method wastes memory resources. Other techniques should be used to input a file, such as reading a file line by line.

Try ReadLine method. For example,

var ForReading = 1, ForWriting = 2, ForAppending = 8;

var fso = new ActiveXObject("Scripting.FileSystemObject");

// Open the file for output.
var filename = "c:\\testfile.txt";

var f = fso.OpenTextFile(filename, ForWriting, true);

// Write to the file.
f.WriteLine("Hello world!");
f.WriteLine("The quick brown fox");
f.Close();

// Open the file for input.
f = fso.OpenTextFile(filename, ForReading);

// Read from the file and display the results.
while (!f.AtEndOfStream)
    {
    var r = f.ReadLine();
    document.write (r + "<br />");
    }
f.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消