8

I'm writing a web-app for the iPad that will be loading data from a text file. (A sample data set is around ~400 kb). I have everything set up except the file reading. The way I have set up my code, you pass an object which reads a file line by line.

How can I read a file line by line?

If there is no direct way to read a file line by line, can someone please show me an example of how to read a file into a string object? (so that I can use the split method :P)

tshepang
  • 12,111
  • 21
  • 91
  • 136
Ryan Amos
  • 5,422
  • 4
  • 36
  • 56

4 Answers4

14

This could work, if I understood what you want to do:

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://website.com/file.txt", true);
txtFile.onreadystatechange = function()
{
  if (txtFile.readyState === 4) {  // document is ready to parse.
    if (txtFile.status === 200) {  // file is found
      allText = txtFile.responseText; 
      lines = txtFile.responseText.split("\n");
    }
  }
}
txtFile.send(null);
afaf12
  • 5,163
  • 9
  • 35
  • 58
  • It sends the request, not the file. At this moment the file is not received yet. The `null` argument, which could be omitted in this case, means that you do not upload any file to the server. – Luc125 Jul 28 '11 at 15:24
  • @awerti Thank you! It works! I had to shuffle around and rearrange my program, but I got the file reading to work. – Ryan Amos Jul 28 '11 at 19:05
  • How do you get one line at a time through EOF in a loop? – Wolfpack'08 Sep 15 '12 at 22:04
  • that comment is in a function called "myfunction()" in my code but when i run it it says "myfunction is undefined ?" – abidinberkay Apr 12 '16 at 10:38
2

With jQuery:

myObject = {}; //myObject[numberline] = "textEachLine";
$.get('path/myFile.txt', function(myContentFile) {
   var lines = myContentFile.split("\r\n");

   for(var i  in lines){
      //here your code
      //each line is "lines[i]"

      //save in object "myObject": 
      myObject[i] = lines[i]

      //print in console
      console.log("line " + i + " :" + lines[i]);
   }
}, 'text');
geralOE
  • 484
  • 4
  • 7
  • 2
    This would work. It does require JQuery though, which is relatively common, but it is still a caveat to consider. I was not using JQuery in that project. – Ryan Amos Jul 22 '13 at 16:05
2

Mobile Safari doesn't have the File API, so I assume you're talking about reading from a web resource. You can't do that. When you read a resource via ajax, the browser will first read it fully into memory and then pass the entire string to your ajax callback as a string.

In your callback, you can take the string and break it into lines, and wrap that up in an object that has the API that your code wants, but you're still going to have the string in memory all at once..

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Is a 400 kb file reasonable for iPad memory? – Ryan Amos Jul 28 '11 at 15:09
  • 1
    @Ryan: Well, the first generation iPad has 256M = 262,656k of RAM. 400k is less than two tenths of one percent of that (~0.15%). I have no idea how much of it Mobile Safari is allowed to use, though. You'll have to try it out and see. And of course, be careful not to end up making lots of copies of the string once you've read it... – T.J. Crowder Jul 28 '11 at 15:13
  • It's more about the memory allocated to Mobile Safari. Many applications greatly limit the memory allocated to JS. – Ryan Amos Jul 28 '11 at 16:23
  • @Ryan: Hence *"I have no idea how much of it Mobile Safari is allowed to use, though."* ;-) – T.J. Crowder Jul 28 '11 at 16:41
0

i dont think thats possible until you use ajax to hit some server side code.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • Well, I found a FileReader object: https://developer.mozilla.org/en/DOM/FileReader. Unfortunately, it reads the entire file and I'm confused on how to use it. I think it is possible, if such an object exists and does what I think it does (read a file). – Ryan Amos Jul 28 '11 at 15:06
  • @Ryan: See my answer. `FileReader` is part of the File API, which Mobile Safari does not support. – T.J. Crowder Jul 28 '11 at 15:08