zoukankan      html  css  js  c++  java
  • java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

    BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStreamReader
    每一种流都介绍到了,详细一目了然的详细

    下面是字节流常见操作的四种方式:

      1 import java.io.BufferedOutputStream;
      2 import java.io.FileInputStream;
      3 import java.io.FileOutputStream;
      4 import java.io.BufferedInputStream;
      5 import java.io.IOException;
      6 
      7 /*
      8  * 四种方式实现大文件数据的读取写入--->复制
      9  * 1.基本字节流一次读取一个字节
     10  * 2.基本字节流一次读取一个字节数组
     11  * 3.高效字节流一次读取一个字节
     12  * 4.高校字节流一次读取一个字节数组
     13  * 
     14  */
     15 public class Test2 {
     16     public static void main(String[] args) throws IOException {
     17         long start = System.currentTimeMillis();
     18 
     19         // method1("E:\b.txt", "c.txt");    //基本字节流一次一个字节
     20         // method2("E:\b.txt", "c.txt");    //一次一个字节数组
     21         // method3("E:\b.txt", "c.txt");    //高效字节流一次一个字节
     22         method4("E:\b.txt", "c.txt");       //高效字节流一次一个字节数组
     23 
     24         long end = System.currentTimeMillis();
     25         System.out.println("共耗时: " + (end - start) + "毫秒");
     26     }
     27 
     28     // 1.基本方法字节流一次读取一个字节
     29     public static void method1(String srcPath, String destPath)
     30             throws IOException {
     31         // 读取数据对象
     32         FileInputStream fis = new FileInputStream(srcPath);
     33         // 写入数据目标文件路径
     34         FileOutputStream fos = new FileOutputStream(destPath);
     35         // 数据读写
     36         // 直接以单字节读取
     37         int by = 0;
     38         while ((by = fis.read()) != -1) {
     39             fos.write(by);
     40         }
     41         // 关闭流
     42         fos.close();
     43         fis.close();
     44     }
     45 
     46     // 2.基本字节读取一次读取一个数组
     47     public static void method2(String srcPath, String destPath)
     48             throws IOException {
     49         // 数据读取的对象封装
     50         FileInputStream fis = new FileInputStream(srcPath);
     51         // 数据写入对象封装
     52         FileOutputStream fos = new FileOutputStream(destPath);
     53         // 数据的读写
     54         byte[] bys = new byte[1024];
     55         int len = 0;
     56         while ((len = fis.read(bys)) != -1) {
     57             fos.write(bys, 0, len);
     58         }
     59         // 关闭流
     60         fis.close();
     61         fos.close();
     62     }
     63 
     64     // 3.高效字节流一次读取一个字节
     65     public static void method3(String srcPath, String destPath)
     66             throws IOException {
     67         // 数据读取对象封装
     68         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
     69                 srcPath));
     70         // 数据写入对象封装
     71         BufferedOutputStream bos = new BufferedOutputStream(
     72                 new FileOutputStream(destPath));
     73 
     74         // 数据读写操作
     75         int by = 0;
     76         while ((by = bis.read()) != -1) {
     77             bos.write(by);
     78         }
     79 
     80         // 关闭流
     81         bos.close();
     82         bis.close();
     83     }
     84 
     85     // 4.高效字节流读取一个字节数组
     86     public static void method4(String srcPath, String destPath)
     87             throws IOException {
     88         // 数据读取对象封装
     89         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
     90                 srcPath));
     91         // 数据写入对象
     92         BufferedOutputStream bos = new BufferedOutputStream(
     93                 new FileOutputStream(destPath));
     94 
     95         // 数据读写操作
     96         byte[] bys = new byte[1024];
     97         int len = 0;
     98         while ((len = bis.read(bys)) != -1) {
     99             bos.write(bys, 0, len);
    100         }
    101 
    102         // 关闭流
    103         bos.close();
    104         bis.close();
    105     }
    106 }

    下面是字符流常见操作的四种方式:

      1 import java.io.BufferedReader;
      2 import java.io.BufferedWriter;
      3 import java.io.FileInputStream;
      4 import java.io.FileReader;
      5 import java.io.FileWriter;
      6 import java.io.IOException;
      7 import java.io.InputStreamReader;
      8 
      9 /*
     10  * 需求:复制文本文件
     11  *     分析:如果使用Windows记事本打开之后能读懂就是用字符流,显然此处使用字符流
     12  *     字符流有五种方式:尤其是第五种字符高效流的特殊方式
     13  *     数据源的地址以及目标地址也应该用String封装
     14  */
     15 public class CopyFileDemo {
     16     public static void main(String[] args) throws IOException {
     17         // 字符流读取源文件对象
     18         // method1(); // 普通方法的单字节读取
     19         // method2(); //普通的字符数组
           //method3(); //高效的字节数组
    20 method4(); //高效的字节数组 21 method5(); //按行读取写入 ,这个很常用!! 22 } 23 24 private static void method1() throws IOException { 25 // 源文件对象,读取数据 26 // InputStreamReader isr = new InputStreamReader(new FileInputStream( 27 // "c:\a.txt")); 28 FileReader fr = new FileReader("c:\a.txt"); 29 // 目标文件对象,写入数据 30 FileWriter fw = new FileWriter("dest.txt"); 31 32 // 数据的复制 33 int ch = 0; 34 while ((ch = fr.read()) != -1) { 35 fw.write(ch); 36 } 37 38 // 释放资源 39 fr.close(); 40 fw.close(); 41 } 42 43 // 字符数组读取复制文件 44 private static void method2() throws IOException { 45 // 数据源文件对象,读取数据 46 FileReader fr = new FileReader("c:\a.txt"); 47 // 目标文件对象,写入数据 48 FileWriter fw = new FileWriter("dest.txt"); 49 50 // 数据复制 51 char[] chs = new char[1024]; 52 int len = 0; 53 while ((len = fr.read(chs)) != -1) { 54 fw.write(chs, 0, len); 55 fw.flush(); // 字符流在写入数据时一定记得刷新! 56 } 57 58 // 释放资源 59 fr.close(); 60 fw.close(); 61 } 62 63 // 高效字符数组复制文件--->单字符模式 64 public static void method3() throws IOException { 65 // 数据源文件对象,读取数据 66 BufferedReader br = new BufferedReader(new FileReader("c:\a.txt")); 67 // 目标文件对象,写入数据 68 BufferedWriter bw = new BufferedWriter(new FileWriter("dest1")); 69 70 // 文件的复制 71 int ch = 0; 72 while ((ch = br.read()) != -1) { 73 bw.write(ch); 74 } 75 76 // 释放资源 77 bw.close(); 78 br.close(); 79 } 80 81 // 高效字符数组复制文本文件--->字符数组模式 82 private static void method4() throws IOException { 83 // 数据源文件对象,读取数据 84 BufferedReader br = new BufferedReader(new FileReader("c:\a.txt")); 85 // 目标文件对象,写入数据 86 BufferedWriter bw = new BufferedWriter(new FileWriter("dest1.txt")); 87 88 // 数据复制 89 char[] chs = new char[1024]; 90 int len = 0; 91 while ((len = br.read(chs)) != -1) { 92 bw.write(chs); 93 bw.flush(); 94 } 95 96 // 释放资源 97 br.close(); 98 bw.close(); 99 } 100 101 // 高效字符流的特殊方法--->按行读取写入~!这个是狠狠常用的 102 private static void method5() throws IOException { 103 // 数据源文件对象,读取数据 104 BufferedReader br = new BufferedReader(new FileReader("c:\a.txt")); 105 // 目标文件对象,写入数据 106 BufferedWriter bw = new BufferedWriter(new FileWriter("dest.txt")); 107 108 // 数据复制操作 109 String line = null; 110 while ((line = br.readLine()) != null) { 111 bw.write(line); 112 bw.newLine(); 113 bw.flush(); 114 } 115 116 // 释放资源 117 br.close(); 118 bw.close(); 119 } 120 }
  • 相关阅读:
    table 表格的增删和修改
    js实现单双行文本溢出添加省略号
    C++
    PAT乙级 1029 旧键盘 (C++ python3)
    图论
    图论
    图论
    springcloud(二):注册中心Eureka
    apollo配置中心初探
    Apollo 配置详细步骤(Windows环境)
  • 原文地址:https://www.cnblogs.com/fuck1/p/5373679.html
Copyright © 2011-2022 走看看