将C:\\abc\\1.txt中的文件信息复制到C:\\abc\\2.txt文件中
时间: 2023-07-24 07:13:52 浏览: 53
要将C:\\abc\\1.txt文件中的文件信息复制到C:\\abc\\2.txt文件中,你可以使用Java的文件读取和写入操作。以下是一个示例代码:
```java
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
String sourceFilePath = "C:\\abc\\1.txt";
String destinationFilePath = "C:\\abc\\2.txt";
File sourceFile = new File(sourceFilePath);
File destinationFile = new File(destinationFilePath);
try {
BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(destinationFile));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个示例代码中,我们首先指定源文件路径为`C:\\abc\\1.txt`,以及目标文件路径为`C:\\abc\\2.txt`,你可以根据需要修改这些路径。
然后,我们使用File类创建表示源文件和目标文件的实例。
我们创建一个BufferedReader来读取源文件的内容,并创建一个BufferedWriter来写入目标文件。
我们使用一个循环来逐行读取源文件的内容,并将每一行写入目标文件中。
最后,我们关闭Reader和Writer,并输出复制成功的消息。
希望这个示例能满足你的需求!如有任何疑问,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)