Пример #1
0
 public static void main(String[] args) throws Exception {
   if (args.length != 2) {
     System.out.println("arguments: sourcefile destfile");
     System.exit(1);
   }
   FileChannel in = new FileInputStream(args[0]).getChannel(),
       out = new FileOutputStream(args[1]).getChannel();
   in.transferTo(0, in.size(), out);
   // Or:
   // out.transferFrom(in, 0, in.size());
 }
Пример #2
0
 /**
  * copies file "in" to file "out"
  *
  * @param in
  * @param out
  * @throws java.io.IOException
  */
 public static boolean copy(File in, File out) {
   Boolean returnVal = true;
   try {
     FileChannel inChannel = new FileInputStream(in).getChannel();
     FileChannel outChannel = new FileOutputStream(out).getChannel();
     try {
       inChannel.transferTo(0, inChannel.size(), outChannel);
     } catch (IOException e) {
       returnVal = false;
     } finally {
       if (inChannel != null) inChannel.close();
       if (outChannel != null) outChannel.close();
     }
   } catch (Exception ea) {
     returnVal = false;
   }
   return returnVal;
 }
Пример #3
0
  public static void copyFileNIO(String filenameIn, String filenameOut, long kbchunks)
      throws IOException {

    FileInputStream in = new FileInputStream(filenameIn);
    FileChannel inChannel = in.getChannel();

    FileOutputStream out = new FileOutputStream(filenameOut);
    FileChannel outChannel = out.getChannel();

    long size = inChannel.size();
    // outChannel.position(size-2);
    // outChannel.write(ByteBuffer.allocate(1));
    // outChannel.position(0);

    if (debug)
      System.out.println(
          "read " + filenameIn + " len = " + size + " out starts at=" + outChannel.position());

    long start = System.currentTimeMillis();
    long done = 0;
    while (done < size) {
      long need = Math.min(kbchunks * 1000, size - done);
      done += inChannel.transferTo(done, need, outChannel);
    }

    outChannel.close();
    inChannel.close();

    double took = .001 * (System.currentTimeMillis() - start);
    if (debug) System.out.println(" write file= " + filenameOut + " len = " + size);

    double rate = size / took / (1000 * 1000);
    System.out.println(
        " copyFileNIO("
            + kbchunks
            + " kb chunk) took = "
            + took
            + " sec; rate = "
            + rate
            + "Mb/sec");
  }
Пример #4
0
  /*
   * Method used to move a file from sourceFile to destFile
   */
  public static void moveFile(File sourceFile, File destFile) throws IOException {
    FileChannel source = null;
    FileChannel destination = null;

    try {
      source = new FileInputStream(sourceFile).getChannel();
      destination = new FileOutputStream(destFile).getChannel();

      long count = 0;
      long size = source.size();
      source.transferTo(count, size, destination);
    } finally {
      if (source != null) {
        source.close();
      }

      if (destination != null) {
        destination.close();
      }
    }
  }
Пример #5
0
 /*
  * Perform a FileChannel.TransferTo on the socket channel.
  */
 long transferTo(FileChannel fc, long pos, long len) throws IOException {
   return fc.transferTo(pos, len, sc);
 }