0

I have a method writing List<String> into file like this:

public static void main(String[] args) {
    List<String> data = Arrays.asList("1", "2", "3", "4", "5");
    writeObjectToFile(data, "demo.dat");
    
    List<String> result = null;
    readObjectFromFile(result, "demo.dat");
    System.out.println(result);
}

with the writeObjectToFile method:

public static <T> void writeObjectToFile(List<T> obj, String fileName) {
    File file = new File(fileName);
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // close fos, oos, ... 
    }
}

and readObjectFromFile method:

public static <T> void readObjectFromFile(List<T> obj, String fileName) {
    File file = new File(fileName);
    FileInputStream fis = null;
    ObjectInputStream ois = null;

    if (file.exists()) {
        try {
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            Object readObject = ois.readObject();
            obj = (List<T>) readObject;
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // close fis, ois,...
        }
    }
}

The writing data to file is OK, but the reading step is not working as expected. I got the result list is null:

// console output
[1, 2, 3, 4, 5] // --> this is result of System.out.println(obj);
null            // --> this is result of System.out.println(result);

But when I change the reading method to return a list:

public static <T> List<T> readObjectFromFile(String fileName) {
    File file = new File(fileName);
    FileInputStream fis = null;
    ObjectInputStream ois = null;

    if (file.exists()) {
        try {
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            Object readObject = ois.readObject();
            List<T> obj = (List<T>) readObject;
            System.out.println(obj);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // close fis, ois,...
        }
    }
    return null;
}

Then I get the expected result:

// console output
[1, 2, 3, 4, 5] // --> this is result of System.out.println(obj);
[1, 2, 3, 4, 5] // --> this is result of System.out.println(result);

I don't know why is it. Why when I passed the result list as a parameter, I could not get the expected result? Please help me.

truongbb96
  • 33
  • 5

2 Answers2

1

You are changing it reference value, Java is pass by value, when you pass an object to a method as a parameter, java pass a variable that keep the reference value, you can't change that value ( correctly, you can change that value just inside method only )

Imagine that it point to null before you pass it into method

Inside method, you are trying to point it to another object ( assign new reference value ) It will be discard when method end, it still point to null

VietDD
  • 1,048
  • 2
  • 12
  • 12
0

Your List obj is only a reference to the list you initialised in main(). And this reference is on the stack as long as the method is executing. When you assign a new value to obj, then obj points to that value. You have not changed the value of result, as you might be expecting. What you have to do is something like

obj.addAll(readObject)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Oliver Marienfeld
  • 1,154
  • 9
  • 17