3 Replies Latest reply: Jun 23, 2008 5:58 AM by mlk RSS

    Stroring certain details in a file, But Could not delete the File.

    807589
      I am storing certain details using serialization. But when i decide to delete the File i am unable to delete the File. I am attaching a sample Code to understand my problem.
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.Serializable;
      
      public class SerialDemo implements Serializable {
        public static void main(String[] args) {
      
          try {
            {
              FileOutputStream f = new FileOutputStream("./test.proj");
              ObjectOutputStream s = new ObjectOutputStream(f);
              SerialDemo d = new SerialDemo(5);
      
              s.writeObject(d);
              s.flush();
            }
            {
              FileInputStream f = new FileInputStream("./test.proj");
              ObjectInputStream s = new ObjectInputStream(f);
              SerialDemo d = (SerialDemo) s.readObject();
              System.out.println(d.getIndex());
      
              s.close();
              f.close();
              System.out.println("SerialDemo.getVal() is: " + d.getVal());
            }
      
            File file = new File("./test.proj");
            System.out.println(file.delete());
      
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      
        int test_val = 7; // value defaults to 7
      
        public SerialDemo() {
          super();
        }
      
        public SerialDemo(int x) {
          super();
          test_val = x;
        }
      
        public int getVal() {
          return test_val;
        }
      
        private int idx = 2;
      
        public int getIndex() {
          return idx;
        }
      
        static final long serialVersionUID = -4717603181820250815L;
      }