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: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

create file on the device

hii. how do I create a file on the device, and how I can write to that file, and where I must create it ?is there an API specific?

Highlighted
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: create file on the device

Hi,

For file connection use this code. I have reffered from one book.

/*
 * FileConMain.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */

package FileApp;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.MenuItem;

class FileConMain extends MainScreen
{
    private ObjectListField filelist;
    private String currentpath = "file:///";
    
    FileConMain() 
    {
        setTitle("File Connection");
        filelist=new ObjectListField();
        filelist.set(new String[] {"store/", "SDCard/"});
        add(filelist);                
    }
    protected void makeMenu(Menu menu, int instance) 
    {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Select", 10, 10) 
        {
            public void run() 
            {
                System.out.println("entered run");
                loadFile();
            }
        });
     }
     private void loadFile() 
     {
         currentpath += filelist.get(filelist, filelist.getSelectedIndex());
         System.out.println("entered loadfile");
         try 
         {
            System.out.println("inside try ");
            FileConnection fileConnection = (FileConnection)Connector.open(currentpath);
         
            System.out.println("after connection ");
            if (fileConnection.isDirectory()) 
            {
                System.out.println("dirctory exsist ");
                Enumeration directoryEnumerator = fileConnection.list();
                Vector contentVector = new Vector();
                while(directoryEnumerator.hasMoreElements()) 
                {
                    contentVector.addElement(directoryEnumerator.nextElement());
                }
                String[] directoryContents = new String[contentVector.size()];
                contentVector.copyInto(directoryContents);
                filelist.set(directoryContents);
            }
        } 
        catch (IOException ex) 
        {
        }
     }
} 

to access our document stored in pc, create a folder SDCard in mydocuments.

In JDE, goto edit, preferences,simulator tab and in that memory tab...

check, use pc filesystem for sd card files. and browse ur path of sdcard.

 

This much i have checked and used documentstogo to view text and  word files..

 

This thread tells something about read and write.. i have not checked...

http://stackoverflow.com/questions/1519328/j2me-blackberry-how-to-read-write-text-file

http://stackoverflow.com/questions/2791369/blackberry-read-local-properties-file-in-project

 

check whether is that helpful

 

 

Thanks & Regards

            pp

Developer
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

Re: create file on the device

thanks for reply, but i need to create a file on the device how to do it, i can create on the sdcard, but when try to create on the device "file://store/home/testfile.txt" nothing happens!?
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: create file on the device

Remeber that to open a file it needs to be prefixed with the file protocol handler file:///.
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: create file on the device

private void copyFile() {
// Prompt for the new filename
FileNameScreen screen = new FileNameScreen();
UiApplication.getUiApplication().pushModalScreen(screen);
String newFilename = screen.getFilename();
try {
FileConnection newFileConnection =
(FileConnection)Connector.open(currentPath + newFilename);
if (newFileConnection.exists()) {
Dialog.alert("The file '" + newFilename + "' already exists!");
newFileConnection.close();
return;
}
// The file doesn't exist, so we'll create it
newFileConnection.create();
OutputStream newFileOutputStream = newFileConnection.openOutputStream();
// Open the old file
currentPath += fileList.get(fileList, fileList.getSelectedIndex());
FileConnection fileConnection = (FileConnection)Connector.open(currentPath);
InputStream inputStream = fileConnection.openInputStream();
// Copy the contents of the old file into the new one
byte[] fileContents = new byte[(int)fileConnection.fileSize()];
inputStream.read(fileContents);
newFileOutputStream.write(fileContents, 0, fileContents.length);
inputStream.close();
newFileOutputStream.close();
Dialog.inform("Successfully copied the file!");
} catch (IOException ex) {
}
}

 This method to create a new file and copy contents of old file... you can edit how ever you want.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class FileNameScreen extends PopupScreen implements FieldChangeListener {
private EditField fileNameField;
private ButtonField okButton;
public FileNameScreen() {
super(new VerticalFieldManager());
fileNameField = new EditField("New Filename:", "");
add(fileNameField);
okButton = new ButtonField("OK", ButtonField.CONSUME_CLICK |
Field.FIELD_HCENTER);
okButton.setChangeListener(this);
add(okButton);
}
public String getFilename() {
return fileNameField.getText();
}
public void fieldChanged(Field field, int context) {
if (field == okButton) {
close();
}
}
}

 this is to get the name of file..

 

Thanks & Regards

             pp