1. IO
①Serialize: When we need to transfer a oject from a computer to another computer's disk, we use serialize. ObjectOutputStream oos
Dog d = new Dog("H",12,"sssssssssss");
try (
ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream("d:/a.properties"));
) {
s.writeObject(d);
s.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
②Deserialize: ObjectInputStream ois
try (
ObjectInputStream i = new ObjectInputStream(new FileInputStream("d:/a.properties"));
)
{
Dog d = (Dog) i.readObject();
System.out.println(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
③Transient: the member variables modified by transient will not participate in the serialize
transient String address;
④writeObject() & readObject()
⑤RandomAccess: program can read and write file freely.
try {
RandomAccessFile f = new RandomAccessFile("d:/a.properties", "rw");//Read and Write
f.seek(1);//Insert into '1' position
f.write("hello ".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2. NIO:
①The difference between IO and NIO: The input and ouput method of traditional IO is blocking. Conversely, NIO will map file or a part of area of file into memory so that it can access file as fast as memory.
②Channel & Buffer:
(1)All the data in NIO need to transferred by Channel, it provide a map method. Traditional input - output is cater to Stream, but NIO is face to block.
(2)All the data need to preserve in Buffer, including the data in Channel.
CharBuffer c = CharBuffer.allocate(8);
System.out.println("Position " + c.position());the next index can be read.
System.out.println("limit " + c.limit());// the data behind limit cannot read
System.out.println("Capacity " + c.capacity());
c.put("a");
c.put("b");
c.put("c");
System.out.println("Position " + c.position());
c.flip();//changing the position of 'limit' to 'position', position set '0'
System.out.println("Position " + c.position());
System.out.println("limit " + c.limit());
System.out.println(c.get(1));
System.out.println("Position " + c.position());
c.clear();
System.out.println(c.get(2));
System.out.println("Position " + c.position());
System.out.println("limit " + c.limit());
System.out.println("Capacity " + c.capacity());
}
3. Thread:
①Process == Application
②Thread is execution unit exist in process. It can only access to resources in this process. When system create a process, it will also create a main thread.
③Thread & Runnable:
Since 'Runnable' is a interface, so we can realize other supclasses and interfaces easily. But we still need a type of Thread to receive.
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
}
④The state of Thread:
⑤Thread.currentThread: gaining current Thread.
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().isDaemon());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thread t = new Thread(){
public void run() {
System.out.println(Thread.currentThread().getState());
}
};
t.start();
⑥Daemon: it will forwordlly stop should declare before current thread start.