FileChannel
oftransferFrom()
Method is an important method in Java NIO for efficient transfer of data between channels, and is often used in scenarios such as file copying.
The following is a detailed introduction to its usage.
Method signature
FileChannel
oftransferFrom()
There are two overloaded forms of the method:
public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;
Parameter description
-
src
: This is oneReadableByteChannel
A parameter of type, indicating the source channel, from which data will be read.FileChannel
ImplementedReadableByteChannel
interface, so you can pass in directlyFileChannel
As the source channel, the object can also be passed into other implementations.ReadableByteChannel
The channel object of the interface. -
position
: This is onelong
Parameter of type, specify the target channel (calltransferFrom()
MethodFileChannel
Object) where the data starts to be written. This position is the offset relative to the starting position of the target channel, counting from 0. -
count
: This is also onelong
A parameter of type that specifies the maximum number of bytes to be transferred from the source channel to the destination channel.
Return value
This method returns the actual number of bytes transferred.
In some cases, the actual number of bytes transmitted may be less thancount
Specified value, such as when there is insufficient available data in the source channel.
Specific usage examples
Here is a usetransferFrom()
Sample code for method for file copying:
import ; import ; import ; import ; public class FileCopyWithTransferFrom { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream(""); FileOutputStream fos = new FileOutputStream(""); FileChannel sourceChannel = (); FileChannel targetChannel = ()) { long size = (); // Start from the start position of the source channel and transfer all data to the start position of the target channel long transferred = (sourceChannel, 0, size); ("Copyed successfully" + transferred + " byte"); } catch (IOException e) { (); } } }
Code explanation
1. Create input and output streams and channels:
- use
FileInputStream
Read the source file,FileOutputStream
Write to the target file. - pass
getChannel()
Methods to obtain source and target files respectivelyFileChannel
Object.
2. Get the source file size:
Call()
Method gets the size of the source file to determine the number of bytes to be transferred.
3. CalltransferFrom()
method:
Transfer the source channelsourceChannel
The data in the target channel is transferred to the target channeltargetChannel
, from the starting position of the target channel (position
0) Start writing, the maximum number of bytes transferred is the size of the source filesize
。
4. Process the return value:
WilltransferFrom()
The actual number of transmitted bytes returned by the method is stored intransferred
variable and print the output.
5. Exception handling:
usetry-with-resources
Statements ensure that the resource is automatically closed, while capturing and processing possible occurrencesIOException
abnormal.
Things to note
-
Performance Advantages:
transferFrom()
The method uses the operating system's zero copy mechanism at the bottom, avoiding multiple copies of data between user space and kernel space, so it has high performance in scenarios such as file copying. -
Insufficient data: If there is insufficient available data in the source channel
count
The specified number of bytes,transferFrom()
The method transfers as much data as possible and returns the actual number of bytes transferred. -
Cross-platform issues:Although
transferFrom()
Methods work properly on most operating systems, but some operating systems may have limits on the maximum number of bytes transferred, so you need to pay attention to when handling large files.
Friendly reminder: You need to""
and""
Replace with the actual file path.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.