Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:16:15+00:00 2026-06-16T05:16:15+00:00

I want to create two methods. The one should store and arraylist to the

  • 0

I want to create two methods. The one should store and arraylist to the cache and the other should retrieve the arraylist from the cache. The code that I’ve tried is the following:

public class ByteConverter<T> {

public byte[] write_to_byte_array(ArrayList<T> list,File file){

    // write to byte array
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
        Log.d("file not found", "file not found", e1);
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    for (T element : list) {
        try {
            out.writeUTF((String) element);
        } catch (IOException e) {
            Log.d("Error converting List to byte array", "Error converting List to byte array", e);
        }
    }

    byte[] bytes = baos.toByteArray();

    int len1 = 0;
    while (len1 < bytes.length) {
        try {
            fos.write(bytes, 0, len1);
            len1++;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytes;

}

public String read_to_byte_array(byte[] bytes) throws IOException{

    // read from byte array
    String element = null;
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    DataInputStream in = new DataInputStream(bais);
    while (in.available() > 0) {
        element = in.readUTF();
    }

    return element; 
}

  }

However the aforementioned methods doesn’t write anything to the file. I don’t know why. Can anyone help me? Also I’m taking a ClassCastException at this line:

       out.writeUTF((String) element);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:16:16+00:00Added an answer on June 16, 2026 at 5:16 am

    First, the ClassCastExcetion is the reason you are not getting any output. The exception is thrown before your code writes to the file …

    Second, the ClassCastException is happening because the element is not a String. In theory, you could call element.toString() to get a string representation, but the chances are that that representation will NOT be suitable for your purpose.

    So what is the correct approach?

    Well it depends on the types of the objects in list.

    • If they implement Serializable (or Externalizable) then you should be able to use Java serialization. Read this section of the Java tutorial for more details.

    • Another option might be to use something like JAXP or GSON which can serialize POJOs to XML and JSON respectively.

    • The final fallback is to code the serialization method (and a corresponding serialization method) by hand.


    Finally, I should point out some other errors in your code:

    1) This code is wrong:

    int len1 = 0;
    while (len1 < bytes.length) {
       fos.write(bytes, 0, len1);
       len1++;
    }
    

    This will write:

    bytes[0]
    bytes[0], bytes[1]
    bytes[0], bytes[1], bytes[2]
    
       and so on.  
    

    The correct way to write bytes is:

    fos.write(bytes);
    

    or

    fos.write(bytes, 0 bytes.length);
    

    2) Your exception handling logic is incorrect. In 3 or 4 places you catch an exception, print or log something … and then continue as if nothing had gone wrong. What you should probably do is let the exceptions propagate, and handle them at a higher level.

    3) If something goes wrong in the write_to_byte_array method it is likely to leak a file descriptor. Best practice is to either close resources in a finally block, or use the new Java 7 try-with-resource syntax.

    4) The names of the read_to_byte_array and write_to_byte_array methods violate the accepted Java style guidelines. They should be readToByteArray (or better still readFromByteArray) and writeToByteArray.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create two widgets in flex. One for the movies ( that
I want to create a method that takes two variables timeIn and timeOut and
I want to create a two level ExpandableListView from a local database. The group
I want to create a two-dimensional array in which I want to store records
I want to create a new field (or two) in my table that is
I want to create a class with two methods at this point (I also
I want to create two equal timestamps for a given day in PHP and
I want to create two and three dimensional vectors using a constructor in a
I have a state column in a dataframe and I want to create two
i am preparing my android apps in which i want to create two row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.