1
 public static void writeIntoFile() {
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("Employee.txt");
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(list1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream == null) {
                System.out.println("file is not created");
            }
            if (objectOutputStream == null) {
                System.out.println("cant able to write");
            }
        }
    }

I want to using this function to writing in a file. it writes successfully but it display data in bytecode. how can I save it into string format?

1
  • You have a few choices. You make it so that you can write the properties in the format you want. That's a lot of work and is really messy or you could use Java Architecture for XML Binding that will allow you to read/write object properties in the form of XML Commented Sep 16, 2013 at 6:42

3 Answers 3

3

Use a FileWriter wrapped inside a BufferedWriter to write character data to a File.

ObjectOutputStream is used for serialization and results in a binary encoded file. Its only useful if you only want to load the file through your program and do not wish to read its contents elsewhere like in an external editor.

You also need to iterate through your List and save the requisite properties of your underlying Object in a format you wish to parse your File later on in. For example, as CSV (comma separated values) every Employee object and its properties would be persisted as one single line in the output file.

BufferedWriter br = new BufferedWriter(new FileWriter("Employee.csv"));
for (Employee employee : list) {
    br.write(employee.getFName() + ", " + employee.getLName());
    br.newLine();
}
br.close();
Sign up to request clarification or add additional context in comments.

Comments

0

in the function writeIntoFile is write a Serialization Object into file

you should use the object's toString() to write a String into file

Comments

0

you can change bytecode into string using one simple way. pass the bytecode into string constructor like this:

new String(bytecode object);

and then write string object into file.

4 Comments

If you can provide a runnable example of writing and reading the object, I'll give you an up-vote
FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeObject("Hello World"); oout.writeObject(new Integer(897648764);); // this statement write byte code oout.writeObject(String.valueOf(new Integer(897648764);.intValue())); //this statement write integer value // close the stream oout.close();
So you're not so much writing the Object as String, but properties of the Object??
for properties of the object use object serialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.