• java的IO学习,字节流与字符流的编码讲解


    字节流与字符流

    字节流可以处理所有类型的数据(图片、视频等),在java中对应的类都为“stream”结尾
    1字节=8位二进制=具体存储空间
     
    字符流仅能处理纯文本的数据,在java中对应的类都是以“reader”或者“writer”结尾
    如汉字,符号等
     
     
     
    import org.junit.Test; 
    
    public class IOTest { 
     /**
      * BufferedInputStream BufferedOutputStream
      * 利用字节缓冲流实现文件的复制
      * @throws IOException
      * */
     @Test
     public void bufferedInputStreamAndbufferedOutputStream() throws IOException{
      //新建字节的输入输出
      InputStream in = new FileInputStream("hellow.txt");
      BufferedInputStream bStream = new BufferedInputStream(in);
     
      OutputStream out = new FileOutputStream("hellow2.txt");
      BufferedOutputStream bOutputStream = new BufferedOutputStream(out);
      //创建缓冲数组
      byte[] bytes = new byte[100];
      //进行复制
      int len = 0;
      while((len = bStream.read(bytes))!= -1)
      {
       bOutputStream.write(bytes, 0, len);
      }
      //关闭流
      bStream.close();
      bOutputStream.close();
     }
    /**
      * bufferdeReader And bufferedWriter
      * 利用缓冲流实现文件的复制
      * @throws IOException
      * **/
     @Test
     public void bufferdeReaderAndbufferedWriter() throws IOException {
      // 新建字符的输入输出
      Reader in = new FileReader("hellow.txt");
      BufferedReader bReader = new BufferedReader(in);
    
      Writer out = new FileWriter("hellow2.txt");
      BufferedWriter bWriter = new BufferedWriter(out);
      // 进行复制
      String line = null;
      int i = 0;
      while ((line = bReader.readLine()) != null) {
       if (i != 0)
        bWriter.write("
    ");
       bWriter.write(line, 0, line.length());
       i++;
      }
      // 关闭流
      bReader.close();
      bWriter.close();
     }
    
    /**
     *reader writer
     * 利用字符输入输出流, 完成 hello.txt 文件的复制.
     * 把该文件复制为 hello2.txt  
     */
    
     @Test
     public void readerAndwriter() throws IOException {
      //新建字符的输入输出
      Reader reader = new FileReader("hellow.txt");
      Writer writer = new FileWriter("hellow2.txt");
      //定义数组,用于读写文件
      char[] cbuf = new char[100];
      //读写文件
      int len;
      while((len = reader.read(cbuf)) != -1)
      {
       writer.write(cbuf, 0, len);
      }
      //关闭流
      reader.close();
      writer.close();
     }
    
    /**
    *InputStream OutputStream  
    * 利用字节输入输出流, 完成 hello.txt 文件的复制. 
    * 把该文件复制为 hello2.txt 
    * @throws IOException 
    */ 
    @Test 
    public void testCopyFile() throws IOException{ 
    //1. 创建定位到 hello.txt 的文件的输入流 
    InputStream in = new FileInputStream("枚举类.avi"); 
    
    //2. 创建定位到 hello2.txt 的文件输出流 
    OutputStream out = new FileOutputStream("枚举类2.avi"); 
    
    //3. 创建一个 byte 数组, 用于读写文件 
    byte [] buffer = new byte[1024 * 10]; 
    int len = 0; 
    
    //4. 读写文件: 
    //in.read(buffer); out.write(buffer, 0, len); 
    while((len = in.read(buffer)) != -1){ 
    out.write(buffer); 
    } 
    
    //5. 关闭流资源. 
    out.close(); 
    in.close(); 
    } 
    
    /** 
    * 测试字节输出流  OutputStream
    * @throws IOException 
    */ 
    @Test 
    public void testOutputStream() throws IOException{ 
    OutputStream out = new FileOutputStream("abcd.txt"); 
    
    String content = "www.atguigu.com
    Hello Java!"; 
    out.write(content.getBytes()); 
    
    out.close(); 
    } 
    
    /** 
    * 测试字符输入流.  Reader
    * @throws IOException 
    */ 
    @Test 
    public void testReader() throws IOException{ 
    //利用字符输入流读取 hello.txt 文档的内容, 输出到控制台. 
    Reader reader = new FileReader("hello.txt"); 
    
    char [] buffer = new char[10]; 
    int len = 0; 
    
    while((len = reader.read(buffer)) != -1){ 
    for(int i = 0; i < len; i++){ 
    System.out.print(buffer[i]); 
    } 
    } 
    
    reader.close(); 
    } 
    
    /** 
    * 测试字节输入流 InputStream
    * @throws IOException 
    */ 
    @Test 
    public void testInputStream() throws IOException{ 
    //1. 创建了一个字节输入流. 
    InputStream in = new FileInputStream("hello.txt"); 
    
    //2. 读取文件的内容 
    //2.1 第一读取一个字节. 效率很低, 不建议这样读. -1 表示读取到文件的结尾处 
    //     int result = in.read(); 
    //    
    //     while(result != -1){ 
    //     System.out.print((char)result); 
    //     result = in.read(); 
    //     } 
    
    //2.2 一次读取一组: 一组字符. 
    //返回一次实际读取的字节数, 若为 -1 表示读取到文件的结尾 
    //     byte [] buffer = new byte[10]; 
    //     int len = 0; 
    //    
    //     while((len = in.read(buffer)) != -1){ 
    //     for(int i = 0; i < len; i++){ 
    //     System.out.print((char)buffer[i]); 
    //     } 
    //     } 
    
    //2.3 把内容读取到字节数组的部分连续的元素中. 
    byte [] result = new byte[1024 * 10]; 
    in.read(result, 10, in.available()); 
    
    //3. 关闭流资源 
    in.close(); 
    } 
    
    /** 
    * File: 代表物理的意义的文件或目录 
    * @throws IOException 
    */ 
    @Test 
    public void testFile() throws IOException{ 
    //1. 创建 File 对象 
    File file = new File("hello.txt"); 
    
    //2. 测试 File 对象的方法. 
    //2.1 文件名相关的方法 
    String fileName = file.getName(); 
    System.out.println(fileName); 
    
    //2.2 访问文件的绝对路径 
    String path = file.getAbsolutePath(); 
    System.out.println(path); 
    
    //2.3 为文件重命名 
    //file.renameTo(new File("d:\hello.txt")); 
    
    //3. 文件检测相关的方法 
    System.out.println(file.exists()); 
    File dir = new File("atguigu"); 
    System.out.println(dir.isFile()); 
    
    //4. 获取文件的常规信息 
    System.out.println(file.length()); 
    
    //5. 文件操作相关. 
    File file2 = new File("abcd.txt"); 
    file2.createNewFile(); 
    } 
    
    } 
  • 相关阅读:
    IP寻址方式三
    IP通信基础4
    IP通信基础3
    IP通信基础2
    IP通信基础 1
    IP通信基础
    IP通信原理第二周
    设备选型
    常用virsh命令记录
    [转]enable spice html5 console access in openstack kilo(centos)
  • 原文地址:https://www.cnblogs.com/6tian/p/4076550.html
Copyright © 2020-2023  润新知