Reading file chunk by chunk

前端 未结 3 1918
名媛妹妹
名媛妹妹 2020-12-02 00:50

I want to read a file piece by piece. The file is split up into several pieces which are stored on different types of media. What I currently do is call each seperate piece

3条回答
  •  星月不相逢
    2020-12-02 01:14

    See InputSteram.read(byte[]) for reading bytes at a time.

    Example code:

    try {
        File file = new File("myFile");
        FileInputStream is = new FileInputStream(file);
        byte[] chunk = new byte[1024];
        int chunkLen = 0;
        while ((chunkLen = is.read(chunk)) != -1) {
            // your code..
        }
    } catch (FileNotFoundException fnfE) {
        // file not found, handle case
    } catch (IOException ioE) {
        // problem reading, handle case
    }
    

提交回复
热议问题