×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    How to read data from a file?

    How to read data from a file? We cannot use FileInputStream and FileOutputStream.
    I want to read data from a file and display data in a TextFieldBox.
    The following program does not work.

    Code:
    byte[] data;
    			fileconnection=(FileConnection)Connector.open(path+filedir);
    			System.out.println(path+filedir);
    			datainputstream=fileconnection.openDataInputStream();
    			/*String data=datainputstream.readUTF();
    			TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.txtfield.setString(data);*/
    			data=new byte[datainputstream.available()];
    			datainputstream.read(data,0,data.length);							
    			TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.txtfield.setString(new String(data));?

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    13

    Re: How to read data from a file?

    Hi
    Use this

    private String readHelpText(FileConnection file)
    {
    InputStream is = file.openInputStream();
    try
    {
    StringBuffer sb = new StringBuffer();
    int chr, i = 0;
    // Read until the end of the stream
    while ((chr = is.read()) != -1)
    sb.append((char) chr);

    return sb.toString();
    }
    catch (Exception e)
    {
    System.out.println("Unable to create stream");
    }
    return null;
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to read data from a file?

    Read Char wise is slow at times, so its recommended to read the InputStream in chunks for better performance.

    Code:
    private String readHelpText(FileConnection file)
    {
    InputStream is = file.openInputStream();
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    try 
    {
                byte[] buffer = new byte[1024]; // Chunk size
                int bytesRead = 0;
                while ((bytesRead = in.read(buffer)) != -1) {
                    baos.write(buffer, 0, bytesRead);
                };
    
    return baos.toString();
    }
    catch (Exception e)
    { 
    System.out.println("Unable to create stream");
    }
    return null;
    }
    Thanks,
    Ekta

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    13

    Re: How to read data from a file?

    Its only read 1024 bytes only... if my file is large then 1024 then ...in this case my approach is working .

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to read data from a file?

    Quote Originally Posted by devdhar.s View Post
    Its only read 1024 bytes only... if my file is large then 1024 then ...in this case my approach is working .
    It does't read only 1024 bytes, but It reads in Chunks of 1024 bytes see the while loop at a time and write that chunk to baos, the while loop ends at EOF and then its converted to String to return; thus its faster then reading char by char, and it works for large data better.

    while ((bytesRead = in.read(buffer)) != -1) {
    baos.write(buffer, 0, bytesRead);
    };
    Thanks,
    Ekta

  6. #6
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: How to read data from a file?

    The way of writing as ektasrv work. But I don't understand how it work. And it does not read the next line. ( I mean it read only the sentences those are written in the same line)
    How it check whether it is the end of the file if it reads in Chunks?

    And in the way what devdhar.s said, can we do type casting from int to char?

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    13

    Re: How to read data from a file?

    yes you can typecast int to char. its is valid

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to read data from a file?

    Quote Originally Posted by komomo View Post
    The way of writing as ektasrv work. But I don't understand how it work. And it does not read the next line. ( I mean it read only the sentences those are written in the same line)
    How it check whether it is the end of the file if it reads in Chunks?
    By while ((bytesRead = in.read(buffer)) != -1) we check for end of file also, this while loop keeps on reading the in (InputStream) till it does not return -1 which means EoF.

    See API doc about in.read(byte[] b)
    read


    public int read(byte[] b)
    throws IOException
    Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.
    If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

    The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

    If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.

    Reading in Chunks is very good in performance while reading large data from server as compared to reading it character by character.
    Thanks,
    Ekta

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    12

    Re: How to read data from a file?

    Same problem here. I cannot read a file completely. kOkmo, let me know if you have got that.

  10. #10
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: How to read data from a file?

    My program reads only one line.

    And it just works for .txt files.
    How to read data from .doc files and .pdf files? The contents are just characters, but the program does not wrok for that type of file.

    There are many types of files such as .PNG, .Zip and so on.
    We have to write different coding for every kind of file?

  11. #11
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: How to read data from a file?

    The program does not display the next line because I used the TextField to display the content of the file. hahaha
    The TextField does not show the next line.
    When I use an Alert, it is ok.

    But how to read other file types?

  12. #12
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to read data from a file?

    Quote Originally Posted by komomo View Post
    But how to read other file types?
    Are you having trouble READING other file types, or trouble displaying what you've read? You can't expect a zip file to be displayable.

  13. #13
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: How to read data from a file?

    may be I don't know how to display.
    I know how to find the contents of a directory. ( For example, after choosing the "content" command" , the contained files and dir of the selected directory will be displayed.
    I want to show to open the files( if I choose .txt file, the text must be displayed. if I choose .png file, the image must be displayed)
    I don't know how to do that.
    only .txt file is working in my program.

  14. #14
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: How to read data from a file?

    Why dont you get the name/type of the file you want to search?
    have this, liek the input from the user to search with.

    Also read the full name of the files with the extensions and then you can metch the entered extension with the all files and if it metches then you can get the list and display the same.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  15. #15
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: How to read data from a file?

    I am sorry.
    I don't get what u said.

    I get the full path( the whole url strart from file:/// )
    but I cann't diplay all the files types
    For .txt file, it works. I display that file in the alert. For example, if the file I choose is a.txt and it includes the text" hello", the alert box will show "hello".
    For other files, such as .png file, it show small rectangles and others.

    Please help me.
    I am really in trouble.

Similar Threads

  1. How to read/write data file ?
    By roi_d in forum Symbian
    Replies: 2
    Last Post: 2009-02-27, 22:09
  2. Replies: 3
    Last Post: 2008-03-04, 11:22
  3. How to Read data from file Dynamically?
    By mayur2007 in forum Symbian
    Replies: 1
    Last Post: 2007-03-09, 18:30
  4. data file read and write
    By toinks in forum Symbian
    Replies: 28
    Last Post: 2006-11-07, 08:27
  5. Can not read data from the file (InputStream)
    By ramis55 in forum Mobile Java General
    Replies: 2
    Last Post: 2005-08-25, 17:18

Posting Permissions

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