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