Write operation in Rhodes Framework

// Expert user has replied.
S Subhajit Choudhury 3 years 7 months ago
5 1 0

Hi,

I am facing 2 issues:
1) Create a file in the project directory through code in controller
2) Cannot write into an existing JSON file.

For issue 2), I have created a code to write into an existing JSON file, but an error is thrown: App error: invalid access mode test.json
Following is the snippet:
    tempHash = {
        "key_a" => "val_a",
        "key_b" => "val_b"
    }
    File.open(Rho::RhoApplication::get_model_path('app','JsonTest'), 'test.json','w') do |f|
      f.write(tempHash.to_json)

What I can guess is that 'w' is not working in Rhodes.
Much appreciated if anyone can work me out from these 2 issues

Subhajit

Please Register or Login to post a reply

1 Replies

B Bhakta Ranjan Satapathy

Hi,

Kindly refer docs available at http://docs.rhomobile.com/en/5.0.0/api/File#mwrite

Code Snippet

new_file = Rho::RhoFile.join(Rho::Application.userFolder, "new_file.txt")
file = Rho::RhoFile.new(new_file, Rho::RhoFile::OPEN_FOR_READ_WRITE)
file.write("Writing to newly created file")
file.close

existing_file = Rho::RhoFile.join(Rho::Application.userFolder, "new_file.txt")
file = Rho::RhoFile.new(openTestFile, Rho::RhoFile::OPEN_FOR_APPEND)
file.write("Writing to existing file")
file.close

Thanks

CONTACT
Can’t find what you’re looking for?