• JDK -- IO


    字符集

    常见字符集:

    1. ASCII 字符集
      ASCII(American Standard Code for Information Interchange,美国信息交换码):包含数字、英文、符号
    2. GBK字符集
      GBK是中国的码表,包含了几万个汉字字符,同时也要兼容ASCII编码
      GBK编码中一个中文字符一般以两个字节存储
    3. Unicode(UTF-8)字符集等
      统一码,也叫万国码。是计算机科学领域里的一项业界标准
      UTF-8是Unicode的一种常见编码方式
      UTF-8编码后一个中文一般以三个字节的形式存储,同时兼容ASCII编码表
    1. 常见字符集 底层字符的编码是什么样的?
    • 英文和数字等在任何国家的字符集中都占1个字节
    • GBK字符中一个中文字符占2个字节
    • UTF-8编码中一个中文1般占3个字节
    2. 编码前的字符集和解码时的字符集有什么要求?
    • 必须一致,否则会出现字符乱码
    • 英文和数字不会乱码

    普通字节流

    FileInputStream & FileOutputStream

    1. public class Test {
    2. public static void main(String[] args) {
    3. try (
    4. // 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
    5. // 1、创建一个字节输入流管道与原视频接通
    6. InputStream is = new FileInputStream("file-io-app/src/out04.txt");
    7. // 2、创建一个字节输出流管道与目标文件接通
    8. OutputStream os = new FileOutputStream("file-io-app/src/out05.txt");
    9. // int age = 23; // 这里只能放资源
    10. MyConnection connection = new MyConnection(); // 最终会自动调用资源的close方法
    11. ) {
    12. // 3、定义一个字节数组转移数据
    13. byte[] buffer = new byte[1024];
    14. int len; // 记录每次读取的字节数。
    15. while ((len = is.read(buffer)) != -1) {
    16. os.write(buffer, 0, len);
    17. }
    18. System.out.println("复制完成了!");
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }
    24. class MyConnection implements AutoCloseable {
    25. @Override
    26. public void close() throws IOException {
    27. System.out.println("连接资源被成功释放了!");
    28. }
    29. }

    普通字符流

    FileReader & FileWriter

    1. public static void main(String[] args) throws Exception {
    2. // 1、创建一个文件字符输入流与源文件接通
    3. Reader fr = new FileReader("file-io-app/src/data07.txt");
    4. // 2、用循环,每次读取一个字符数组的数据。 1024 + 1024 + 8
    5. char[] buffer = new char[1024]; // 1K字符
    6. int len;
    7. while ((len = fr.read(buffer)) != -1) {
    8. String rs = new String(buffer, 0, len);
    9. System.out.print(rs);
    10. }
    11. }
    12. public static void main(String[] args) throws Exception {
    13. // 1、创建一个字符输出流管道与目标文件接通
    14. // Writer fw = new FileWriter("file-io-app/src/out08.txt"); // 覆盖管道,每次启动都会清空文件之前的数据
    15. Writer fw = new FileWriter("file-io-app/src/out08.txt", true); // 覆盖管道,每次启动都会清空文件之前的数据
    16. fw.write(98);
    17. fw.write('a');
    18. fw.write('徐'); // 不会出问题了
    19. fw.write("\r\n"); // 换行
    20. // b.public void write(String c)写一个字符串出去
    21. fw.write("abc我是中国人");
    22. fw.write("\r\n"); // 换行
    23. // c.public void write(char[] buffer):写一个字符数组出去
    24. char[] chars = "abc我是中国人".toCharArray();
    25. fw.write(chars);
    26. fw.write("\r\n"); // 换行
    27. // d.public void write(String c ,int pos ,int len):写字符串的一部分出去
    28. fw.write("abc我是中国人", 0, 5);
    29. fw.write("\r\n"); // 换行
    30. // e.public void write(char[] buffer ,int pos ,int len):写字符数组的一部分出去
    31. fw.write(chars, 3, 5);
    32. fw.write("\r\n"); // 换行
    33. // fw.flush();// 刷新后流可以继续使用
    34. fw.close(); // 关闭包含刷线,关闭后流不能使用
    35. }

    缓冲字节流

    BufferedInputStream & BufferedOutputStream

    1. public static void main(String[] args) {
    2. try (
    3. // 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
    4. // 1、创建一个字节输入流管道与原视频接通
    5. InputStream is = new FileInputStream("D:\\resources\\newmeinv.jpeg");
    6. // a.把原始的字节输入流包装成高级的缓冲字节输入流
    7. InputStream bis = new BufferedInputStream(is);
    8. // 2、创建一个字节输出流管道与目标文件接通
    9. OutputStream os = new FileOutputStream("D:\\resources\\newmeinv222.jpeg");
    10. // b.把字节输出流管道包装成高级的缓冲字节输出流管道
    11. OutputStream bos = new BufferedOutputStream(os);
    12. ) {
    13. // 3、定义一个字节数组转移数据
    14. byte[] buffer = new byte[1024];
    15. int len; // 记录每次读取的字节数。
    16. while ((len = bis.read(buffer)) != -1){
    17. bos.write(buffer, 0 , len);
    18. }
    19. System.out.println("复制完成了!");
    20. } catch (Exception e){
    21. e.printStackTrace();
    22. }
    23. }

    缓冲字符流

    BufferedReader & BufferedWriter

    1. public static void main(String[] args) {
    2. try(
    3. // 1、创建缓冲字符输入流管道与源文件接通
    4. BufferedReader br = new BufferedReader(new FileReader("io-app2/src/csb.txt"));
    5. // 5、定义缓冲字符输出管道与目标文件接通
    6. BufferedWriter bw = new BufferedWriter(new FileWriter("io-app2/src/new.txt"));
    7. ) {
    8. // 2、定义一个List集合存储每行内容
    9. List data = new ArrayList<>();
    10. // 3、定义循环,按照行读取文章
    11. String line;
    12. while ((line = br.readLine()) != null){
    13. data.add(line);
    14. }
    15. System.out.println(data);
    16. // 4、排序
    17. // 自定义排序规则
    18. List sizes = new ArrayList<>();
    19. Collections.addAll(sizes, "一","二","三","四","五","陆","柒","八","九","十","十一");
    20. Collections.sort(data, new Comparator() {
    21. @Override
    22. public int compare(String o1, String o2) {
    23. // o1 八.,....
    24. // o2 柒.,....
    25. return sizes.indexOf(o1.substring(0, o1.indexOf(".")))
    26. - sizes.indexOf(o2.substring(0, o2.indexOf(".")));
    27. }
    28. });
    29. System.out.println(data);
    30. // 6、遍历集合中的每行文章写出去,且要换行
    31. for (String datum : data) {
    32. bw.write(datum);
    33. bw.newLine(); // 换行
    34. }
    35. } catch (Exception e) {
    36. e.printStackTrace();
    37. }
    38. }

    字符转换流

    当读取固定字符集,或者输出为固定字符集时

    1. public static void main(String[] args) throws Exception {
    2. // 代码UTF-8 文件 GBK "D:\\resources\\data.txt"
    3. // 1、提取GBK文件的原始字节流。 abc 我
    4. // ooo oo
    5. InputStream is = new FileInputStream("D:\\resources\\data.txt");
    6. // 2、把原始字节流转换成字符输入流
    7. // Reader isr = new InputStreamReader(is); // 默认以UTF-8的方式转换成字符流。 还是会乱码的 跟直接使用FileReader是一样的
    8. Reader isr = new InputStreamReader(is , "GBK"); // 以指定的GBK编码转换成字符输入流 完美的解决了乱码问题
    9. BufferedReader br = new BufferedReader(isr);
    10. String line;
    11. while ((line = br.readLine()) != null){
    12. System.out.println(line);
    13. }
    14. }
    15. public static void main(String[] args) throws Exception {
    16. // 1、定义一个字节输出流
    17. OutputStream os = new FileOutputStream("io-app2/src/out03.txt");
    18. // 2、把原始的字节输出流转换成字符输出流
    19. // Writer osw = new OutputStreamWriter(os); // 以默认的UTF-8写字符出去 跟直接写FileWriter一样
    20. Writer osw = new OutputStreamWriter(os , "GBK"); // 指定GBK的方式写字符出去
    21. // 3、把低级的字符输出流包装成高级的缓冲字符输出流。
    22. BufferedWriter bw = new BufferedWriter(osw);
    23. bw.write("我爱中国1~~");
    24. bw.write("我爱中国2~~");
    25. bw.write("我爱中国3~~");
    26. bw.close();
    27. }

    对象输入输出流

    将对象写入到磁盘,或者从磁盘读取到内存。需要对象实现序列化

    1. public static void main(String[] args) throws Exception {
    2. // 1、创建学生对象
    3. Student s = new Student("陈磊", "chenlei","1314520", 21);
    4. // 2、对象序列化:使用对象字节输出流包装字节输出流管道
    5. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io-app2/src/obj.txt"));
    6. // 3、直接调用序列化方法
    7. oos.writeObject(s);
    8. // 4、释放资源
    9. oos.close();
    10. System.out.println("序列化完成了~~");
    11. }
    12. public static void main(String[] args) throws Exception {
    13. // 1、创建对象字节输入流管道包装低级的字节输入流管道
    14. ObjectInputStream is = new ObjectInputStream(new FileInputStream("io-app2/src/obj.txt"));
    15. // 2、调用对象字节输入流的反序列化方法
    16. Student s = (Student) is.readObject();
    17. System.out.println(s);
    18. }

     序列化的版本需要和反序列时一致,否则报错

    打印流

    PrintStream & PrintWriter

    作用:可以方便、高效打印数据到文件中去,例如直接打印整性,Boolean,都可以直接写入流

    • 打印数据功能上是一模一样的,都是使用方便,性能高效(核心优势)
    • PrintStream继承自字节输出流OutputStream,支持写字节数据的方法。
    • PrintWriter继承自字符输出流Writer,支持写字符数据出去。
    1. public static void main(String[] args) throws Exception {
    2. // 1、创建一个打印流对象
    3. // PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt"));
    4. // PrintStream ps = new PrintStream(new FileOutputStream("io-app2/src/ps.txt" , true)); // 追加数据,在低级管道后面加True
    5. // PrintStream ps = new PrintStream("io-app2/src/ps.txt" );
    6. PrintWriter ps = new PrintWriter("io-app2/src/ps.txt"); // 打印功能上与PrintStream的使用没有区别
    7. ps.println(97);
    8. ps.println('a');
    9. ps.println(23.3);
    10. ps.println(true);
    11. ps.println("我是打印流输出的,我是啥就打印啥");
    12. ps.close();
    13. }

    Properties

     

    1. public static void main(String[] args) throws Exception {
    2. // 需求:使用Properties把键值对信息存入到属性文件中去。
    3. Properties properties = new Properties();
    4. properties.setProperty("admin", "123456");
    5. properties.setProperty("dlei", "003197");
    6. properties.setProperty("heima", "itcast");
    7. System.out.println(properties);
    8. /**
    9. 参数一:保存管道 字符输出流管道
    10. 参数二:保存心得
    11. */
    12. properties.store(new FileWriter("io-app2/src/users.properties")
    13. , "this is users!! i am very happy! give me 100!");
    14. }
    15. public static void main(String[] args) throws Exception {
    16. // 需求:Properties读取属性文件中的键值对信息。(读取)
    17. Properties properties = new Properties();
    18. System.out.println(properties);
    19. // 加载属性文件中的键值对数据到属性对象properties中去
    20. properties.load(new FileReader("io-app2/src/users.properties"));
    21. System.out.println(properties);
    22. String rs = properties.getProperty("dlei");
    23. System.out.println(rs);
    24. String rs1 = properties.getProperty("admin");
    25. System.out.println(rs1);
    26. }

    Commons-IO

    简单的API说明

    1. public static void main(String[] args) throws Exception {
    2. // // 1.完成文件复制!
    3. IOUtils.copy(new FileInputStream("D:\\resources\\hushui.jpeg"),
    4. new FileOutputStream("D:\\resources\\hushui2.jpeg"));
    5. // // 2.完成文件复制到某个文件夹下!
    6. FileUtils.copyFileToDirectory(new File("D:\\resources\\hushui.jpeg"), new File("D:/"));
    7. // 3.完成文件夹复制到某个文件夹下--包含子目录文件!
    8. FileUtils.copyDirectoryToDirectory(new File("D:\\resources") , new File("D:\\new"));
    9. FileUtils.deleteDirectory(new File("D:\\new"));
    10. //删除文件夹包含子文件,目录
    11. FileUtils.deleteDirectory(new File("D:\\new"));
    12. }

    更多API,可以参考

    【java基础】-【api】commons.io常用类和方法_lydiamosiying的博客-CSDN博客

  • 相关阅读:
    滑膜间充质干细胞复合壳聚糖水凝胶/角蛋白壳聚糖水凝胶复合材料/壳聚糖/海藻酸纳复合水凝胶的制备
    搜维尔科技:【软件篇】TechViz是一款专为工程设计的专业级3D可视化软件
    [附源码]计算机毕业设计水果管理系统Springboot程序
    硬件基础 -- D/A数字模拟信号
    js input 正则保留2位小数中文拼音输入问题 + 限制输入整数的方案
    Matlab:Matlab 软件学习之Matlab内置的各种系统的简介、案例应用(基于Simulink模糊控制仿真/二阶瞬态震荡电路案例)之详细攻略
    【免费赠送源码】Springboot喵喵宠物医院管理系统ti5f6计算机毕业设计-课程设计-期末作业-毕设程序代做
    【N年测试总结】论提升测试效率和质量的思路
    【AHK】任务栏调节音量/边缘滚动调节/边缘触发
    站稳前沿消费趋势,IU酒店持续领跑轻中端品牌
  • 原文地址:https://blog.csdn.net/qq_33753147/article/details/127793541