Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

where should i create file?

hi,

my application must create a file at the beginning. i'm wondering-which path should i use to create a file? no every device has a memory card. is there smth like 'C', for example:

 

 _fc = (FileConnection)
                    Connector.open("C:\"+_fileName, Connector.READ_WRITE);

 

or where should i create it-so that there won't be a problem on any device?

 

 kind regards

Highlighted
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: where should i create file?

ok, i've found that it should be a path like:

 

 _fileConnection = (FileConnection)

Connector.open("file:///store/home/user/"+_fileName);

 

it seems that the file was created. but now i'm on my device (bb curve 8300) and i would like to find the file! where could i?

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: where should i create file?

[ Edited ]

misty83 wrote:
where should i create it-so that there won't be a problem on any device?

You can programatically check if the device have external memory(SDCard).

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/1295814/How_To...

 

If the device don't have external memory you can save your file in internal memory.

In that case make sure that device has available space for saving that file. You will get that information from MemoryStats Class.

 

For saving file in SDCard use  something like:

FileConnection fc = (FileConnection) Connector.open(System.getProperty("file:///SDCard/"+"fileName.txt"),Connector.READ_WRITE);

 

if(!fc.exists())
{
fc.create();
}

OutputStream os =fc.openOutputStream();
os.write("Test");
os.close();
fc.close()

 

Also check this link for more information about file path.

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/What_Is...

 

 

Regards

Bikas

Message Edited by bikas on 06-20-2009 06:15 PM
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: where should i create file?

thx!

 

ps.

i also realized that i can found created file using fileConnection.getList()

 

 

 kind regards

New Developer
Posts: 17
Registered: ‎08-27-2009
My Device: Not Specified

Re: where should i create file?

Hi,

 

I've already work through the process of creating file into my hard drive for testing the file operation using file Connection. It seem to me you are expert in this section. When i am try to create a file into my project folder using the following code. It is not working. In eclipse how to create a file and write some to it? 

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
public class FileCreate {

    /**
     * @param args
     */
    public void CreateFile() {
       
        // TODO Auto-generated method stub
       
        String fullPath = "file:////foo.txt";
        byte[] data = "foo".getBytes();

        try {
            FileConnection fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);

            if (!fconn.exists()){
                System.out.println("Fconn not Open!");

                fconn.create(); // create the file if it doesn't exist

                }

                fconn.setWritable(true);

            OutputStream os = fconn.openOutputStream();

            os.write(data);

            os.close();

            fconn.close();
        }
        catch (IOException e) {
            System.out.println("Oh noes!!1! " + e.toString());
        }

    }

}

 

And here is my Workspace structure:

 

| TEST

  |src

   | FileCreate.java

   | Mainscreen.java

  | NET_RIM_ BLACKBERRY

 

 

Please give me a solution if possible... I am now in a bad situation...

 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: where should i create file?

creating a file:

 

 

public void createMyFile(String _fileName){ try{ _fileConnection = (FileConnection) Connector.open("file:///store/home/user/"+_fileName, Connector.READ_WRITE); if( _fileConnection.exists() == false ){ _fileConnection.create(); } }catch(Exception e){ } }

 

 

reading from file:

 

public void readFromFile(String _fileName){ _stringBuffer = new StringBuffer(); int _c; DataInputStream _dis = null; FileConnection _fileConnection; try{ _fileConnection = (FileConnection) Connector.open("file:///store/home/user/"+_fileName, Connector.READ_WRITE); _dis = _fileConnection.openDataInputStream(); while( (_c = _dis.read()) != -1){ _stringBuffer.append((char)_c); } _dis.close(); //close file _fileConnection.close(); }catch(Exception e){_fileConnection.close(); } }//end of readFromFile() method

 

 

writing to a file:

 

public void writeToFile(String _s){ DataBuffer _db = new DataBuffer(); _db.writeChars(_s); try{ _fileConnection = (FileConnection) Connector.open("file:///store/home/user/"+_fileName, Connector.READ_WRITE); _dos = _fileConnection.openOutputStream(); _dos.write(_db.getArray(), 0, _db.getArray().length); _dos.flush(); _dos.close(); //close file _fileConnection.close(); }catch(Exception e){ _fileConnection.close()} }//end of writeToFile() method

 

 

hope that helps,

           regards

New Developer
Posts: 17
Registered: ‎08-27-2009
My Device: Not Specified

Re: where should i create file?

Thanks a lot. This will really help me a lot...
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: where should i create file?

actually, it would be better if You add a "finally" - to try-catch block in reading and writing method to close connection and streams properly:

 

try{ }catch(Exception e){} finally{ if( _dos != null){ try{ _dos.close(); } catch(Exception e){} } if( _fileConnection != null){ try{ _fileConnection.close(); }catch(Exception e){} } }

 

 

and do the same for _dis in reading method, instead of closing it as i presented before. this will asure You that You won't have "too many open connections to file" error.

 

 

  regards

New Developer
Posts: 17
Registered: ‎08-27-2009
My Device: Not Specified

Re: where should i create file?

Thanks for your valuable reply. Writing into file is easy now. But will you please help me a bit more. I just want to append new string into file. Here what happen is older string is overwritten by newer one. I wnat to append this line by line. What should i do then?

 

Another thing i think _fileConnection.truncate(0) or _fileConnection.delete() is ok for deleteing individual line. What you think?

 

Please send me your valuable suggestion. 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: where should i create file?

if You want to add line after line, You must do 2 things:

 

first, add "\n" to the end of Your string. so that we know that we will write line after line, not string next to string, i mean,You'll get:

 

ABC

DEF

 

instead of ABCDEF

 

also, You must open the stream with a parameter-offset, where this offset is the size of Your file:

 

_dis = _fileConnection.openDataInputStream( _fileConnection.fileSize());

 

 

 

regards

regards