Java IO流 ---字節流 案例分析

import java.io.*;

/**
 *  IO(輸入流、輸出流)
 字節流、字符流
 1.字節流
 1)InputStreamOutputStream
 InputStream抽象了應用程序讀取數據的方式
 OutputStream抽象了應用程序寫出數據的方式
 2)EOF = End   讀到-1就讀到結尾
 3)輸入流基本方法
 int  b = in.read();讀取一個字節無符號填充到int低八位.-1EOF
 in.read(byte[] buf)
 in.read(byte[] buf,int start,int size)
 4)輸出流基本方法
 out.write(int b)  寫出一個byte到流,b的低8 out.write(byte[] buf)buf字節數組都寫入到流
 out.write(byte[] buf,int start,int size)

 5)FileInputStream--->具體實現了在文件上讀取數據
 6)FileOutputStream 實現了向文件中寫出byte數據的方法
 7)DataOutputStream/DataInputStream
 ""功能的擴展,可以更加方面的讀取int,long,字符等類型數據
 DataOutputStream
 writeInt()/writeDouble()/writeUTF()

 8)BufferedInputStream&BufferedOutputStream
 這兩個流類位IO提供了帶緩衝區的操作,一般打開文件進行寫入
 或讀取操作時,都會加上緩衝,這種流模式提高了IO的性能
 從應用程序中把輸入放入文件,相當於將一缸水倒入到另一個缸中:
 FileOutputStream--->write()方法相當於一滴一滴地把水轉移過去
 DataOutputStream-->writeXxx()方法會方便一些,相當於一瓢一瓢把水轉移過去
 BufferedOutputStream--->write方法更方便,相當於一飄一瓢先放入桶中,再從桶中倒入到另一個缸中,性能提高了
 */
public class IOUtil {
    /**
     * 讀取指定文件內容,按照16進制輸出到控制檯
     * 並且每輸出10byte換行
     * @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];//建立一個8k大小的數組
      /*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){//10個就換行
                    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();
    }

}

===================================================================================================

import java.io.File;
import java.io.IOException;

/**
 * 測試
 */
public class IOUtilTest1 {
    public static void main(String[] args) {
        try {
            IOUtil.printHex("C:\\Users\\hasee\\Desktop\\demo\\海蔘多少.txt");
            IOUtil.printHexByByteArray("C:\\Users\\hasee\\Desktop\\demo\\海蔘多少.txt");
            IOUtil.copyFile(new File("C:\\Users\\hasee\\Desktop\\demo\\文件一.txt"),
                            new File("C:\\Users\\hasee\\Desktop\\demo\\文件二.txt"));
            IOUtil.copyFileByBuffer(new File("C:\\Users\\hasee\\Desktop\\demo\\文件三.txt"),
                                    new File("C:\\Users\\hasee\\Desktop\\demo\\文件四.txt"));


        } catch (IOException e) {

            e.printStackTrace();
        }

        /*如果系統報這個錯: java.io.FileNotFoundException: C:\Users\hasee\Desktop\demo\海蔘多少 (系統找不到指定的文件。)
        * 那就是拓展名的原因,解決辦法:電腦工具欄-文件夾選項-查看-高級設置:-隱藏已知文件類型的擴展名(勾選去掉-應用-確定)
        * */
    }
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章