user4498972
user4498972

Reputation:

Why buffer size has no effect on this file read operation

I am learning about Java I/O. So using buffered streams reduces time taken to read or write because if you use normal FileInputStream , you are fetching one byte every time read is invoked but if you use buffers you will fetch specified size of data and store it in memory. So I tried to see this in practice.

package javaIO;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class BufferTest {

    public static void main(String[] args) throws Exception{

        int starttime=(int) (System.currentTimeMillis()/1000);

        BufferedInputStream is=new BufferedInputStream(new FileInputStream("/home/anil/Downloads/Vidoes/batman.mp4"),65536);
//      InputStream is=new FileInputStream("/home/anil/Downloads/Vidoes/batman.mp4");
        int a=0;
        while((a=is.read())!=-1){

            System.out.println(a);
        }

        is.close();

        int endTime=(int) (System.currentTimeMillis()/1000);

        System.out.println("Took "+(endTime-starttime)+"seconds");
    }

}

I have set the buffer size for BufferedInputStream to 512,8192,65536. Each time it takes 87 seconds to complete the execution. So I tried using FileInputStream and still it takes 87 seconds to complete the execution. The size of the batman.mp4 is 24.7mb.

So what am I missing?

Upvotes: 1

Views: 400

Answers (1)

TDG
TDG

Reputation: 6161

What slows you down is the printing - System.out.println(a); - you always print 24.7M chars to the screen. Try to copy the file to another location - read it with different buffer sizes and write it again to a new file.

Upvotes: 1

Related Questions