Let's write a few reviews of IO. Let's review IO.

Source: Internet
Author: User
Tags object serialization

Let's write a few reviews of IO. Let's review IO.

The opening report is completed in the morning. In the afternoon, I made 90 questions about javaSE. In the evening, I reviewed the Jdbc connection section. Now let's review several IO packaging classes.
ObjectInputStream/ObjectOutputStream

ObjectOutputStream oos = null;try{    oos = new ObjectOutputStream(new FileOutputStream(new File("person.bat")));    oos.writeObject(new Person("zhangsan",23));    oos.flush();    oos.writeObject(new Person("lisi",12));    oos.flush();} catch(Exception e){    System.out.println(e.getMessage());} finally {    if(oos != null){        try{            oos.close();        } catch(){        }    }}
try{ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.bat"));Person per1 = ois.readObject();Person per2 = ois.readObject();System.out.println("per1:" + per1 + " per2:" + per2);} catch(Exception e){    System.out.println(e.getMessage());} finally {    if(ois != null){        try{            ois.close();        }catch(){        }    }}
public class Person implements Serializable{    private static final long serivalVersionUID = 1L;    private String name;    private int age;    ......}

This completes Object serialization and deserialization.

Another special

RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try{    raf1 = new RandomAccessFile(new File("a.txt"), "r");    raf2 = new RandomAccessFile(new File("b.txt"), "rw");    byte[] b = new byte[20];    int len;    while((len = raf1.read(b))!= -1){        raf2.write(b, 0, len);    }} catch(){}finally {    if(raf1 != null){        try{            ra1.close();        } catch (){        }        try{            raf2.close();        } catch (){        }    }}

It seems that nothing has been reflected !!!! In fact, it is mainly the use of a seek () method.

Let's take a look at this example.

@Test    public void test(){        RandomAccessFile rs1 = null;        try {            rs1 = new RandomAccessFile(new File("e.txt"), "rw");            rs1.seek(4);            StringBuffer sb = new StringBuffer(rs1.readLine());            rs1.seek(4);            rs1.write("xy".getBytes());            rs1.write(sb.toString().getBytes());        } catch (IOException e) {            e.printStackTrace();        } finally {            if(rs1 != null){                try {                    rs1.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.