1

Suppose I have an object of class Student, which has the following fields:

String name;

int age;

int roll_number;

And I instantiate like so:

Student A[] = new Student();

Now, is there a way to store all this information in a text file using File Handling?

I thought about looping through each element in the array and then converting all the fields into Strings but that seems wrong.

5
  • How do you want the information to be stored in a text file ? I mean can you specify any format ? Commented Jan 6, 2016 at 6:46
  • I guess it should be easy to work with, not binary. But, if there is no way out, then I'll adapt to anything. Commented Jan 6, 2016 at 6:59
  • 1
    If you really want to store data in a string form, try using JSON. You can convert a POJO to a JSON string and write it out to a file. That way, when you read back the file, using a JSON deserializer, you can get the POJO back in it's original form. The other consideration is that if your data contains sensitive information, you should NOT store it in plain text - please use a custom serializer class to store the data in binary (unreadable) form. Hope this helps ! Commented Jan 6, 2016 at 7:10
  • @trishul thank you for suggestions, but frankly, I've never ever heard all those words (POJO, JSON) before. I am still learning Java. Could you recommend something basic? Commented Jan 6, 2016 at 7:14
  • @ShreyAryan That's ok. We are all learners. POJO -> Plain Old Java Object and JSON -> Javascript Object Notation (a string based format). These are just terms that are being used these days. And they are pretty basic once you Google them a bit. All the best! Commented Jan 6, 2016 at 7:26

3 Answers 3

3

Another way would be to use Serialization. Your Student class would have to implement Serializable:

class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    private int age;
    private int rollNumber;
    //...
}

Then to read and write the Student array:

try {
    Student[] students = new Student[3];

    //Write Student array to file.
    FileOutputStream fos = new FileOutputStream("students.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(students);
    oos.close();

    //Read Student array from file.
    FileInputStream fis = new FileInputStream("students.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Student[] studentsFromSavedFile = (Student[]) ois.readObject();
    ois.close();
}
catch (FileNotFoundException e) {
    e.printStackTrace();
}
catch (IOException e) {
    e.printStackTrace();
}
catch (ClassNotFoundException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can always work out the format in your toString() method of the class so that when you do write to a file, it will write the object as per that format.

So lets say your toString() is,

public String toString() {
    return "Name : " + name + " : Age : " + age;
}

So this will write to your file as, (if you call bw.write(person.toString());)

Name : Azhar : Age : 25

1 Comment

How would the stored results will be parsed later while reading back from the file?
0

If you don't care of any format (text or binary), you can try a few ways:

  1. Serialize them using Jackson to JSON or XML
  2. Serialize then using Xstream to XML

Then you can store them to file using FileWriter

Comments

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.