still problems working with files [obsolete]

Posted on
  • Good morning!

    I still have my problems working with StorageFiles.

    When I try

    const Storage = require('Storage');
      function compareFileWith (expectedContent) {
        let readText = Storage.open('4testing','r').read();
        if (readText != expectedContent) {
          print('>>>> file does not look as expected');
          print('>>>> expected "' + expectedContent + '"');
          print('>>>> got "' + readText + '"');
        }
      }
    
    /**** create file ****/
    
      let TestFile = Storage.open('4testing','w');
    
      if (TestFile.write('4testing','Hello, World!')) {
        print('file "4testing" was successfully created');
      } else {
        print('file "4testing" could not be created');
      }
      compareFileWith('Hello, World!');
    print('finished');
    

    I only get the following output

    file "4testing" could not be created
    >>>> file does not look as expected
    >>>> expected "Hello, World!"
    >>>> got "undefined"
    finished
    

    Thus, how can I create a new file if I must not mix Storage methods with StorageFile ones?

  • Oops - I found my mistake: I should replace

    if (TestFile.write('4testing','Hello, World!')) {
    

    with

    if (TestFile.write('Hello, World!')) {
    

    shouldn't I?

    Sorry for bothering you!

  • Thr 2020.01.09

    Glad to see the solution worked through @Andreas_Rozek

    For those seeking the reference link for read and write

    https://banglejs.com/reference#Storage
    https://banglejs.com/reference#StorageFi­le

    other suggested use snippet examples

    http://forum.espruino.com/conversations/­329768/

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

still problems working with files [obsolete]

Posted by Avatar for Andreas_Rozek @Andreas_Rozek

Actions