-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileChannelTransfer.java
More file actions
33 lines (30 loc) · 902 Bytes
/
Copy pathFileChannelTransfer.java
File metadata and controls
33 lines (30 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
public class FileChannelTransfer{
public static void main(String[] args){
if(args.length!=1){
System.err.println("usage: java FileChannelTransfer file");
return;
}
FileInputStream fis = null;
try{
fis = new FileInputStream(args[0]);
FileChannel fc = fis.getChannel(); // position initially sunsequent input stream reading position
WritableByteChannel outChannel = Channels.newChannel(System.out);
fc.transferTo(0, fc.size(), outChannel); // transfer bytes to another channel without intermediate buffers.
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
}