Copy chars from a Reader to a Writer. : Copy « File Input Output « Java






Copy chars from a Reader to a Writer.

  
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Main {
  /**
   * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
   * <p>
   * This method buffers the input internally, so there is no need to use a
   * <code>BufferedReader</code>.
   * 
   * @param input
   *          the <code>Reader</code> to read from
   * @param output
   *          the <code>Writer</code> to write to
   * @return the number of characters copied
   * @throws NullPointerException
   *           if the input or output is null
   * @throws IOException
   *           if an I/O error occurs
   * @since 1.1
   */
  public static int copy(Reader input, Writer output) throws IOException {
    char[] buffer = new char[1024];
    int count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
      output.write(buffer, 0, n);
      count += n;
    }
    return count;
  }
}

   
    
  








Related examples in the same category

1.Copies file contents from source to destination
2.Copies the contents of the given InputStream to the given OutputStream
3.Copy Pipe
4.Copy any input stream to output file
5.Copy any input stream to output stream
6.Copy file and directory
7.Utility methods for file and stream copying
8.Copies all data from an input stream to an output stream.
9.copy Completely (InputStream input, OutputStream output)
10.copy Completely (Reader input, Writer output)
11.copy Completely(URI input, URI output)
12.Copies the InputStream into the OutputStream, until the end of the stream has been reached.
13.Copies the InputStream into the OutputStream, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
14.Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
15.Copies the contents of the Reader into the Writer, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
16.Copy a file and user buffer
17.Copy a directory and all of its contents.
18.Copy in stream to an out stream
19.Copy the source file system structure into the supplied target location.