Code sample: Creating a file

import javax.microedition.io.*;
import java.io.IOException;
import javax.microedition.io.file.*;
import net.rim.device.api.system.Application.*;

public class CreateFileApp extends Application 
{
   public static void main(String[] args) 
   {
      CreateFileApp app = new CreateFileApp();
      app.setAcceptEvents(false);
      try 
      {
          FileConnection fc = 
               (FileConnection)Connector.open("file:///store/home/user/newfile.txt");
          // If no exception is thrown, the URI is valid but the file might not exist.
          if (!fc.exists())
          {
              fc.create();  // create the file if it doesn't exist
          }
          fc.close();
       }
       catch (IOException ioe) 
       {
          System.out.println(ioe.getMessage() );
       }
  }
}

Was this information helpful? Send us your comments.