通过给文件中的每个字节加5来对文件编码,编写一个程序,提示用户输入一个输入文件名和一个输出文件名,然后将输入文件的加密版本存入输出文件
时间: 2024-02-29 07:53:05 浏览: 31
好的,以下是 Python 代码实现:
```python
input_file = input("请输入输入文件名:")
output_file = input("请输入输出文件名:")
with open(input_file, 'rb') as f_in:
with open(output_file, 'wb') as f_out:
byte = f_in.read(1) # 读取一个字节
while byte:
byte = (byte[0] + 5) % 256 # 对字节加5
f_out.write(bytes([byte])) # 将加密后的字节写入输出文件
byte = f_in.read(1) # 继续读取下一个字节
```
这个程序首先要求用户输入输入文件名和输出文件名。然后使用 `with` 语句打开输入文件和输出文件,分别用 `'rb'` 和 `'wb'` 模式打开,因为我们需要以二进制模式读取和写入文件。接着,程序使用一个循环逐个读取输入文件的字节,对每个字节加5,再将加密后的字节写入输出文件。最后,程序关闭输入和输出文件。
相关问题
Java通过给文件中的每个字节加5来对文件编码,编写一个程序,提示用户输入一个输入文件名和一个输出文件名,然后将输入文件的加密版本存入输出文件
以下是 Java 代码实现:
```java
import java.io.*;
public class Main {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("请输入输入文件名:");
String inputFileName = reader.readLine();
System.out.print("请输入输出文件名:");
String outputFileName = reader.readLine();
FileInputStream fis = new FileInputStream(inputFileName);
FileOutputStream fos = new FileOutputStream(outputFileName);
int b;
while ((b = fis.read()) != -1) {
b = (b + 5) % 256; // 对字节加5
fos.write(b); // 将加密后的字节写入输出文件
}
fis.close();
fos.close();
System.out.println("加密成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个程序首先使用 `BufferedReader` 读取用户输入的输入文件名和输出文件名。然后使用 `FileInputStream` 和 `FileOutputStream` 分别打开输入文件和输出文件。程序使用一个循环逐个读取输入文件的字节,对每个字节加5,再将加密后的字节写入输出文件。最后,程序关闭输入和输出文件,并输出加密成功的提示信息。需要注意的是,加密后的字节要进行取模运算,确保它们在 0~255 的范围内。
通过给文件中的每个字节加5来对文件编码。编写一个程序,提示用户输入 一个输入文件名和一个输出文件名,然后将输入文件的加密版本存入输出文件。
下面是Java代码实现:
```java
import java.io.*;
public class FileEncoder {
public static void main(String[] args) {
String inputFileName, outputFileName;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter input file name: ");
try {
inputFileName = reader.readLine();
System.out.println("Enter output file name: ");
outputFileName = reader.readLine();
File inputFile = new File(inputFileName);
File outputFile = new File(outputFileName);
if (inputFile.exists()) {
try (InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile)) {
int byteRead;
while ((byteRead = inputStream.read()) != -1) {
byteRead += 5;
outputStream.write(byteRead);
}
System.out.println("File encoded successfully.");
} catch (IOException e) {
System.out.println("Error occurred while encoding the file.");
e.printStackTrace();
}
} else {
System.out.println("Input file does not exist.");
}
} catch (IOException e) {
System.out.println("Error occurred while reading input.");
e.printStackTrace();
}
}
}
```
在这个程序中,我们首先提示用户输入输入文件名和输出文件名,然后尝试打开这两个文件。如果输入文件存在,就读取它的每个字节并将其加5,然后将加密后的字节写入输出文件。如果输入文件不存在,就输出错误信息。如果在编码过程中发生了错误,就输出错误信息并打印异常堆栈跟踪。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)