JAVA IO流(2)

字节流的使用

InputStream、OutputStream 抽象类

InputStream 抽象了应用程序读取数据的方式
OutputStream抽象了应用程序写出数据的方式

EOF = End 读到-1就读到结尾

输入流基本方法–>读操作

int b = in.read();读取一个字节无符号填充到int低八位。-1是EOF
in.read(byte[] buf) 读取数据填充到字节数组buf
in.read(byte[] buf, int start, int size)读取数据到字节数组buf,从buf的start位置开始存放size长度的数据。

输出流基本方法–>写操作

out.write(int b) 写出一个byte到流,b的低8位
out.write(byte[] buf)将buf字节数组都写入到流
out.write(byte[] buf, int start, int size)字节数组buf从start位置开始写size长度的字节到流

FileInputStream–>实现了在文件上读取数据

FileOutputStream–>实现了向文件中写出byte数据的方法

DataOutputStream\DataInputStream

对”流”功能的扩展,可以更加方便的读取int,long,字符等类型数据

DataOutputStream

writeInt()/writeDouble()/writeUTF()->采用utf-8编码写出/writeChars()->采用utf-16be编码写出

BufferedInputStream/BufferedInputStream

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

字符流的使用

文本和文本文件

java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
文件是byte byte byte…的数据序列
文本文件是文本(char)序列按照某种编码方案(utf-8、utf-16be、gbk)序列化为byte的存储

字符流(Reader Writer)

字符的处理,一次处理一个字符
字符的底层仍然是基本的字节序列

字符流的基本实现

InputStreamReader 按照编码,完成byte流解析为char流OutputStreamWriter 提供了char流到byte流,按照编码处理

FileReader/FileWriter 主要是对文件进行读写

字符流的过滤器

BufferedReader —>readLine 一次读一行
BufferedWriter/PrintWriter —>写一行

代码

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 IOUtils {

    /**
     * 读取指定文件内容,按照16进制输出到控制台
     * 并且每输出10个byte换行
     * @param fileName
     * @throws IOException 
     * 单字节读取,不适合大文件
     */
    public static void printHex(String fileName) throws IOException{
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(fileName);
            int b = 0;
            int i = 1; //换行
            while((b = fin.read()) != -1){
                if(b < 0xf){
                    System.out.print("0");
                }
                System.out.print(Integer.toHexString(b) + " ");
                if(i++%10 == 0){
                    System.out.println();
                }
            }
        }finally{
            if(fin != null){
                fin.close();
            }
        }
    }

    /**
     * 读取指定文件内容,按照16进制输出到控制台
     * 并且每输出10个byte换行
     * @param fileName
     * @throws IOException
     * 批量读取,对大文件而言,效率高
     */
    public static void printHexByByteArray(final String fileName) throws IOException{
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(fileName);
            byte[] buf = new byte[8*1024];
            int bytes = 0;
            int j = 1;
            while((bytes = fin.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();
                }
            }
        }finally{
            if(fin != null){
                fin.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 = null;
        FileOutputStream out = null;
        try{
            in = new FileInputStream(srcFile);      //读操作
            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();
            }
        }finally{
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
        }
    }

    /**
     * 
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFileByBufferByte(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 = 0;
        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 fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        int c = 0;
        while((c = fis.read()) != -1){
            fos.write(c);
            fos.flush();
        }
        fis.close();
        fos.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值