ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

对Java中IO流的理解

2026/9/7 19:24:51 拓冰建站 浏览量
对Java中IO流的理解 1.IO流IO流是读取数据和发送数据的一种机制Input是读取Output是发送。最基本的两个InputStream和OutputStream在网络通信中我们通常会用到IO流中最基础的IO先构建两台机器的连接程序中用server和client来演示我们在发送时要遵从一定的消息协议规范发送的消息内容通常是固定发送字节长度然后读取字节例如我程序中就是先写要发送消息的长度再将消息存储在bytes数组里面最后读取bytes里面的字节转换为显示的信息in.read()和os.write()都是读取和传输的字节例如发送了你好就会将你好写成字符编码字符编码再写成用字节表示。package Inter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Scanner; import static java.lang.System.in; public class A { public static void main(String[] args) { try { Socket scnew Socket(127.0.0.1,54321); System.out.println(A发送连接); InputStream is sc.getInputStream(); OutputStream os sc.getOutputStream(); Scanner scanner new Scanner(System.in); String NameA; os.write(Name.length()); os.write(Name.getBytes(StandardCharsets.UTF_8)); System.out.println(); System.out.println(输入你想联系的人); String nscanner.nextLine(); os.write(n.length()); os.write(n.getBytes(StandardCharsets.UTF_8)); os.flush(); while(true){ System.out.println(请发送消息); String mscanner.nextLine(); int lenm.length(); os.write(len); byte[] mbytem.getBytes(StandardCharsets.UTF_8); os.write(mbyte);//把数组里面的内容写出来 os.flush(); int m1is.read(); byte[] isbytenew byte[m1]; is.read(isbyte); String isstrnew String(isbyte,StandardCharsets.UTF_8); System.out.println(接收到); System.out.println(isstr); } } catch (IOException e) { throw new RuntimeException(e); } } }package Inter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Scanner; import static java.lang.System.in; public class B { public static void main(String[] args) { try { Socket sc new Socket(127.0.0.1, 54321); System.out.println(B发送连接); InputStream is sc.getInputStream(); OutputStream os sc.getOutputStream(); Scanner scanner new Scanner(System.in); String NameB; os.write(Name.length()); os.write(Name.getBytes(StandardCharsets.UTF_8)); System.out.println(); System.out.println(输入你想联系的人); String nscanner.nextLine(); os.write(n.length()); os.write(n.getBytes(StandardCharsets.UTF_8)); os.flush(); while (true) { System.out.println(请发送消息); String mscanner.nextLine(); int lenm.length(); os.write(len); byte[] bytesm.getBytes(StandardCharsets.UTF_8); os.write(bytes); os.flush(); int islenis.read(); byte[] isbytenew byte[islen]; is.read(isbyte); String isstrnew String(isbyte,StandardCharsets.UTF_8); System.out.println(接收到); System.out.println(isstr); } } catch (RuntimeException | IOException e) { throw new RuntimeException(e); } } }A和B就是IO流的体现遵循发送的消息的消息协议先发送消息的长度再发送消息内容接收的时候则是先接收消息长度根据长度创建合适的byte数组将消息存放进去2、IO流拓展根据基础的IO流我们可以拓展出很多包含其他功能的package IOtest; import java.io.*; import java.net.Socket; public class Client { static void main() { try { FileInputStream fisnew FileInputStream(C:\\Users\\20553\\Pictures\\Screenshots\\屏幕截图 2026-08-04 174453.png); Socket socketnew Socket(localhost,10005); OutputStream ossocket.getOutputStream(); BufferedInputStream bisnew BufferedInputStream(fis); BufferedOutputStream bosnew BufferedOutputStream(os); System.out.println(开始传输文件); while(true) { int filebis.read(); if(file-1){ break; } bos.write(file); bos.flush(); } os.close(); System.out.println(文件发送完成); } catch (IOException e) { throw new RuntimeException(e); } } }package IOtest; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server{ static void main() { try { ServerSocket ssnew ServerSocket(10005); Socket sss.accept(); InputStream iss.getInputStream(); File filenew File(photo/img.png); FileOutputStream fosnew FileOutputStream(file); BufferedInputStream bisnew BufferedInputStream(is); BufferedOutputStream bosnew BufferedOutputStream(fos); if(!file.exists()){ file.createNewFile(); } System.out.println(连接成功读取数据); long startSystem.currentTimeMillis(); while(true){ int datasbis.read(); if(datas-1){ break; } bos.write(datas); bos.flush(); } fos.close(); System.out.println(读取完成发送文件); long endSystem.currentTimeMillis(); System.out.println(运行时间为(end-start)ms); } catch (IOException e) { throw new RuntimeException(e); } } }上面的server与client端的就是fileoutputstream和fileInputstream就是针对与电脑上存储文件的位置的直接流读取和输入的buffered则是相当于加上了缓存的可以使得读取速度快很多input和output主要的读取的字节相应的WriterReader是读取字符更适合处理文本。