Java的簡單理解(25)---(隨機訪問檔案)RandomAccessFile

weixin_34146805發表於2018-07-11
3532835-93b18ce3b780a64a
Java

RandomAccessFile

/**
 * 檔案的分割
 * 1. 分割的塊數     n塊
 * 2. 每一塊的大小   blocksize
 * 3. 最後:總的檔案大小 - (n - 1) * blocksize
 */
public void test(){

    try {
        File file = new File("E:/xp/test/a.txt");
        RandomAccessFile rnd = new RandomAccessFile(file,"r");

        rnd.seek(10);
        byte[] car = new byte[1024];
        int len = 0;

        while ((len = rnd.read(car)) != -1) {
            if (len >= 200) {
                System.out.println(new String(car,0,120));
            } else {
                System.out.println(new String(car,0,len));
            }
        }

        rnd.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

檔案分割的思路

  • 第一步:分割的準備
    • 塊數
    • 確定每塊大小
    • 每塊的名稱
  • 第二步
    • 分割
    • 第幾塊,每塊的起點,實際大小
    • 檔案分割
public class SplitFile {

    // 檔案的路徑
    private String filePath;

    // 檔名
    private String fileName;

    // 檔案大小
    private long length;

    // 塊數
    private int size;

    // 每塊的大小
    private long blockSize;

    // 每塊的名稱
    private List<String> blockPath;

    public SplitFile() {
        blockPath = new ArrayList<>();
    }

    public SplitFile(String filePath) {
        this(filePath,1024);
    }

    public SplitFile(String filePath,long blockSize){
        this();
        this.filePath = filePath;
        this.blockSize = blockSize;
        init();
    }

    /**
     * 初始化操作,計算塊數,確定檔名
     */
    public void init() {
        File src = null;
        if (filePath == null || !((src = new File(filePath)).exists())){
            return;
        }

        if (src.isDirectory()) {
            return;
        }

        // 檔名
        this.fileName = src.getName();

        // 檔案的實際大小
        this.length = src.length();

        // 修正每塊大小
        if (this.blockSize > length) {
            this.blockSize = length;
        }
        // 確定塊數
        size = (int) Math.ceil(length * 1.0 / this.blockSize);

    }

    /**
     * 確定檔名
     */
    public void initPathName(String destPath) {
        for (int i = 0; i < size; i++){
            this.blockPath.add(destPath + "/" + this.fileName + ".part" + i);
        }
    }

    /**
     * 檔案的分割
     * @param destPath 分割檔案存放目錄
     */
    public void split(String destPath){

        // 確定檔案的路徑
        initPathName(destPath);

        long beginPos = 0;// 起始點
        long actualBlockSize = blockSize;//實際大小

        // 計算所有塊的大小
        for (int i = 0; i < size; i++) {

            if (i == size - 1){
                actualBlockSize = this.length - beginPos;
            }

            spiltDetail(i,beginPos,actualBlockSize);
            beginPos = beginPos + actualBlockSize;
        }
    }

    /**
     * 檔案的分割 輸入 輸出
     * 檔案的拷貝
     * @param idx 第幾塊
     * @param beginPos 起始點
     * @param actualBlockSize 實際大小
     */
    public void spiltDetail(int idx,long beginPos,long actualBlockSize){
        // 1. 建立源
        File src = new File(this.filePath);
        // 2. 目標檔案
        File dest = new File(this.blockPath.get(idx));
        // 3. 選擇流
        RandomAccessFile raf = null;
        BufferedOutputStream bos = null;
        try {
            raf = new RandomAccessFile(src,"r");
            bos = new BufferedOutputStream(new FileOutputStream(dest));

            // 讀取檔案
            raf.seek(beginPos);
            // 快取
            byte[] flush = new byte[1024];
            // 接收長度
            int len = 0;
            while (-1 != (len = raf.read(flush))) {
                // 寫出
                if (actualBlockSize - len >= 0) {
                    bos.write(flush,0,len);
                    actualBlockSize = actualBlockSize - len;
                } else {
                    bos.write(flush,0, (int) actualBlockSize);
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

合併檔案的兩種方法
1.
public void mergeFile1(String destPath) {
    // 建立源
    File dest = new File(destPath);
    // 選擇流
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(dest,true)); // true: 追加而不是替換
        for (int i = 0; i < this.blockPath.size(); i++) {
            bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));

            // 緩衝區
            byte[] flush = new byte[1024];
            // 接收長度
            int len = 0;
            while ((len = bis.read(flush)) != -1) {
                bos.write(flush,0,len);
            }

            bos.flush();

            bis.close();
            bos.close();

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
2.
public void mergeFile2(String destPath) {
    // 建立源
    File file = new File(destPath);

    // 選擇流
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    SequenceInputStream sis = null;

    // 建立一個容器
    Vector<InputStream> vector = new Vector<>();

    for (int i = 0; i < this.blockPath.size(); i++){
        try {
            vector.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    try {
        bos = new BufferedOutputStream(new FileOutputStream(file,true));
        sis = new SequenceInputStream(vector.elements());

        byte[] flush = new byte[1024];
        int len = 0;

        while ((len = sis.read(flush)) != -1) {
            bos.write(flush,0,len);
        }

        bos.flush();

        bos.close();
        bis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

相關文章