java使用I/O流完成文件复制
时间: 2023-09-06 12:07:32 浏览: 92
### 回答1:
在 Java 中使用 I/O 流复制文件可以使用如下的代码:
```
import java.io.*;
public class CopyFile {
public static void main(String[] args) {
// 要复制的文件的路径
String srcFile = "/path/to/src/file.txt";
// 复制后的文件的路径
String destFile = "/path/to/dest/file.txt";
try (InputStream in = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile)) {
// 缓冲区大小
byte[] buffer = new byte[1024];
int length;
// 循环读取文件内容并写入到新文件中
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上面的代码使用了 try-with-resources 语句,这样在结束后会自动关闭流。如果不使用 try-with-resources 语句,需要在 finally 块中手动关闭流。
需要注意的是,上面的代码只能复制纯文本文件。如果要复制二进制文件,需要使用字节流(InputStream 和 OutputStream)而不是字符流(Reader 和 Writer)。
### 回答2:
文件复制通过使用Java中的I/O流可以轻松地实现。下面是一个简单的示例,演示了如何使用I/O流来实现文件复制。
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
String sourceFilePath = "source.txt"; // 源文件路径
String targetFilePath = "target.txt"; // 目标文件路径
try {
// 创建输入流和输出流
FileInputStream input = new FileInputStream(sourceFilePath);
FileOutputStream output = new FileOutputStream(targetFilePath);
// 创建一个缓冲区,用于存储读取/写入的数据
byte[] buffer = new byte[1024];
int bytesRead;
// 读取源文件中的数据,并将其写入目标文件中
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
// 关闭流
input.close();
output.close();
System.out.println("文件复制完成!");
} catch (IOException e) {
System.out.println("发生错误:" + e.getMessage());
}
}
}
```
上述代码中,我们首先指定源文件和目标文件的路径。然后,我们使用`FileInputStream`和`FileOutputStream`类创建输入流和输出流。我们还创建了一个缓冲区,用于存储读取和写入的数据。
在`while`循环中,我们使用`read()`方法从输入流读取数据,并使用`write()`方法将读取的数据写入输出流。我们还使用`bytesRead`变量来记录实际读取的字节数,以便正确写入到输出流中。
最后,我们在`IOException`中捕获任何可能发生的错误,并将错误消息输出到控制台。如果没有错误发生,则输出"文件复制完成!"的消息。
通过运行上述代码,我们可以将一个文件的内容复制到另一个文件中。请确保源文件存在,并且目标文件不存在,以避免发生意外的覆盖。
### 回答3:
Java使用I/O流完成文件复制非常简单。我们可以使用字节流或字符流来实现文件复制。
使用字节流实现文件复制时,首先需要创建一个文件输入流和一个文件输出流。然后,通过循环从文件输入流中读取字节,并将其写入文件输出流中,实现文件复制。最后记得关闭输入流和输出流。
代码示例:
```java
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
// 设置输入文件和输出文件的路径
String inputFilePath = "input.txt";
String outputFilePath = "output.txt";
try {
FileInputStream inputStream = new FileInputStream(inputFilePath);
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
// 定义一个字节数组来存储读取的字节数据
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 关闭输入流和输出流
inputStream.close();
outputStream.close();
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
使用字符流实现文件复制时,操作步骤与字节流类似。只是在创建流时需要使用Reader和Writer类,同时要保证源文件和目标文件的编码类型一致。
代码示例:
```java
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
// 设置输入文件和输出文件的路径
String inputFilePath = "input.txt";
String outputFilePath = "output.txt";
try {
FileReader reader = new FileReader(inputFilePath);
FileWriter writer = new FileWriter(outputFilePath);
// 定义一个字符数组来存储读取的字符数据
char[] buffer = new char[1024];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, charsRead);
}
// 关闭输入流和输出流
reader.close();
writer.close();
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
无论使用字节流还是字符流,Java中的I/O流都提供了方便的方法来实现文件复制。通过以上的代码示例,我们可以轻松地完成文件复制操作。