IO流-文件传输基础

*编码基础
机器上的文本文件,如果是直接创建的,就只认识ansi,
如果是粘贴过来的,则都认识
gbk:中文占用2个字节,英文占用1个字节
utf-8:中文占用3个字节,英文占用1个字节
java是双字节编码:utf-16be:中文占用2个字节,英文占用2个字节
转换成16进制输出时要与上0xff,屏蔽前面24位,取后8位
Integer.toHexString(b&0xff)
指定编码格式:byte[] bytes=s.getBytes(“指定编码”);
字节序列在转换成字符串的时候要指定编码格式:
String str=new String(bytes,“指定编码”);(指定的编码格式要与字节序列指定的编码格式相同)

BufferedOutputStream/BufferedInputStream
这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入
或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
从应用程序中把数据放入文件,相当于将一缸水倒入到另一个缸中:
FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了

java的文本(char)是16位无符号整数,是字符的unico编码(双字节编码)
文件是byte byte byte...的数据序列
文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)
序列化为byte的存储结果
字符流(Reader Writer)
字符的处理,一次处理一个字节
字符的底层仍然是基本的字节序列
字符流的基本实现
InputStreaReader 完成byte流解析问char流,按照编码解析
OutputStreamWrite 提供char流到byte流,按照编码处理

FileWriter:文件输出流,FileWriter()里除了文件路径,若再追加参数true,则不会删掉该文件,而是在该文件里将内容再追加一遍
字符流的过滤器
    BufferedReader   ---->readLine 一次读一行
    BufferedWriter/PrintWriter   ---->写一行


package com.imooc.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOUtil {
    /**
     * 读取指定文件内容,按照16进制输出到控制台
     * 并且每输出10个byte换行
     * @param fileName
     * 单字节读取不适合大文件,大文件效率很低
     */
    public static void printHex(String fileName)throws IOException{
        //把文件作为字节流进行读操作
        FileInputStream in = new FileInputStream(fileName);
        int b ;
        int i = 1;
        while((b = in.read())!=-1){
            if(b <= 0xf){
                //单位数前面补0
                System.out.print("0");
            }
            System.out.print(Integer.toHexString(b)+"  ");
            if(i++%10==0){
                System.out.println();
            }
        }
        in.close();
    }
    /**
     * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
     * @param fileName
     * @throws IOException
     */
    public static void printHexByByteArray(String fileName)throws IOException{
        FileInputStream in = new FileInputStream(fileName);
        byte[] buf = new byte[8 * 1024];
        /*从in中批量读取字节,放入到buf这个字节数组中,
         * 从第0个位置开始放,最多放buf.length个 
         * 返回的是读到的字节的个数
        */
        /*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大
        int j = 1; 
        for(int i = 0; i < bytes;i++){
            System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
            if(j++%10==0){
                System.out.println();
            }
        }*/
      int bytes = 0;
      int j = 1;
      while((bytes = in.read(buf,0,buf.length))!=-1){
          for(int i = 0 ; i < bytes;i++){
              System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
              if(j++%10==0){
                  System.out.println();
              }
          }
      }
      in.close();
    }
    /**
     * 文件拷贝,字节批量读取
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFile(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[8*1024];
        int b ;
        while((b = in.read(buf,0,buf.length))!=-1){
            out.write(buf,0,b);
            out.flush();//最好加上
        }
        in.close();
        out.close();
        
    }
    /**
     * 进行文件的拷贝,利用带缓冲的字节流
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));
        int c ;
        while((c = bis.read())!=-1){
            bos.write(c);
            bos.flush();//刷新缓冲区
        }
        bis.close();
        bos.close();
    }
    /**
     * 单字节,不带缓冲进行文件拷贝
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFileByByte(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        int c ;
        while((c = in.read())!=-1){
            out.write(c);
            out.flush();
        }
        in.close();
        out.close();
    }
    
}

转载于:https://my.oschina.net/dreambreeze/blog/994493

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值