×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    Print byte array as string array in J2ME

    In my mobile application , i am reading a text file using byte array , that file contains

    Add.java
    Scan.txt
    Vir.txt

    i am storing this data in a string array(TotalFiles) i want to print the contains of file line by line(if i print TotalFiles[1], it should print Add.java) but it prints null, if i print TotalFiles[0] it prints Add.java Scan.java Vir.txt
    , why it is not printing TotalFiles[1]=Scan.txt ,i am new to J2ME , any help would be great. my code is here

    try {
    String updateFile="file:///root1/rs/update.txt";
    FileConnection textFile = (FileConnection) Connector.open(updateFile,Connector.READ);
    getTextBox().setString("");
    InputStream fis = textFile.openInputStream();
    byte[] b = new byte[1024];
    length = fis.read(b, 0, 1024);
    fc++;
    fis.close();
    if (length > 0) {
    textBox.setString(new String(b, 0, length));
    TotalFiles[length]=new String(b,0,length);
    String h=new String(b);
    TotalFiles[TotalFileCount]=h;
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    System.out.println(TotalFiles[2]);

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Print byte array as string array in J2ME

    Java InputStream.read does not split strings by line breaks or anything. It is actually reading a byte array, so it does not even care about that the file actually contains text or binary data.
    It is up to you to split 'h' along the line breaks, String.indexOf and String.substring are the methods which you may find useful.
    Note that indexing of arrays starts with 0, so the 1st line (Add.java) will end up in TotalFiles[0] at the end.

  3. #3
    Regular Contributor
    Join Date
    Dec 2012
    Location
    India
    Posts
    271

    Re: Print byte array as string array in J2ME

    If you are trying to open and display the contents of a file which will be included in the root of your JAR,try this approach.
    Code:
    try {
                int chars, i = 0;
                InputStream is = getClass().getResourceAsStream("/Scan.txt");
                StringBuffer sb = new StringBuffer();
    
    
                while ((chars = is.read()) != -1) {
                    {
                        // System.out.print((char)chars);
                        sb.append((char) chars);
                    }
             catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(sb.toString());

Similar Threads

  1. byte array and array object!!
    By divi11 in forum Symbian
    Replies: 7
    Last Post: 2012-05-24, 17:46
  2. how to convert byte array to string
    By murugacse4 in forum Mobile Java General
    Replies: 2
    Last Post: 2011-04-17, 07:56
  3. RecordStore Byte Array to String
    By gsarmentero in forum Mobile Java General
    Replies: 6
    Last Post: 2010-09-23, 13:10
  4. byte array / string / sha 1 problem
    By nuker85 in forum Mobile Java General
    Replies: 1
    Last Post: 2008-02-28, 07:45
  5. howto convert byte array to string???
    By NFC_Man in forum Near Field Communication
    Replies: 3
    Last Post: 2007-07-05, 03:38

Posting Permissions

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