1.
import java.io.File;
2. import java.io.FileInputStream;
3. import java.io.FileNotFoundException;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.nio.ByteBuffer;
7. import java.nio.channels.FileChannel;
8.
9.
10. public class ChannelDemo2
11. {
12.
13. public static void main(String [] args)
14. {
15. File fin = new File("F:"+File.separator+"4399.txt");
16. File fout = new File("F:"+File.separator+"5026.txt");
17.
18. FileInputStream fis = null;
19. FileOutputStream fos = null;
20. try {
21. fis = new FileInputStream(fin);
22. fos = new FileOutputStream(fout,false);
23.
24.
25.
26. FileChannel fcis = fis.getChannel(); //擷取 輸入 輸出流的通道
27. FileChannel fcos = fos.getChannel();
28.
29. ByteBuffer sb = ByteBuffer.allocate(1024);
30.
31. int len =0;
32. while((len = fcis.read(sb))!=-1)
33. {
34. sb.flip();
35. fcos.write(sb);
36. sb.clear();
37. }
38.
39. fis.close();
40. fos.close();
41. fcis.close();
42. fcos.close();
43.
44. } catch (FileNotFoundException e) {
45. e.printStackTrace();
46. }catch(IOException e)
47. {
48. e.printStackTrace();
49. }
50. }
51.
52. }