@Test public void readAndWrite_normal() throws IOException { String sourceFile = this.getClass().getResource("input/").getPath() + "testinput.txt"; String destFile = this.getClass().getResource("output/").getPath() + "output.txt"; new FileChannelExample().readAndWrite(sourceFile, destFile); }
public void readAndWrite(String sourceFile, String destFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { String infile = sourceFile; String outfile = destFile; fin = new FileInputStream(infile); fout = new FileOutputStream(outfile); FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); fcout.write(buffer); } } catch (FileNotFoundException ex1) { throw ex1; } catch (IOException ex2) { throw ex2; } finally { FileUtil.closeInput(fin); FileUtil.closeOutPut(fout); } }
FileChannelExample { public void readAndWrite(String sourceFile, String destFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { String infile = sourceFile; String outfile = destFile; fin = new FileInputStream(infile); fout = new FileOutputStream(outfile); FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); fcout.write(buffer); } } catch (FileNotFoundException ex1) { throw ex1; } catch (IOException ex2) { throw ex2; } finally { FileUtil.closeInput(fin); FileUtil.closeOutPut(fout); } } }
FileChannelExample { public void readAndWrite(String sourceFile, String destFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { String infile = sourceFile; String outfile = destFile; fin = new FileInputStream(infile); fout = new FileOutputStream(outfile); FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); fcout.write(buffer); } } catch (FileNotFoundException ex1) { throw ex1; } catch (IOException ex2) { throw ex2; } finally { FileUtil.closeInput(fin); FileUtil.closeOutPut(fout); } }  }
FileChannelExample { public void readAndWrite(String sourceFile, String destFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { String infile = sourceFile; String outfile = destFile; fin = new FileInputStream(infile); fout = new FileOutputStream(outfile); FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); fcout.write(buffer); } } catch (FileNotFoundException ex1) { throw ex1; } catch (IOException ex2) { throw ex2; } finally { FileUtil.closeInput(fin); FileUtil.closeOutPut(fout); } }  void readAndWrite(String sourceFile, String destFile); MappedByteBuffer readFileToMemory(String sourceFile, int pos, int len); }
FileChannelExample { public void readAndWrite(String sourceFile, String destFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { String infile = sourceFile; String outfile = destFile; fin = new FileInputStream(infile); fout = new FileOutputStream(outfile); FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); fcout.write(buffer); } } catch (FileNotFoundException ex1) { throw ex1; } catch (IOException ex2) { throw ex2; } finally { FileUtil.closeInput(fin); FileUtil.closeOutPut(fout); } }  void readAndWrite(String sourceFile, String destFile); MappedByteBuffer readFileToMemory(String sourceFile, int pos, int len);  }