读取D:\2023training\file02.txt,写入到D:\2023training\file02_copy.txt。
时间: 2023-08-15 07:09:34 浏览: 39
好的,请稍等。
```python
file_path = r'D:\2023training\file02.txt'
copy_file_path = r'D:\2023training\file02_copy.txt'
with open(file_path, 'r') as file:
content = file.read()
with open(copy_file_path, 'w') as copy_file:
copy_file.write(content)
print("文件复制完成。")
```
这段代码会将 `D:\2023training\file02.txt` 中的内容复制到 `D:\2023training\file02_copy.txt` 中。
相关问题
读取D:\2023training\file02.txt,写入到D:\2023training\file02_copy.txt。 并且读写文件发生的异常信息写入到D:\2023training\log\training.log文件中。 在读写完成后,将D:\2023training\file02.txt文件删除。
您可以使用Java的`BufferedReader`和`BufferedWriter`类来读取和写入文本文件,以及`File`类来删除文件。下面是一个示例代码:
```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
public class FileReadWriteExample {
public static void main(String[] args) {
String sourceFilePath = "D:\\2023training\\file02.txt";
String destinationFilePath = "D:\\2023training\\file02_copy.txt";
String logFilePath = "D:\\2023training\\log\\training.log";
try {
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader(sourceFilePath));
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
reader.close();
// 写入文件
BufferedWriter writer = new BufferedWriter(new FileWriter(destinationFilePath));
writer.write(content.toString());
writer.close();
System.out.println("文件读写成功!");
// 记录日志
String logMessage = LocalDateTime.now() + ": 文件读写成功!\n";
Files.write(Path.of(logFilePath), logMessage.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
// 删除源文件
File sourceFile = new File(sourceFilePath);
boolean deleted = sourceFile.delete();
if (deleted) {
System.out.println("源文件删除成功!");
} else {
System.out.println("源文件删除失败!");
}
} catch (IOException e) {
System.out.println("文件读写失败:" + e.getMessage());
// 记录异常信息到日志文件
String logErrorMessage = LocalDateTime.now() + ": 文件读写失败!" + e.getMessage() + "\n";
try {
Files.write(Path.of(logFilePath), logErrorMessage.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException ex) {
System.out.println("日志记录失败:" + ex.getMessage());
}
}
}
}
```
在上面的示例中,我们首先指定源文件路径、目标文件路径和日志文件路径。然后,我们使用`BufferedReader`读取源文件的内容,并使用`BufferedWriter`将内容写入目标文件。接下来,我们使用`Files.write()`方法将成功或失败的日志信息追加到日志文件中。最后,我们使用`File`类的`delete()`方法删除源文件。
请确保源文件`D:\2023training\file02.txt`存在,并且您具有读取和写入权限。完成后,您将在`D:\2023training\log\training.log`中看到相应的日志信息,并且源文件`D:\2023training\file02.txt`将被删除。新创建的副本文件将保存在`D:\2023training\file02_copy.txt`中。
利用Java实现:读取D:\2023training\file03.txt,加密写入D:\2023training\file03_encryption.txt。并且读取加密文件D:\2023training\file03_encryption.txt,解密写入到D:\2023training\file03_decrypt.txt。
您可以使用Java的文件操作和加密解密功能来实现读取文件并进行加密解密的操作。以下是一个示例代码:
```java
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class FileEncryptionExample {
public static void main(String[] args) {
String inputFile = "D:\\2023training\\file03.txt";
String encryptedFile = "D:\\2023training\\file03_encryption.txt";
String decryptedFile = "D:\\2023training\\file03_decrypt.txt";
String key = "myEncryptionKey"; // 加密解密使用的密钥
try {
// 加密文件
encryptFile(inputFile, encryptedFile, key);
// 解密文件
decryptFile(encryptedFile, decryptedFile, key);
System.out.println("文件加密解密完成");
} catch (Exception e) {
e.printStackTrace();
}
}
// 加密文件
public static void encryptFile(String inputFile, String outputFile, String key) throws Exception {
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] block = new byte[8];
int i;
while ((i = fis.read(block)) != -1) {
cos.write(block, 0, i);
}
cos.close();
fis.close();
}
// 解密文件
public static void decryptFile(String inputFile, String outputFile, String key) throws Exception {
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
byte[] block = new byte[8];
int i;
while ((i = cis.read(block)) != -1) {
fos.write(block, 0, i);
}
fos.close();
cis.close();
}
}
```
在上面的代码中,我们定义了`encryptFile()`和`decryptFile()`两个方法来分别加密和解密文件。首先,我们使用`FileInputStream`读取原始文件,然后使用`FileOutputStream`写入加密或解密后的文件。我们使用`SecretKeySpec`来创建一个加密解密使用的密钥,并使用`Cipher`类来进行加密解密操作。在加密和解密过程中,我们使用`CipherOutputStream`和`CipherInputStream`来处理数据流。最后,我们通过指定的文件路径和密钥调用这两个方法来进行文件的加密和解密操作。
请注意,在实际应用中,加密解密使用的密钥应该更为复杂和安全,并且可以通过其他方式进行存储和传输。此示例仅用于演示目的。
相关推荐
![](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)