×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    97

    Question How to take file data in s String using Java and FileConnection API?

    Hello All,
    I faced problem while I was trying to store whole file data of a text file in a String data and displaying it on a text box. I used the FileConnection API to open the file and got the InputStream to it and then I read some bytes from that stream and converting those bytes to String data. But nothing is shown on screen...plz tell me how to get all the file data in a string variable?
    Here is the code--

    FileConnection fc = (FileConnection)Connector.open("file:///E:/TestXML.txt");
    if (fc.exists()) {
    InputStream is = fc.openInputStream();
    byte b[] = new byte[82];
    int length = is.read(b, 0, 82);
    String Str = new String(b);

    the file contains 82 bytes of data.

    Actually i want to parse the file data and want to create a xml file depending on some values in that file..for that i need to parse the file data...

    plz give some pointers/code snippets to get rid of this..since i m stuck..
    thanks a lot.
    regards,
    sanjay

  2. #2
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: How to take file data in s String using Java and FileConnection API?

    check this snippet out....


    InputStream iStream = Connector.openInputStream("http://java.sun.com/");

    byte[] buffer = new byte[255];

    int length = iStream.read(buffer);

    if(length > 0)
    {
    iStream.close();

    String webString = new String(buffer, 0, bytesRead);
    }
    Made With Caffiene And Sleepless Nights

  3. #3
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    97

    Re: How to take file data in s String using Java and FileConnection API?

    Many thanks buddy, I got the problem solved..Abt the other part, I want to create a file in the same directory mentioned in the code snippet and write some text data to it. Plz tell me how can i do it using FileConnection API? Does that give any method to write data to file?
    regards,
    sanjay

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: How to take file data in s String using Java and FileConnection API?


Similar Threads

  1. Replies: 2
    Last Post: 2007-02-06, 15:16
  2. FileConnection API with new Firmware
    By cburstow in forum Mobile Java General
    Replies: 3
    Last Post: 2006-03-29, 18:40
  3. Java FileConnection Optional Package 1.0 (JSR 75)
    By thecrowi in forum Mobile Java General
    Replies: 0
    Last Post: 2004-09-24, 08:29
  4. getting Real file with Java (file i/o)
    By ivanxqz in forum Mobile Java General
    Replies: 0
    Last Post: 2004-04-11, 18:59

Posting Permissions

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