开发者

read a file byte by byte then perform some operation every n bytes

I would like to know how can I read a file byte by byte then perform 开发者_StackOverflow中文版some operation every n bytes.

for example:

Say I have a file of size = 50 bytes, I want to divide it into blocks each of n bytes. Then each block is sent to a function for some operations to be done on those bytes. The blocks are to be created during the read process and sent to the function when the block reaches n bytes so that I don`t use much memory for storing all blocks.

I want the output of the function to be written/appended on a new file.

This is what I've reached to read, yet I don't know it it is right:

fc = new JFileChooser();
File f = fc.getSelectedFile();
FileInputStream in = new FileInputStream(f);
byte[] b = new byte[16];
in.read(b);

I haven't done anything yet for the write process.


You're on the right lines. Consider wrapping your FileInputStream with a BufferedInputStream, which improve I/O efficiency by reading the file in chunks.

The next step is to check the number of bytes read (returned by your call to read) and to hand-off the array to the processing function. Obviously you'll need to pass the number of bytes read to this method too in case the array was only partially populated.


So far your code looks OK. For reading binary files (as opposed to text files) you should indeed use FileInputStream (for reading text files, you should use a Reader, such as FileReader).

Note that you should check the return value from in.read(b);, because it might read less than 16 bytes if there are less than 16 bytes left at the end of the file.

Ofcourse you should add a loop to the program that keeps reading blocks of bytes until you reach the end of the file.

To write data to a binary file, use FileOutputStream. That class has a constructor that you can pass a flag to indicate that you want to append to an existing file:

FileOutputStream out = new FileOutputStream("output.bin", true);

Also, don't forget to call close() on the FileInputStream and FileOutputStream when you are done.

See the Java API documentation, especially the classes in the java.io package.


I believe that this will work:

final int blockSize = // some calculation
byte[] block = new byte[blockSize];
InputStream is = new FileInputStream(f);
try {
    int ret = -1;
    do {
        int bytesRead = 0;
        while (bytesRead < blockSize) {
            ret = is.read(block, bytesRead, blockSize - bytesRead);
            if (ret < 0)
                break; // no more data
            bytesRead += ret;
        }

        myFunction(block, bytesRead);
    } while (0 <= ret);
}
finally {
    is.close();
}

This code will call myFunction with blockSize bytes for all but possibly the last invocation.


It's a start.

You should check what read() returns. It can read fewer bytes than the size of the array, and also indicate that the end of the file is reached.

Obviously, you need to read() in a loop...

It might be a good idea to reuse the array, but that requires that the part that reads the array copies what it needs, rather than just keeping a reference to the array.


I think this is what you migth need

void readFile(String path, int n) {
    try {
        File f = new File(path);
        FileInputStream fis = new FileInputStream(f);
        int ret = 0;
        byte[] array = new byte[n];
        while(ret > -1) {
            ret = fis.read(array);
            doSomething(array, ret);
        }
                    fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜