locked
Opening & editing a txt file RRS feed

  • Question

  • Hello everybody,

    i'm new to Windows 8 and WinJS and i have a question. How can i open a .txt file and write new content( with the old content) in this .txt file?

    It would be awesome if somebody can help me :)

    Sincerly, saiikon

    Sunday, November 11, 2012 9:52 PM

Answers

  • Hi,

    Please refer to the code as follow:

    localFolder.getFileAsync("myfile.data")
                        .then(function (file) {
                            Windows.Storage.FileIO.writeTextAsync(file, "I am rewrite again again").done(function () {
                                localFolder.getFileAsync("myfile.data")
                        .then(function (file) {
                            return Windows.Storage.FileIO.readTextAsync(file);
                        }).done(function (text) {
                            // activitiesArray = JSON.parse(text);
                            document.getElementById("content").innerHTML = text;
                        });
                            });
                        });

    More details, please refer to: http://msdn.microsoft.com/en-us/library/windows/apps/hh701508.aspx .


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework



    • Edited by Song Tian Tuesday, November 13, 2012 3:11 AM
    • Marked as answer by Song Tian Friday, November 16, 2012 9:06 AM
    Tuesday, November 13, 2012 2:03 AM

All replies

  • Hi,

    Please refer the example as follow:

    //var jsonText = JSON.stringify(activitiesArray)  
        var jsonText="hey, I am transfered";  
      
        var localFolder = Windows.Storage.ApplicationData.current.localFolder;  
        //localFolder.createFileAsync("myfile.data", Windows.Storage.CreationCollisionOption.replaceExisting)  
        //    .then(function (file) {  
        //        return Windows.Storage.FileIO.writeTextAsync(file, jsonText);  
        //    });  
      
      
        localFolder.getFileAsync("myfile.data")  
                        .then(function (file) {  
                            return Windows.Storage.FileIO.readTextAsync(file);  
                        }).done(function (text) {  
                            // activitiesArray = JSON.parse(text);  
                            document.getElementById("content").innerHTML=text;  
                        });  


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Monday, November 12, 2012 9:05 AM
  • Hey man!

    Thansk for the answer, i have tried this code now and it does nothing :/ No error or something else..

        function writeFile(input) {
            var applicationData = Windows.Storage.ApplicationData.current;
            var localFolder = applicationData.localFolder;
    
            localFolder.getFileAsync("last.txt")
               .then(function (sampleFile) {
                   var old = Windows.Storage.FileIO.readTextAsync(sampleFile);
                   return Windows.Storage.FileIO.writeTextAsync(sampleFile, old + input);
               });
        }

    Monday, November 12, 2012 1:24 PM
  • Hi,

    Please refer to the code as follow:

    localFolder.getFileAsync("myfile.data")
                        .then(function (file) {
                            Windows.Storage.FileIO.writeTextAsync(file, "I am rewrite again again").done(function () {
                                localFolder.getFileAsync("myfile.data")
                        .then(function (file) {
                            return Windows.Storage.FileIO.readTextAsync(file);
                        }).done(function (text) {
                            // activitiesArray = JSON.parse(text);
                            document.getElementById("content").innerHTML = text;
                        });
                            });
                        });

    More details, please refer to: http://msdn.microsoft.com/en-us/library/windows/apps/hh701508.aspx .


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework



    • Edited by Song Tian Tuesday, November 13, 2012 3:11 AM
    • Marked as answer by Song Tian Friday, November 16, 2012 9:06 AM
    Tuesday, November 13, 2012 2:03 AM