how to create a file using RhoFile

Hello,

I have a newbie question: Can I create a file and write to it using the RhoFile APIs?

I am able to read the file contents using RhoFile.read() but there is no corresponding write. There is a write instance method, but I cannot figure out how to create a RhoFile instance for the specified path from the documentation.

Higher level question - I need to store a few settings that are specific to my application, is there a simpler way to do this, instead of the approach of creating my own config file and serializing my settings to it?

Thanks!

Ram

vinothini balak...
Hi Ram,Let me try to come up

Hi Ram,

Let me try to come up with an example for RhoFile.write(), in the mean time you can make use of rhoconfig.txt file for storing temporary variables. See Rhomobile | Application Configuration

It is also an easier way instead of creating a file.

Example:

Let say you have an variable called FOOBAR = 23 in rhoconfig.txt file, then

you can access it as Rho::RhoConfig.FOOBAR

you can set value for it as Rho::RhoConfig.FOOBAR = 32

Vinothini B

Spritle Software

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Ram Venkatesh
Hello Vinothini,Thank you for

Hello Vinothini,

Thank you for the reply, one clarification: We are using the JavaScript APIs in Rho Mobile 4.0. A quick experiment to set a custom property fails with a type error: TypeError: 'undefined' is not an object (evaluating 'Rho.RhoConfig.foobar = "foobar"'). Is there a different object / syntax for doing this via javascript in 4.0?

Thanks,

Ram

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


vinothini balak...
Hi Ram,You are right. Rho

Hi Ram,

You are right. Rho.RhoConfig is not available as JavaScript object in Rho, which means we cannot access variables stored at rhoconfig.txt

With ruby, it was easy and we prefer using rhoconfig.txt instead of storing settings variables in model. (example: api_url)

Thank you.

Vinothini

Spritle Software

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Kutir Mobility
A simple approach could be to

A simple approach could be to create a Ruby controller that uses Rho::RhoConfig to read and store the values, and use Javascript to call that Ruby controller. You have an example of calling Ruby from Javascript here: Rhomobile | Accessing Web services from RhoMobile

Thanks,

Javier

Kutir Mobility

Vote: 
Vote up!
Vote down!

Points: 1

You voted ‘up’


Ram Venkatesh
Thank you Javier.At this

Thank you Javier.

At this point we don't have any Ruby in our app (all HTML5 and JavaScript) , and it seems overkill to introduce a completely different language for this small task.

Do appreciate the suggestion though.

Thanks,

Ram

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Jon Tara
Why not store your settings

Why not store your settings in a model?

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Ram Venkatesh
We are currently not using

We are currently not using the ORM functionality or the local database in this app.

Our requirement is to persist a couple string settings, I am open to creating a model and using it if that is the best approach.

Thanks,

Ram

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Shruti Kukkar
You could use AppApplication

You could use AppApplication class. This class is available all the time while the application is running. It can be used to store settings like you are describing.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


vinothini balak...
Hi Shruti,How to access

Hi Shruti,

How to access AppApplication from Javascript ?

Vinothini B

Spritle Software

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Kutir Mobility
The way to create a file with

The way to create a file with RhoFile is:

var myFile = new Rho.RhoFile(Rho.File.join(Rho.Application.userFolder,"myfilename.txt"),<file open mode>);

where <file open mode> is one of:

Rho.RhoFile.OPEN_FOR_APPEND

Rho.RhoFile.OPEN_FOR_READ

Rho.RhoFile.OPEN_FOR_READ_WRITE

Rho.RhoFile.OPEN_FOR_WRITE

You would use OPEN_FOR_WRITE to overwrite a file with new contents and OPEN_FOR_READ to read stored values.

Thanks,

Javier

Kutir Mobility

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Ram Venkatesh
Thank you, this is exactly

Thank you, this is exactly what I was looking for.

BTW, this information is now also in the updated documentation for Rhomobile | RhoFile.

Best,
Ram

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Mohankumar SK
Hi all,I used this method var

Hi all,

I used this method

var myFile = new Rho.RhoFile(Rho.File.join(Rho.Application.userFolder,"myfilename.txt"),<file open mode>);

to create a file. but getting uninitialized constant Rho:RhoFile error in tc55 device. Please advise how to resolve this issue.

Actually I want to create a file TC55 but getting permission denied error at "/internal/download/" I tried in SDCard also same error. but its working in emulator please help me to solve this problem.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Kutir Mobility
Hi Could you please create a

Hi

Could you please create a new thread with your question.

Visnupriya R

Kutir Mobility

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments