File Connection API

Hey

I am having problem in JSR-75.

I am reading a file from CFCard byte by byte and then write some bytes to a file.

BUT

When I start the midlet,it asks many times for permission but that can be solved by setting permission but the main problem is that after asking many times it then throws java.lang,security exception.

The code is:

try

{

String extension = ".mpg";

String filePath = "file:///CFCard/B.mpg";

//String Filename ="B.mpg";

String destination = "file:///CFCard/";

String name ="B"; //Filename.substring(0, Filename.length()-4) + "000";

byte[] input = new byte[1024];

FileConnection pRead = (FileConnection)Connector.open(filePath);

InputStream is = pRead.openInputStream();

FileConnection pWrite = (FileConnection)Connector.open(destination+name+extension);

OutputStream ot = pWrite.openOutputStream();

int nLen, nCtr=0;

while((nLen = is.read(input))>0)

{

if(nCtr < nMax)

nCtr++;

else

{

nCtr=0;

pWrite.close();

name = nextFilename(name);

pWrite = (FileConnection)Connector.open(destination+name+extension);

ot = pWrite.openOutputStream();

ot.write(input,0,nLen);

}

pRead.close();

pWrite.close();

}

catch(IOException ioe)

{

ioe.printStackTrace();

}

How to set Permissions for a midlet and how to solve the 'exception' problem.

[1500 byte] By [Shujaata] at [2007-11-27 5:08:37]
# 1
I throws security exception at :ot.write(input,0,nLen);
Shujaata at 2007-7-12 10:28:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Hello,

Using the "code" format tags will make your post much more readable. That said, there are some other problems in the aforementioned code. For example -1 signifies the end of a stream and 0 could simply mean no bytes were read.

You should also check exists() and canWrite(). It is possible to be able to read a file and not be able write to it or create a file in the same directory.

rashidmayesa at 2007-7-12 10:28:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

About the permission being asked a lot of times, the only way to solve it is

signing your Midlet. Otherwise, permission will be asked at every file

operation, like "open" or "write", i.e. every method call.

Note that this doesn't happen in every device, some may

ask for file connection permission once, just like http connections.

Danniel_Williana at 2007-7-12 10:28:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...