locked
Is it possible to read line per line from a file In JavaScript win8? RRS feed

  • Question

  • Hi , 

    i would like to make a function that can manipulate data from a file and read them  line per line , I was wondering if that's possible 

    Example : if a file named exemple.txt containts 

    007

    008

    009

    005 

    how to returns value seperatly and store them in a variable? 

    thank you 

    Friday, April 5, 2013 3:35 PM

Answers

  • It's quite straightforward. For example, if you have a file called data.txt in your local appdata, use this code:
    Windows.Storage.PathIO.readLinesAsync("ms-appdata:///local/data.txt").done(function (lines) {    
        lines.forEach(function (line) {
            console.log(line);
        });
    });
    If you have a StorageFile instead of a path or URI, then use FileIO.readLinesAsync instead. As you can see, the result of either function is a vector you can iterate with forEach, where the forEach function gets each line in turn.
    • Marked as answer by Bassel elb Friday, April 5, 2013 7:33 PM
    Friday, April 5, 2013 7:32 PM

All replies

  • Try Windows.Storage.FileIO.readLinesAsync or Windows.Storage.PathIO.readLineAsync. These read the file into a vector object, which you can then iterate through to process the individual lines.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    Friday, April 5, 2013 4:28 PM

  • Thank you Mr. kraig but this is the libraries , is there any examples on how to use them? would you mind if you show me somes examples or refer me to a good tutorial ? I can't find

    Thanks

    Friday, April 5, 2013 6:07 PM
  • It's quite straightforward. For example, if you have a file called data.txt in your local appdata, use this code:
    Windows.Storage.PathIO.readLinesAsync("ms-appdata:///local/data.txt").done(function (lines) {    
        lines.forEach(function (line) {
            console.log(line);
        });
    });
    If you have a StorageFile instead of a path or URI, then use FileIO.readLinesAsync instead. As you can see, the result of either function is a vector you can iterate with forEach, where the forEach function gets each line in turn.
    • Marked as answer by Bassel elb Friday, April 5, 2013 7:33 PM
    Friday, April 5, 2013 7:32 PM
  • Thank you Sir!  and nice tutorial you have ! I will read it
    Friday, April 5, 2013 7:34 PM