File handling in blackberry

I am reusing some android code into my blackberry application. There is one line of code like this

File f = new File(cacheDir, filename); 

where cacheDir is a File & filename is a string. But when implementing this same line in blackberry i get error

"The constructor File(File, String) is undefined".

Can anyone help me out.

UPDATE

Another error which i am facing is for this line

OutputStream os = new FileOutputStream(f);

where f is the instance of FileConenction stream. The error says

"The constructor FileOutputStream(FileConnection) is undefined"

can anyone help?

Upvotes: 1

Views: 1240

Answers (2)

npinti
npinti

Reputation: 52185

You have to use

Connector.open("file://" + dirName);

More info available here

You can try this:

OutputConnection connection = (OutputConnection)                     
    Connector.open("file://c:/myfile.txt;append=true", Connector.WRITE );
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream( out );

output.println( "This is a test." );

out.close();
connection.close();

Taken from here

Upvotes: 1

Vit Khudenko
Vit Khudenko

Reputation: 28418

Usual File Java API does not work on BB.

See BB API documentation for javax.microedition.io.Connector and javax.microedition.io.file.FileConnection.

You will need to do something like:

FileConnection fconn = (FileConnection) Connector.open("file:///CFCard/newfile.txt");

// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists()) fconn.create(); // create the file if it doesn't exist

OutputStream os = fconn.openOutputStream();

...

fconn.close();

Upvotes: 2

Related Questions