java-ioStream

/** ioStream
* 从传输方式上分为:字节流(InputStream、OutputStream)、字符流(Reader、Writer)
* 从数据操作上分为:文件、数组、管道操作、基本数据类型、缓冲操作、打印、对象序列化反序列化、转换
*
* 字节流读取单个字节、字符流读取单个字符
* 字节流用来处理二进制文件(图片、mp3..),字符流用来处理文本文件
* 编码就是把字符转换为字节,解码则相反
*/

  1 public class ioStreamExample {
  2 
  3     // 装饰者模式,BufferedInputStreamz装饰fileInputStream
  4     public void decorator_method() throws FileNotFoundException {
  5         FileInputStream fileInputStream = new FileInputStream("");
  6         FileOutputStream fileOutputStream  =new FileOutputStream("");
  7         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
  8     }
  9 
 10     // IO-磁盘操作:File
 11     public void listAllFiles(File dir){
 12         if (dir == null || !dir.exists()){
 13             return;
 14         }if (dir.isFile()){
 15             System.out.println(dir.getName());
 16             return;
 17         }
 18         // 递归地列出一个目录下所有文件
 19         for (File file: Objects.requireNonNull(dir.listFiles())){
 20             listAllFiles(file);
 21         }
 22     }
 23 
 24     // IO-字节操作:(InputStream和OutputStream)拷贝文件内容
 25     public void copyFile(String src,String dst) throws IOException {
 26         FileInputStream fi = new FileInputStream(src);
 27         FileOutputStream fo = new FileOutputStream(dst);
 28         byte[] buf = new byte[20 * 1024];
 29         while (fi.read(buf,0,buf.length) != -1){
 30             fo.write(buf);
 31         }
 32 
 33         fi.close();
 34         fo.close();
 35     }
 36 
 37     // IO-字符操作:(Reader和Writer)输出文件内容
 38     public void readFileContent(String filePath) throws IOException {
 39         FileReader fileReader = new FileReader(filePath);
 40         BufferedReader bufferedReader = new BufferedReader(fileReader);
 41         String line = "";
 42         while ((line = bufferedReader.readLine()) != null){
 43             System.out.println(line);
 44         }
 45 
 46         bufferedReader.close();
 47     }
 48 
 49     // IO-对象操作:序列化。序列化的类需要实现Serializable接口
 50     /**序列化:将一个对象转换成字节序列,方便存储和传输
 51      * 序列化:ObjectOutputStream.writeObject()
 52      * 反序列化:ObjectInputStream.readObject()
 53      */
 54     public void serializableExample(String filePath) throws IOException, ClassNotFoundException {
 55         A a = new A(100,"y");
 56         ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(filePath));
 57         oo.writeObject(a);
 58         oo.close();
 59 
 60         ObjectInputStream oi = new ObjectInputStream(new FileInputStream(filePath));
 61         A a1 = (A) oi.readObject();
 62         oi.close();
 63         System.out.println(a1);
 64 
 65     }
 66 
 67     // IO-网络操作:(socket)
 68     /** Sockets:使用tcp协议实现网络通信
 69      *      ServerSocket:服务端类,Socket:客户端类。服务端和客户端通过InputStream和OutputStream进行输入输出
 70      *  Datagram:使用udp协议实现网络通信
 71      *      DatagramSocket:通信类,DatagramPacket:数据包类
 72      */
 73     public void socketExample() throws IOException {
 74         URL url = new URL("http://www.baidu.com");
 75         InputStream is = url.openStream();      // 字节流
 76         InputStreamReader isr = new InputStreamReader(is,"utf-8");      // 字符流
 77         BufferedReader br = new BufferedReader(isr);        // 提供缓存功能
 78         String line;
 79         while ((line = br.readLine()) != null){
 80             System.out.println(line);
 81         }
 82         br.close();
 83     }
 84 
 85     public static void main(String[] args) throws IOException, ClassNotFoundException {
 86         String file = "D:\\oldfile.txt";
 87         ioStreamExample ioStreamExample = new ioStreamExample();
 88 
 89 //        ioStreamExample.serializableExample(file);
 90 //        ioStreamExample.readFileContent(file);
 91 
 92         ioStreamExample.socketExample();
 93 
 94     }
 95 
 96 }
 97 
 98 @Data
 99 @AllArgsConstructor
100 @NoArgsConstructor
101 class A implements Serializable{
102     private int x;
103     private String y;
104 }

 

posted @ 2023-03-20 14:18  _titleInfo  阅读(27)  评论(0编辑  收藏  举报
//雪花飘落效果