file.transferTo
时间: 2023-12-21 21:04:42 浏览: 30
file.transferTo() is a method in Java that transfers the contents of a file to another file or to an OutputStream. It is used to move files between directories or to send files over a network.
Syntax:
```
public void transferTo(long position, long count, OutputStream out) throws IOException
```
Parameters:
- position: Starting position in the file from where data needs to be transferred
- count: Number of bytes to be transferred
- out: OutputStream to which data will be transferred
Example:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class TransferFile {
public static void main(String[] args) {
File source = new File("source.txt");
File destination = new File("destination.txt");
try (FileOutputStream outputStream = new FileOutputStream(destination)) {
source.transferTo(0, source.length(), outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
In this example, the contents of the file "source.txt" are transferred to "destination.txt" using the transferTo() method. The method takes the starting position as 0 and the length of the file as the count. The OutputStream is created using FileOutputStream, which writes the data to the file "destination.txt".