public static int copy ( Reader reader , Writer writer ) throws IOException { Params . notNull ( reader , "Reader" ) ; Params . notNull ( writer , "Writer" ) ; if ( ! ( reader instanceof BufferedReader ) ) { reader = new BufferedReader ( reader ) ; } if ( ! ( writer instanceof BufferedWriter ) ) { writer = new BufferedWriter ( writer ) ; } int charsCount = 0 ; try { char [ ] buffer = new char [ BUFFER_SIZE ] ; for ( ; ; ) { int readChars = reader . read ( buffer ) ; if ( readChars == - 1 ) { break ; } charsCount += readChars ; writer . write ( buffer , 0 , readChars ) ; } } finally { close ( reader ) ; close ( writer ) ; } return charsCount ; }
Copy characters from a reader to a given writer then close both character streams .