locked
JavaScript Metra App reading a file

    Question

  • I have a txt file in a Data folder in my Project

    I want to read that file and assign it to a string variable

    I found This code 

      function ReadAllDataFile(fileNameInLocalTree) {
             
             var package = Windows.ApplicationModel.Package.current;
             var installedLocation = package.installedLocation;
          
             installedLocation.createFileAsync(fileNameInLocalTree, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                 dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size == 0) {
                        // Data not found
                    }
                    else {
                        var inputStream = stream.getInputStreamAt(0);
                        
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
         
                        reader.loadAsync(size).then(function () {
                            var newString = "";
                            //var contents = reader.readString(size); // fails with multibyte error if bad data (see legislators.getList.json)
                           // allocate the full array so readBytes can insert it in full
                            var array = new Array(size);
                            
                            reader.readBytes(array);
                          
                            
                           
                             
                            
                            for (var i = 0; i < array.length; i++) {
                                //// only printable characters (include spaces because could be part of names) (very rough here)
                                //// http://www.csgnetwork.com/asciiset.html
                               if (array[i] >= 32 && array[i] <= 126) {
                                var c = String.fromCharCode(array[i]);
                               var c = String.fromCharCode(array[i]);
                              newString += c;
                               }
         
                          return newString ;
                          
                        });

    this code is working but it does read the file correctly

    because txt file is coded in utf-8

    I was hoping that some one give me a code to do the job or a way to convert bytes into a utf-8

    Tuesday, August 14, 2012 12:05 PM

Answers

All replies

  • Tuesday, August 14, 2012 10:28 PM
  • I changed my code to became like this

      function ReadAllDataFile(fileNameInLocalTree) {
             
             var package = Windows.ApplicationModel.Package.current;
             var installedLocation = package.installedLocation;
          
             installedLocation.createFileAsync(fileNameInLocalTree, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                 dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size == 0) {
                        // Data not found
                    }
                    else {
                        var inputStream = stream.getInputStreamAt(0);
                        
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
         
                        reader.loadAsync(size).then(function () {
                            var newString = "";
                            //var contents = reader.readString(size); // fails with multibyte error if bad data (see legislators.getList.json)
                           // allocate the full array so readBytes can insert it in full
                            var array = new Array(size);
                            
                            reader.readBytes(array);
                 newString = Windows.Security.Cryptography.CryptographicBuffer.convertBinaryToString(Windows.Security.Cryptography.BinaryStringEncoding.utf8, Windows.Storage.Streams.Buffer(size));
    
    return newString ;
    
                        });

    but it did not work for me , it returns an empty string

    Wednesday, August 15, 2012 5:47 AM
  • You do not have to go through all that to read a text file.

    The file access sample shows you how to read text: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

      Windows.Storage.FileIO.readTextAsync(SdkSample.sampleFile).done(function (fileContent) { 
              

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, August 15, 2012 3:44 PM
    Moderator
  • If you have a UTF8 file that you'd like to read via DataReader, you can do the following (I tested this with a simple UTF8 file):

    file.openReadAsync().done(function (stream) {
        var reader = new Windows.Storage.Streams.DataReader(stream);
        reader.loadAsync(stream.size).then(function (n) {
            var text = reader.readString(n);
            document.getElementById("output").innerText += text + "\n";
        }).done(function () { 
            stream.close(); 
        });
    });

    Let me know if that works for you...

    As Jeff mentioned, you can also use the file I/O helper functions as well:

    Windows.Storage.FileIO.readTextAsync(file).done(function (contents) {
        // Do something with contents
    });

    Thanks,

    Justin Cooperman

    Thursday, August 16, 2012 7:11 PM
  • If you have a UTF8 file that you'd like to read via DataReader, you can do the following (I tested this with a simple UTF8 file):

    file.openReadAsync().done(function (stream) {
        var reader = new Windows.Storage.Streams.DataReader(stream);
        reader.loadAsync(stream.size).then(function (n) {
            var text = reader.readString(n);
            document.getElementById("output").innerText += text + "\n";
        }).done(function () { 
            stream.close(); 
        });
    });

    Let me know if that works for you...

    As Jeff mentioned, you can also use the file I/O helper functions as well:

    Windows.Storage.FileIO.readTextAsync(file).done(function (contents) {
        // Do something with contents
    });

    Thanks,

    Justin Cooperman

    The code didn't work for me . but thanx
    Saturday, August 18, 2012 12:26 AM