×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Creating Text file in N70

    Hi,
    Can any one tell me how to create text file using j2me . I found that when i am using E:/pimlog.txt as my url and try to create a file it gives me Security Exception and IlllegalMode Exception . I am just able to read .
    Thanks in advance.
    BR.

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    8

    Re: Creating Text file in N70

    Hi,

    The info you have provided is not enough. Could you try and paste your code here.

    SecurityException is when you are trying to access a restricted folder.

    IllegalModeException is given when you are trying to write into a file but have opened the connection in READ mode.
    ~ CSB ~

  3. #3
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Re: Creating Text file in N70

    public class DisplayScreen extends Form implements ItemCommandListener , Runnable
    {
    /** Creates a new instance of DisplayScreen */
    private final Command exitCommand = new Command("<", Command.ITEM, 0);
    private final Command selCommand = new Command("<", Command.ITEM, 0);
    public String s = "";
    private final PIMDemo midlet;
    public TextField tf = null , tf1=null;
    public String mobnum = " ";
    StringBuffer results;
    boolean click = false ;
    String fURL = "file:///memory card/pimlog.txt";
    public FileConnection fc;
    public DisplayScreen(PIMDemo midlet)
    {
    super(null);
    results = new StringBuffer();


    click = false;
    Image img = null , img1 = null;
    try
    {
    img = Image.createImage("/ok.gif");
    img1 = Image.createImage("/icons/demo.png");
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }
    this.midlet = midlet;
    append(new ImageItem(null, img1,ImageItem.LAYOUT_CENTER, null,ImageItem.PLAIN));

    tf1 = new TextField("User ID:" ,null , 15 , TextField.NUMERIC);
    tf1.setLayout(tf.LAYOUT_CENTER);

    tf = new TextField("PIN Number:" ,null , 15 , TextField.NUMERIC);
    tf.setLayout(tf.LAYOUT_CENTER);
    append(tf1);
    append(tf);


    ImageItem it = new ImageItem(null, img,ImageItem.LAYOUT_CENTER, null,ImageItem.PLAIN);
    append(it);
    new Thread(this).start();
    //it.addCommand(newCommand);
    it.setDefaultCommand(exitCommand);
    it.setItemCommandListener(this);
    }

    public String getMobnum()
    {
    return mobnum;
    }




    public void commandAction(Command command, Item item)
    {
    if(command == selCommand)
    {
    DisplayAlrt();
    }
    else
    {
    click = true;
    new Thread(this).start();
    }
    }

    private void DisplayAlrt() {
    Alert a = new Alert("", "Thank You.Your Will recieve your PIN through SMS.", null, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a);
    }




    private String validatePIN()
    {
    String URL = "http://www.magwebonline.com/sushma/validation.aspx?pin1="+tf1.getString()+"&pin2="+tf.getString();
    append(tf1.getString());
    append(tf.getString());
    String str = "";


    HttpConnection conn = null;

    try {

    StringBuffer results = new StringBuffer();
    conn = (HttpConnection) Connector.open(URL);
    InputStream is = conn.openInputStream();
    append("connecting please wait");
    DataInputStream dataInputStream = new DataInputStream(is);
    // retrieve the contents of the requested page from Web server
    int inputChar;
    while ( (inputChar = dataInputStream.read()) != -1) {
    results.append((char) inputChar);
    append("Reading data");
    }
    System.out.println(results.toString());
    str = results.toString();
    append(str);
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    return str;
    }

    public void run() {
    Image image = null , image1 = null , img2 = null;

    if(click == false)
    {
    try {
    try {
    fc = (FileConnection) Connector.open(fURL, Connector.READ);

    append("hi");
    } catch (Exception ex) {
    ex.printStackTrace();
    append(ex.toString());
    }
    append("Connection Open for Reading");

    if(fc.exists())
    {
    append("file Reading ");
    InputStream dis = fc.openInputStream();

    DataInputStream dataInputStream = new DataInputStream(dis);

    // retrieve the contents of the requested page from Web server
    int inputChar;
    while ( (inputChar = dataInputStream.read()) != -1)
    {
    results.append((char)inputChar);
    }
    append("The data in file is :"+results.toString());
    tf1.setString(results.toString().substring(0 ,results.toString().indexOf(",")));
    tf.setString(results.toString().substring(results.toString().indexOf(",")+1));
    dis.close();
    fc.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    else
    {
    try
    {
    midlet.checkPermission("javax.microedition.io.Connector.file.write");
    try {
    fc = (FileConnection) Connector.open(fURL);
    midlet.checkPermission("javax.microedition.io.Connector.file.write");
    append("hi");
    } catch (Exception ex) {
    ex.printStackTrace();
    append(ex.toString());
    }
    append("Connection Open");
    mobnum = tf.getString();

    if((mobnum.equals("") != true) && (tf1.getString().equals("") != true))
    {
    // String status = validatePIN();
    String status = "true";

    try {
    image = Image.createImage("/welcome.gif");
    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }
    // mobnum = mobnum.trim();
    if(status.equals("true")== true)
    {
    append("File created1 0");
    if(!fc.exists())
    {
    append("File created 1");
    try
    {
    fc.create();
    append("File created");
    OutputStream os = fc.openOutputStream();
    os.write(tf1.getString().getBytes());
    os.write(",".getBytes());
    os.write(tf.getString().getBytes());
    append("Data written");
    System.out.println("file not created");
    os.close();

    }
    catch(Exception e)
    {
    append(e.toString());
    }
    }
    /* midlet.logged = true;
    Displayable screen = new LoggedUser(midlet , mobnum);
    Alert a = new Alert("Logged"," You Have Successfully Logged In.... ",image, AlertType.INFO);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, screen);*/

    }
    else
    {

    try {
    image = Image.createImage("/sad.gif");

    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }
    Alert a = new Alert("", "Enter a valid 6-digit PIN number", image, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, DisplayScreen.this);

    }
    }
    else
    {
    try {
    image = Image.createImage("/sad.gif");
    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }



    Alert a = new Alert("", "please enter your PIN number to proceed", image, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, DisplayScreen.this);

    }
    }
    catch (Exception ex)
    {
    //midlet.reportException(ex, this);
    }

    }
    }

    }

  4. #4
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Re: Creating Text file in N70

    Quote Originally Posted by csbhushan View Post
    Hi,

    The info you have provided is not enough. Could you try and paste your code here.

    SecurityException is when you are trying to access a restricted folder.

    IllegalModeException is given when you are trying to write into a file but have opened the connection in READ mode.
    public class DisplayScreen extends Form implements ItemCommandListener , Runnable
    {
    /** Creates a new instance of DisplayScreen */
    private final Command exitCommand = new Command("<", Command.ITEM, 0);
    private final Command selCommand = new Command("<", Command.ITEM, 0);
    public String s = "";
    private final PIMDemo midlet;
    public TextField tf = null , tf1=null;
    public String mobnum = " ";
    StringBuffer results;
    boolean click = false ;
    String fURL = "file:///memory card/pimlog.txt";
    public FileConnection fc;
    public DisplayScreen(PIMDemo midlet)
    {
    super(null);
    results = new StringBuffer();


    click = false;
    Image img = null , img1 = null;
    try
    {
    img = Image.createImage("/ok.gif");
    img1 = Image.createImage("/icons/demo.png");
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }
    this.midlet = midlet;
    append(new ImageItem(null, img1,ImageItem.LAYOUT_CENTER, null,ImageItem.PLAIN));

    tf1 = new TextField("User ID:" ,null , 15 , TextField.NUMERIC);
    tf1.setLayout(tf.LAYOUT_CENTER);

    tf = new TextField("PIN Number:" ,null , 15 , TextField.NUMERIC);
    tf.setLayout(tf.LAYOUT_CENTER);
    append(tf1);
    append(tf);


    ImageItem it = new ImageItem(null, img,ImageItem.LAYOUT_CENTER, null,ImageItem.PLAIN);
    append(it);
    new Thread(this).start();
    //it.addCommand(newCommand);
    it.setDefaultCommand(exitCommand);
    it.setItemCommandListener(this);
    }

    public String getMobnum()
    {
    return mobnum;
    }




    public void commandAction(Command command, Item item)
    {
    if(command == selCommand)
    {
    DisplayAlrt();
    }
    else
    {
    click = true;
    new Thread(this).start();
    }
    }

    private void DisplayAlrt() {
    Alert a = new Alert("", "Thank You.Your Will recieve your PIN through SMS.", null, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a);
    }




    private String validatePIN()
    {
    String URL = "http://www.magwebonline.com/sushma/validation.aspx?pin1="+tf1.getString()+"&pin2="+tf.getString();
    append(tf1.getString());
    append(tf.getString());
    String str = "";


    HttpConnection conn = null;

    try {

    StringBuffer results = new StringBuffer();
    conn = (HttpConnection) Connector.open(URL);
    InputStream is = conn.openInputStream();
    append("connecting please wait");
    DataInputStream dataInputStream = new DataInputStream(is);
    // retrieve the contents of the requested page from Web server
    int inputChar;
    while ( (inputChar = dataInputStream.read()) != -1) {
    results.append((char) inputChar);
    append("Reading data");
    }
    System.out.println(results.toString());
    str = results.toString();
    append(str);
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    return str;
    }

    public void run() {
    Image image = null , image1 = null , img2 = null;

    if(click == false)
    {
    try {
    try {
    fc = (FileConnection) Connector.open(fURL, Connector.READ);

    append("hi");
    } catch (Exception ex) {
    ex.printStackTrace();
    append(ex.toString());
    }
    append("Connection Open for Reading");

    if(fc.exists())
    {
    append("file Reading ");
    InputStream dis = fc.openInputStream();

    DataInputStream dataInputStream = new DataInputStream(dis);

    // retrieve the contents of the requested page from Web server
    int inputChar;
    while ( (inputChar = dataInputStream.read()) != -1)
    {
    results.append((char)inputChar);
    }
    append("The data in file is :"+results.toString());
    tf1.setString(results.toString().substring(0 ,results.toString().indexOf(",")));
    tf.setString(results.toString().substring(results.toString().indexOf(",")+1));
    dis.close();
    fc.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    else
    {
    try
    {
    midlet.checkPermission("javax.microedition.io.Connector.file.write");
    try {
    fc = (FileConnection) Connector.open(fURL);
    midlet.checkPermission("javax.microedition.io.Connector.file.write");
    append("hi");
    } catch (Exception ex) {
    ex.printStackTrace();
    append(ex.toString());
    }
    append("Connection Open");
    mobnum = tf.getString();

    if((mobnum.equals("") != true) && (tf1.getString().equals("") != true))
    {
    // String status = validatePIN();
    String status = "true";

    try {
    image = Image.createImage("/welcome.gif");
    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }
    // mobnum = mobnum.trim();
    if(status.equals("true")== true)
    {
    append("File created1 0");
    if(!fc.exists())
    {
    append("File created 1");
    try
    {
    fc.create();
    append("File created");
    OutputStream os = fc.openOutputStream();
    os.write(tf1.getString().getBytes());
    os.write(",".getBytes());
    os.write(tf.getString().getBytes());
    append("Data written");
    System.out.println("file not created");
    os.close();

    }
    catch(Exception e)
    {
    append(e.toString());
    }
    }
    /* midlet.logged = true;
    Displayable screen = new LoggedUser(midlet , mobnum);
    Alert a = new Alert("Logged"," You Have Successfully Logged In.... ",image, AlertType.INFO);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, screen);*/

    }
    else
    {

    try {
    image = Image.createImage("/sad.gif");

    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }
    Alert a = new Alert("", "Enter a valid 6-digit PIN number", image, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, DisplayScreen.this);

    }
    }
    else
    {
    try {
    image = Image.createImage("/sad.gif");
    }
    catch (IOException ex)
    {
    System.out.println(ex);
    ex.printStackTrace();
    return;
    }



    Alert a = new Alert("", "please enter your PIN number to proceed", image, AlertType.ERROR);
    a.setType(AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(midlet).setCurrent(a, DisplayScreen.this);

    }
    }
    catch (Exception ex)
    {
    //midlet.reportException(ex, this);
    }

    }
    }

    }
    This is my code

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    8

    Re: Creating Text file in N70

    The code seems to be ok.
    I presume this is the part where you are getting the error.
    try {
    fc = (FileConnection) Connector.open(fURL);
    midlet.checkPermission("javax.microedition.io.Connector.file.write");
    append("hi");
    } catch (Exception ex) {
    ex.printStackTrace();
    append(ex.toString());
    }
    Can you tell when it actually happens? rather than the whole code. Other things would not cause file errors. ;-)
    Are you sure you have memory card ready in emulator?
    Creating e beside c folder would do the trick.
    ~ CSB ~

  6. #6
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Re: Creating Text file in N70

    Quote Originally Posted by csbhushan View Post
    The code seems to be ok.
    I presume this is the part where you are getting the error.

    Can you tell when it actually happens? rather than the whole code. Other things would not cause file errors. ;-)
    Are you sure you have memory card ready in emulator?
    Creating e beside c folder would do the trick.
    It gives me error when i try to create a file.I did not understand Creating e beside c folder would do the trick.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    8

    Re: Creating Text file in N70

    The emulator should be able to map your call to memory card to some folder in the file system.
    For c:, there is a folder present, called c.
    Similarly if you want e:, you must have a folder called e.

    Probably this is the reason why you are not able to create the file.
    ~ CSB ~

  8. #8
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Re: Creating Text file in N70

    Quote Originally Posted by csbhushan View Post
    The emulator should be able to map your call to memory card to some folder in the file system.
    For c:, there is a folder present, called c.
    Similarly if you want e:, you must have a folder called e.

    Probably this is the reason why you are not able to create the file.
    I am testing it in mobile not in emulator. In emulator it works fine.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    8

    Re: Creating Text file in N70

    Quote Originally Posted by susrap View Post
    I am testing it in mobile not in emulator. In emulator it works fine.
    In that case, it must work fine on device too. I dont see any reason why it should not work.
    I do not know if you can restrict access to memory card.
    ~ CSB ~

  10. #10
    Nokia Developer Champion
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,192

    Re: Creating Text file in N70

    First of all, the MIDlet.checkPermission returns an integer.

    "0 if the permission is denied; 1 if the permission is allowed; -1 if the status is unknown"

    You never seem to check the result....

    Try to open it in some folder and not at the root of the card.

    Hartti

  11. #11
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    62

    Re: Creating Text file in N70

    Quote Originally Posted by hartti View Post
    First of all, the MIDlet.checkPermission returns an integer.

    "0 if the permission is denied; 1 if the permission is allowed; -1 if the status is unknown"

    You never seem to check the result....

    Try to open it in some folder and not at the root of the card.

    Hartti
    Thanks a lot hartti for your suggestion . This will really help me for my future projects .But for now with RMS i solved the problem . Instead of file i used RMS.
    BR
    Sushma.

Similar Threads

  1. Help needed with a 3gp file...
    By joedoe_1981 in forum Streaming and Video
    Replies: 0
    Last Post: 2007-07-18, 18:58
  2. write body text to file
    By aden2004 in forum Symbian
    Replies: 3
    Last Post: 2007-03-26, 22:01
  3. Error Carbide C++ creating the SIS file
    By Naco in forum Symbian
    Replies: 3
    Last Post: 2007-03-14, 22:57
  4. Replies: 1
    Last Post: 2005-05-26, 14:22
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •