Performance drawbacks File copying

Performance drawbacks File copying


Performance drawbacks File copying

Summary: By the end of this tutorial "Performance drawbacks File copying", you will come to know the reasons of slow performance in file copying. Gives links to other programs to increase speed of copying.

Performance drawbacks of FileInputStream and FileOutputStream

Following is the for loop used in the FileToFile1.java program.

Observe the above for loop. In the loop header, there is fis.read() and in the loop body fos.write(k). That is, the execution control is shifting, for each byte read, from the execution context area of FileInputStream to FileOutputStream and again back to read the next byte. This exection control shifting between two memories takes a long time than actual reading and writing. More the bytes, more are the shiftings. For this low performance, the programmer prefers this type of copying (using FileInputStream and FileOutputStream) when the file size is very small of few thousand bytes. For larger data files, buffering is used in the forthcoming programs. By buffering, thousands of times the performance increases.

To put it again, file copying with FileInputStream is very slow and to increase the performance, these streams can be chained with BufferedInputStream and DataInputStream. When chained, the affect will be atleast 2000 times performance increases.

Read the above links to increase the speed of file copying and pass your comments on this tutorial "Performance drawbacks File copying".

Three Questions for You?

1. Why Java introduced StringBuilder with the same functionality (usage) of StringBuffer?
2. Why split() method was introduced when Java has already StringTokenizer?
Ans: StringTokenizer comes with JDK1.0 version where as split() method comes with JDK1.4 version. StringTokenizer beongs to java.util package and split() is defined in String class of java.lang package. One major difference is, split() supports regular expressions but StringTokenizer does not. If tokens (independent words) are required in an array form for easy retrieval and operation, split() is preferred. No operations on elements and only retrieval, StringTokenizer is preferred. It is jsut only coding requirements.
3. Way ArrayList was introduced when Vector with similar functionality exists?
Ans: ArrayList methods are not synchronized but Vector are. For important and critical data operations, where data corruption or inconsistency should not prevail, prefer Vector and for unimportant prefer ArrayList. Vector performance is slow (as methods are synchronized) but ArrayList operations is very fast.

3 thoughts on “Performance drawbacks File copying

Leave a Reply

Your email address will not be published. Required fields are marked *