0

I have the following code to save an Object to file, the problem is I want to save it as CP1252 and as an object, But it only lets me save as String

        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_path), "Cp1252"));
        output.write(object_root.toString());
        output.close();

Saving as String gives me problem when loading the Object, that is an ArrayList<StringBuffer>, to process its content

When I load the object from file it gives me a String, and I cant figure a way to make it an ArrayList<StringBuffer> again, I tried:

        BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file_path), "Cp1252"));
        Object object_root = input.read();
        String object_root_str = object_root.toString();
        String[] array = object_root_str.split(".,");
        ArrayList<String> arrayString = new ArrayList<String>(Arrays.asList(array));

and then

ArrayList<StringBuffer> arraySB = (ArrayList<StringBuffer>)arrayS; 

but it says it can't be converted

I need to know if there's a way to save my ArrayList<StringBuffer> using BufferedWriter (because i need it to be CP1252) as an object or an ArrayList<StringBuffer>. Or I need to find a way to get my saved String to be a ArrayList<StringBuffer> again

  • Please don't use `StringBuffer` as it was replaced by `StringBuilder` more than ten years ago. Why can't you use the code above with a `List`, it looks fine to me? i.e. don't cast it as there is no apparent reason to do so. Note: Java doesn't support converting of objects. You have to do the conversion yourself. – Peter Lawrey Jul 10 '16 at 21:15
  • I'll be dealing with a lot of strings and my computer has limited memory. Since Strings are immutable (everytime you change it, it creates another one) I figured using StringBuffer would be lighter. @peter – Mário Garcia Jul 10 '16 at 21:18
  • Here is a test I would like to try out first before actually writing the content to a file. Could you convert the object to string and string back to object and see if it's intact? Something tells me writing it to a file is altering it's content. Let me know – java_doctor_101 Jul 10 '16 at 21:20
  • The way I was doing before this post was: Convert `ArrayList` to String, then save to file. Load String as Object and tried to parse Object to `ArrayList`. but I get this error "java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.ArrayList" @niti – Mário Garcia Jul 10 '16 at 21:26
  • You keep talking about abot saving objects, but what you are actually doing is saving strings. If you're constrained to do the latter, by having to use CP1252, you will have to *parse* these strings to reconstitute these objects. There's nothing in Java that will magically do that for you. Or else you are looking for Object Serialization of some sort. At present you're just reading a single byte, receiving a Byte, and expecting to be able to magically split that into numerous array elements. It doesn't make sense. – user207421 Jul 10 '16 at 22:32
  • @MárioGarcia You are already adding one transformation without any clear reason to do so, in your example above using a `StringBuffer` just makes your code needlessly complicated. If you are worried about memory, you should read and process your data progressively and you will use fraction of the memory you are using now. – Peter Lawrey Jul 11 '16 at 08:17

0 Answers0