Code:
//...
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;
import java.util.*;
public class myClass extends MIDlet implements CommandListener
{
public void ReadFile(String fileUrl)
{
String temp = System.getProperty("microedition.io.file.FileConnection.version");
if ((temp!="")&&(temp!=null))
{
FileConnection fc = null;
try
{
fc = (FileConnection)Connector.open(fileUrl, Connector.READ_WRITE);
if(fc.exists()&&(fc != null))
{
DataInputStream dis = fc.openDataInputStream();
String inStr;
String contents = "";
try
{
while ((inStr = dis.readUTF()) != null )
{
contents = contents + inStr;
}
}
catch(EOFException eofex)
{
/*Alert a = new Alert("end of file reached", eofex.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);*/
switchDisplayable(null, getForm());
}
catch(Exception e)
{
Alert a = new Alert("file read exception", e.getMessage(), null, AlertType.ERROR);
a.setTimeout(5000);
switchDisplayable(a, getForm());
}
DoSomething();
}
else
{
DoSomethingElse();
fc.create();
}
}
catch(Exception e)
{
Alert a = new Alert("Error creating file", e.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
finally
{
try{
fc.close();
}
catch(Exception exr)
{
Alert a = new Alert("error closing read connection", exr.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
}
}
}
public void WriteFile(String fileUrl,String myText)
{
String temp = System.getProperty("microedition.io.file.FileConnection.version");
if ((!temp.equals(""))&&(temp!=null))
{
boolean DebugResume = true;
DataOutputStream dos = null;
try
{
FileConnection fc = (FileConnection)Connector.open(fileUrl, Connector.READ_WRITE);
try{
DebugResume = false;
if (fc.exists())
{
try
{
fc.truncate(0);//exception is thrown here!! <----
}
catch(IOException ioe)
{
Alert a = new Alert("truncating-access denied", ioe.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
catch(ConnectionClosedException cce)
{
Alert a = new Alert("truncating-connection closed", cce.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
catch(SecurityException se)
{
Alert a = new Alert("truncating-security issue", se.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
catch(IllegalModeException ile)
{
Alert a = new Alert("truncating-illegal mode exception", ile.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
catch(IllegalArgumentException iae)
{
Alert a = new Alert("truncating-illegal argument", iae.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
catch(Exception e)
{
Alert a = new Alert("truncating existing file", e.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
}
else
{
try
{
fc.create();
}
catch(Exception e)
{
Alert a = new Alert("Creating file to write", e.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
}
}
catch(Exception ex1)
{
Alert a = new Alert("Error opening fileConection", ex1.getMessage(), null, AlertType.ERROR);
a.setTimeout(50000);
switchDisplayable(a, getForm());
}
try{
dos = new DataOutputStream(fc.openDataOutputStream());
dos.writeUTF(myText);
if (dos != null)
{
dos.close();
}
}
catch(Exception ex2)
{
Alert a = new Alert("Error writing to file", ex2.getMessage(), null, AlertType.ERROR);
a.setTimeout(10000);
switchDisplayable(a, getForm());
}
try{
if (fc.isOpen()) fc.close();
}
catch(Exception ex3)
{
Alert a = new Alert("Error Closing write connection", ex3.getMessage(), null, AlertType.ERROR);
a.setTimeout(50000);
switchDisplayable(a, getForm());
}
}
catch(Exception e)
{
Alert a = new Alert("file exception4", e.getMessage(), null, AlertType.ERROR);
a.setTimeout(50000);
switchDisplayable(a, getForm());
}
}
}
private void Work()
{
try
{
DirAddress = System.getProperty("fileconn.dir.recordings");//where to read/write file
FileAddress = "myfile.txt";
if(DirAddress!=null)
{
FileAddress = System.getProperty("fileconn.dir.recordings")+ FileAddress;
}
}
catch(Exception e)
{
String directory = "X",
FileAddress = "file:///" + getRoots() + Directory+"/"+ FileAddress;
DirAddress = "file:///" + getRoots() + Directory;
}
ReadFile(FileAddress);
DoSth();
WriteFile(FileAddress,myText);
DoSthElse();
}
}