Lara

File Handling

Monday(16-01-2017)

package com.lara;

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

 

class P implements Serializable

{

            int i;

            transient int j;         

           

            private void writeObject(ObjectOutputStream out) throws IOException

            {

                        System.out.println("writeObject");

                        out.defaultWriteObject();

                        //out.writeInt(j);

            }          

            private void readObject(ObjectInputStream in)

            throws IOException, ClassNotFoundException

            {

                        System.out.println("readObject");

                        in.defaultReadObject();

                        //j = in.readInt();

            }

           

}

public class Manager55

{

            public static void main(String[] args)

            {

                        P p1 = new P();

                        p1.i = 10;

                        p1.j = 20;

                        try(FileOutputStream fout = new FileOutputStream("test9.ser");

                                    ObjectOutputStream out = new ObjectOutputStream(fout))

                        {

                                    out.writeObject(p1);

                                    System.out.println("done");

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

package com.lara;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

 

public class Manager56

{

            public static void main(String[] args)

            {

                        try(FileInputStream fin = new FileInputStream("test9.ser");

                                    ObjectInputStream in = new ObjectInputStream(fin))

                        {

                                    P obj = (P) in.readObject();

                                    System.out.println(obj.i);

                                    System.out.println(obj.j);

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                        catch(ClassNotFoundException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

package com.lara;

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

 

class Q implements Serializable

{

            int i;

}

class R extends Q

{

            int j;

}

public class Manager57

{

            public static void main(String[] args)

            {

                        R r1 = new R();

                        r1.i = 10;

                        r1.j = 20;

                        try(FileOutputStream fout = new FileOutputStream("test10.ser");

                                    ObjectOutputStream out = new ObjectOutputStream(fout))

                        {

                                    out.writeObject(r1);

                                    System.out.println("done");

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                       

            }

}

package com.lara;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

 

public class Manager58

{

            public static void main(String[] args)

            {

                        try(FileInputStream fin = new FileInputStream("test10.ser");

                                    ObjectInputStream in = new ObjectInputStream(fin))

                        {

                                    R obj = (R) in.readObject();

                                    System.out.println(obj.i);

                                    System.out.println(obj.j);

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                        catch(ClassNotFoundException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

package com.lara;

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

 

class S

{

            int i;

            S()

            {

                        System.out.println("S()");

            }

}

class T extends S implements Serializable

{

            int j;

            T()

            {

                        System.out.println("T()");

            }

}

class U extends T

{

            int k;

            U()

            {

                        System.out.println("U()");

            }

            /*

            private void writeObject(ObjectOutputStream out) throws IOException

            {

                        out.defaultWriteObject();

                        out.writeInt(i);

                        out.writeInt(j);

            }

           

            private void readObject(ObjectInputStream in)

            throws IOException, ClassNotFoundException

            {

                        in.defaultReadObject();

                        i = in.readInt();

                        j = in.readInt();

            }

            */

}

public class Manager59

{

            public static void main(String[] args)

            {

                        U u1 = new U();

                        u1.i = 10;

                        u1.j = 20;

                        u1.k = 30;

                        try(FileOutputStream fout = new FileOutputStream("test10.ser");

                                    ObjectOutputStream out = new ObjectOutputStream(fout))

                        {

                                    out.writeObject(u1);

                                    System.out.println("done");

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                       

            }

}

package com.lara;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

 

public class Manager60

{

            public static void main(String[] args)

            {

                        try(FileInputStream fin = new FileInputStream("test10.ser");

                                    ObjectInputStream in = new ObjectInputStream(fin))

                        {

                                    U obj = (U) in.readObject();

                                    System.out.println(obj.i);

                                    System.out.println(obj.j);

                                    System.out.println(obj.k);

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                        catch(ClassNotFoundException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

 

 Tuesday(17-01-2017)

package com.lara;

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

 

class Z implements Serializable

{

            int i;

            int j;

            int k;

}

public class Manager61

{

            public static void main(String[] args)

            {

                        Z obj = new Z();

                        obj.i = 20;

                        try(FileOutputStream fout = new FileOutputStream("test11.ser");

                                    ObjectOutputStream out = new ObjectOutputStream(fout))

                        {

                                    out.writeObject(obj);

                                    System.out.println("done");

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

package com.lara;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

 

public class Manager62

{

            public static void main(String[] args)

            {

                        try(FileInputStream fin = new FileInputStream("test11.ser");

                                    ObjectInputStream in = new ObjectInputStream(fin))

                        {

                                    Z obj = (Z) in.readObject();

                                    System.out.println(obj.i);

                        }

                        catch(IOException ex)

                        {

                                    ex.printStackTrace();

                        }

                        catch(ClassNotFoundException ex)

                        {

                                    ex.printStackTrace();

                        }

            }

}

package com.lara;

 

import java.io.PrintStream;

 

public class Manager63

{

            public static void main(String[] args)

            {

                        System.out.println("Hello to all");

                        PrintStream ps = System.out;

                        ps.println("Hello to all again");

                        ps.println("Hello to all again");

                        ps.println("Hello to all again");

                        ps.println("Hello to all again");

                        ps.println("Hello to all again");

            }

}

package com.lara;

 

import java.io.IOException;

import java.io.PrintStream;

 

public class Manager64

{

            public static void main(String[] args)

            throws IOException

            {

                        PrintStream ps = new PrintStream("stdout.txt");

                        ps.println("Hello to all");

                        ps.println("Hello to all");

                        ps.println("Hello to all");

                        System.out.println("done");

            }

}

 

 

 

package com.lara;

 

import java.io.IOException;

import java.io.PrintStream;

 

public class Manager65

{

            public static void main(String[] args)

            throws IOException

            {

                        System.out.println("main begin");

                        PrintStream ps = new PrintStream("stdout1.txt");

                        System.setOut(ps);

                        System.out.println("Hello to all");

                        System.out.println("Hello to all");

                        System.out.println("Hello to all");                                           

            }

}

 

 

 

 

package com.lara;

 

import java.io.IOException;

import java.io.PrintStream;

 

public class Manager66

{

            public static void main(String[] args)

            throws IOException

            {

                        PrintStream ps1 = new PrintStream("stdout2.txt");

                        PrintStream ps2 = System.out;

                        ps1.println(1); //f

                        ps2.println(2); //c

                        System.out.println(3); //c

                        System.setOut(ps1);

                        ps1.println(4); //f

                        ps2.println(5); //c

                        System.out.println(6); //f

                        System.setOut(ps2);

                        ps1.println(7); //f

                        ps2.println(8); //c

                        System.out.println(9); //c

            }

}

package com.lara;

 

import java.io.Console;

 

public class Manager67

{

            public static void main(String[] args)

            {

                        //Console c1 = new Console();

                        Console c2 = System.console();

                        System.out.println(c2);

            }

}