Question

I want to write a method that reads an object from a binary file but I want to generalize it using generics.

I have this code:

@SuppressWarnings ("unchecked")
public static <T> T readFromBinaryFile (String filename){
T obj = null;

if (FileUtils.existsFile (filename)){
    ObjectInputStream ois = null;
    try{
        ois = new ObjectInputStream (new FileInputStream (filename));
        obj = (T)ois.readObject ();
    }catch (IOException e){
        Debug.out (e);
    }catch (ClassNotFoundException e){
        Debug.out (e);
    }finally{
        try{
            if (ois != null) ois.close();
        }catch (IOException e){
            Debug.out (e);
        }
    }
}

return obj;

}

When I execute it I get a ClassCastException. I don't know anything about templates in java so any information will be apreciated. I've read something related to erasure, compile-time and execution-time but I don't understand very well why I get this ClassCastException.

Thanks.

EDIT: I call the method like this: FileUtils.readFromBinaryFile (filename); (Without "")

Was it helpful?

Solution

You're calling your method wrong. Just do it this way:

FileUtils.readFromBinaryFile (filename); 

What you're calling is so-called generic method.

"We don't have to pass an actual type argument to a generic method. The compiler infers the type argument for us, based on the types of the actual arguments. It will generally infer the most specific type argument that will make the call type-correct." source

EDIT:

I've tried your example and it works (I had actually comment some lines)

    private static String filename = "number.serialized";

    public static void main(String[] args) {
        Double d = 3.14;
        writeToBinary(d, filename);
        Double readD = readFromBinaryFile(filename);
        System.out.println(readD);
    }

    @SuppressWarnings ("unchecked")
    public static <T> T readFromBinaryFile(String filename) {
        T obj = null;
        File file = new File(filename);
        if (file.exists()) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(filename));
                obj = (T)ois.readObject();
            } catch (IOException e) {
            } catch (ClassNotFoundException e) {
            } finally {
                try {
                    if (ois != null)
                        ois.close();
                } catch (IOException e) {
                }
            }
        }
        return obj;
    }

    public static <T> void writeToBinary(T obj, String filename) 
    {
        try {
            FileOutputStream fis = new FileOutputStream(filename);
            ObjectOutputStream oos = new ObjectOutputStream(fis);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

OTHER TIPS

Are there templates in Java? Just use Object instead of T. In Java everything derives from Object at the base, so you don't want T obj but Object obj.

A ClassCastException means the type you read didn't match the type you were expecting (and cast to) I suggest you see which object you are read in a debugger (or a log message) and compare it with what you are expecting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top