BIO
字节流
FileOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("file.txt"); fos.write(100);
byte[] bytes = {65, 66, 67, 68}; fos.write(bytes);
fos.write(bytes, 1, 1);
fos.write("hello".getBytes());
fos.close(); } }
|
FileInputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Main { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("file.txt");
byte[] bytes = new byte[1024]; int len = 0; while ((len = fis.read(bytes)) != -1) { System.out.print(new String(bytes, 0, len)); }
fis.close(); } }
|
字符流
FileWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class Main { public static void main(String[] args) throws Exception { FileWriter fw = new FileWriter("file.txt"); fw.write(100); fw.flush();
char[] c = {'a', 'b', 'c', 'd'}; fw.write(c); fw.flush();
fw.write(c, 2, 2); fw.flush();
fw.write("hello"); fw.flush();
fw.close(); } }
|
FileReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Main { public static void main(String[] args) throws Exception { FileReader fr = new FileReader("file.txt");
char[] c = new char[1024]; int len = 0; while ((len = fr.read(c)) != -1) { System.out.print(new String(c, 0, len)); }
fr.close(); } }
|
转换流
OutputStreamWriter
1 2 3 4 5 6 7
| public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); se = StreamEncoder.forOutputStreamWriter(out, this, cs); }
|
InputStreamReader
1 2 3 4 5 6 7 8 9
| public InputStreamReader(InputStream in) { super(in); try { sd = StreamDecoder.forInputStreamReader(in, this, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } }
|
字节缓冲流
BufferedOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("file.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(55);
byte[] bytes = "hello".getBytes(); bos.write(bytes); bos.write(bytes, 3, 2);
bos.close(); fos.close(); } }
|
BufferedInputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Main { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("file.txt"); BufferedInputStream bis = new BufferedInputStream(fis); byte[] bytes = new byte[1024]; int len = 0; while ((len = bis.read(bytes)) != -1) { System.out.print(new String(bytes, 0, len)); } bis.close(); fis.close(); } }
|
字符缓冲流
BufferedWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class Main { public static void main(String[] args) throws Exception { FileWriter fw = new FileWriter("file.txt"); BufferedWriter bw = new BufferedWriter(fw);
bw.write(100); bw.flush();
bw.write("你好".toCharArray()); bw.newLine(); bw.flush();
bw.write("大家好"); bw.flush();
bw.close(); fw.close(); } }
|
BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Main { public static void main(String[] args) throws Exception { FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr);
String line = null; while ((line = br.readLine()) != null) { System.out.println(line); }
br.close(); fr.close(); } }
|
对象流
User
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class User implements Serializable { int id; String name;
public User() { }
public User(int id, String name) { this.id = id; this.name = name; } }
|
ObjectOutputStream
1 2 3 4 5 6 7 8 9 10 11
| public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("file.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); User user = new User(1, "echo"); oos.writeObject(user); oos.close(); fos.close(); } }
|
ObjectInputStream
1 2 3 4 5 6 7 8 9 10 11
| public class Main { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("file.txt"); ObjectInputStream ois = new ObjectInputStream(fis); User user = (User) ois.readObject(); System.out.println(user); ois.close(); fis.close(); } }
|
打印流
PrintStream
1 2 3 4 5 6 7 8 9 10 11 12
| public class Main { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("file.txt"); PrintStream ps = new PrintStream(fos); ps.print("a"); ps.println("b"); ps.print("c"); ps.close(); fos.close(); } }
|
PrintWriter
1 2 3 4 5 6 7 8 9 10 11 12
| public class Main { public static void main(String[] args) throws Exception { FileWriter fw = new FileWriter("file.txt"); PrintWriter pw = new PrintWriter(fw); pw.print("a"); pw.println("b"); pw.print("c"); pw.close(); fw.close(); } }
|
标准输入输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = null; while ((s = br.readLine()) != null) { if ("886".equals(s)) { break; } System.out.println(s); } br.close(); } }
|
网络编程
UDP通信
UDP发送端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class UDPSend { public static void main(String[] args) throws Exception { byte[] data = "你好UDP".getBytes(); InetAddress inet = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(data, data.length, inet, 8080); DatagramSocket ds = new DatagramSocket(); ds.send(dp); ds.close(); } }
|
UDP接收端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class UDPReceive { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(8080); byte[] data = new byte[1024]; DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); int length = dp.getLength(); System.out.println(ip + " " + port + ":" + new String(data, 0, length)); ds.close(); } }
|
TCP通信
TCP服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8080); Socket socket = serverSocket.accept(); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); OutputStream out = socket.getOutputStream(); out.write("收到,谢谢!!!".getBytes()); out.close(); in.close(); socket.close(); serverSocket.close(); } }
|
TCP客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class TCPClient { public static void main(String[] args) throws Exception { Socket socket = new Socket("127.0.0.1", 8080); OutputStream out = socket.getOutputStream(); out.write("服务器ok!".getBytes()); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); in.close(); out.close(); socket.close(); } }
|
NIO
在单线程的情况下,BIO是无法处理并发的。
服务器端如果不活跃的线程比较多,应该考虑单线程。
NIO:new io,noblocking io。
三大组件
Buffer、Channel、Selector。
Netty
传统IO与NIO比较
传统IO特点
阻塞点:server.accept()、in.read(bytes)
单线程情况下只能有一个客户端
用线程池可以有多个客户端连接,但是非常消耗性能
NIO的特点
ServerSocketChannel
SocketChannel
Selector
SelectionKey
netty介绍
netty版本
- netty3.x
- netty4.x
- netty5.x
netty可以运用在哪些领域
- 分布式进程通信:例如hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架版本通常还在用netty3.x。
- 游戏客户端开发:最新的游戏服务器有部分公司可能已经开始采用netty4.x或netty5.x。