寫幾個關於IO的回顧吧,IO回顧吧

來源:互聯網
上載者:User

寫幾個關於IO的回顧吧,IO回顧吧

上午將開題報告寫完了。下午的時候做了90道關於javaSE部分的題。晚上把Jdbc串連部分又回顧了一下。現在回顧一下IO的幾個封裝類吧。
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;    ......}

這就完成了對象的序列化和還原序列化

再來一個比較特殊的

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 (){        }    }}

這樣好像什麼效果都沒體現出來!!!!其實主要還是一個seek()方法的使用

再看看這個例子

@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();                }            }        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.