博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO流的复习笔记
阅读量:7227 次
发布时间:2019-06-29

本文共 5457 字,大约阅读时间需要 18 分钟。

IO字节流和缓冲流

IO字节流的读取和写入

读取

1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4  5 public class Test { 6     public static void main(String[] args) { 7         try (FileInputStream fis = new FileInputStream("java.txt") 8         ) { 9             byte[] bytes = new byte[40];10             int temp;11             while ((temp = fis.read(bytes)) != -1) {12                 System.out.println(new String(bytes, 0, temp));13             }14         } catch (FileNotFoundException e) {15             e.printStackTrace();16         } catch (IOException e) {17             e.printStackTrace();18         }19     }20 }

写入

1 import java.io.*; 2  3 public class Test { 4     public static void main(String[] args) { 5         try (FileOutputStream fos = new FileOutputStream("file" + File.separator + "1024.txt", true) //写入时不清除原有数据 6         ) { 7             fos.write("Hello".getBytes()); 8             fos.write("\n".getBytes());  //换行 9             fos.write("Wrold!".getBytes());10             fos.flush(); //刷新11         } catch (FileNotFoundException e) {12             e.printStackTrace();13         } catch (IOException e) {14             e.printStackTrace();15         }16     }17 }

IO字节流的文件拷贝

 

1 import java.io.*; 2  3 public class Test { 4     public static void main(String[] args) { 5         try ( 6                 FileOutputStream fos = new FileOutputStream("file" + File.separator + "new.txt"); 7                 FileInputStream fis = new FileInputStream("java.txt") 8         ) { 9             int temp;10             byte[] bytes = new byte[50];11             while ((temp = fis.read(bytes)) != -1) {12                 fos.write(bytes);13             }14         } catch (FileNotFoundException e) {15             e.printStackTrace();16         } catch (IOException e) {17             e.printStackTrace();18         }19     }20 }

IO缓冲流的读取和写入

1 import java.io.*; 2  3 /** 4  * IO缓冲字节流的读取 5  */ 6 public class Test { 7     public static void main(String[] args) { 8         try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("java.txt")); 9              BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new.txt"));10         ) {11             int temp;12             while ((temp = bis.read()) != -1) {13                 bos.write(temp);14             }15         } catch (FileNotFoundException e) {16             e.printStackTrace();17         } catch (IOException e) {18             e.printStackTrace();19         }20     }21 }

 

 IO字符流和缓冲流

IO字符流的文件拷贝

1 import java.io.*; 2  3 /** 4  * IO字符流的文件拷贝 5  */ 6 public class Test { 7     public static void main(String[] args) { 8         try (FileReader fr = new FileReader("java.txt"); 9              FileWriter fw = new FileWriter("file" + File.separator + "new.txt")10         ) {11             int temp;12             while ((temp = fr.read()) != -1) {13                 fw.write(temp);14             }15         } catch (FileNotFoundException e) {16             e.printStackTrace();17         } catch (IOException e) {18             e.printStackTrace();19         }20     }21 }

IO缓冲字符流的文件拷贝

1 import java.io.*; 2  3 /** 4  * IO缓冲字符流的文件拷贝 5  */ 6 public class Test { 7     public static void main(String[] args) { 8         try ( 9                 BufferedReader br = new BufferedReader(new FileReader("java.txt"));10                 BufferedWriter bw = new BufferedWriter(new FileWriter("file" + File.separator + "new.txt"))11         ) {12                 String msg;13                 while ((msg = br.readLine()) != null) {14                     bw.write(msg);15                     bw.newLine();  //换行16                     bw.flush();  //刷新17                 }18 19         } catch (FileNotFoundException e) {20             e.printStackTrace();21         } catch (IOException e) {22             e.printStackTrace();23         }24     }25 }

练习:图片简单的加密和解密

加密

1 import java.io.*; 2  3 /** 4  * 图片的简单加密 5  * 通过异或的方式 6  */ 7 public class Test { 8     public static void main(String[] args) { 9         try (10                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "123.jpg"));11                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new123.jpg"))12         ) {13             int temp;14             while ((temp = bis.read()) != -1) {15                 bos.write(temp ^ 88);16             }17         } catch (FileNotFoundException e) {18             e.printStackTrace();19         } catch (IOException e) {20             e.printStackTrace();21         }22     }23 }

解密

1 import java.io.*; 2  3 /** 4  * 图片的简单解密 5  * 通过异或的方式 6  */ 7 public class Test { 8     public static void main(String[] args) { 9         try (10                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "code.jpg"));11                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "decode.jpg"))12         ) {13             int temp;14             while ((temp = bis.read()) != -1) {15                 bos.write(temp ^ 88);16             }17         } catch (FileNotFoundException e) {18             e.printStackTrace();19         } catch (IOException e) {20             e.printStackTrace();21         }22     }23 }

 

转载于:https://www.cnblogs.com/xiaowangtongxue/p/10716108.html

你可能感兴趣的文章
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
查看>>
Golang数据库编程之GORM模型定义与数据库迁移
查看>>
Oracle redo解析之-4、rowid的计算
查看>>
Easy Scheduler 1.0.3 发布,分布式工作流任务调度系统
查看>>
java 颠倒整数
查看>>
Python入门教程100天:Day05-练习总结
查看>>
环境搭建,8种基本类型,Static,package和import,log4j
查看>>
即将到来的 Debian 10 Buster 发布版的新特点
查看>>
iOS 头部视图下拉变大
查看>>
Disruptor并发框架
查看>>
react-hooks 实现简单的评论list
查看>>
【多图警告】学会JavaScript测试你就是同行中最亮的仔(妹)
查看>>
19-04-25
查看>>
一个JAVA程序员成长之路分享
查看>>
30K iOS程序员的简述:如何快速进阶成为高级开发人员
查看>>
Go 夜读 - 每周四晚上 Go 源码阅读技术分享
查看>>
tranform知多少
查看>>
Android电量优化
查看>>
[爬虫手记] 我是如何在3分钟内开发完一个爬虫的
查看>>
【译】Css Grid VS Flexbox: 实践比较
查看>>