我现在正在尝试使用 Java 进行简单的文件加密。没有什么严肃和硬核的,只是非常基本的。现在我需要通过向文件中的每个字节添加 5 来对文件进行编码。程序提示用户输入输入文件名和输出文件名,并将输入文件的加密版本保存到输出文件中。
这是我的代码
import java.util.Scanner;
import java.io.*;
public class EncryptFiles {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("Enter a file to encrypt: ");
FileInputStream in = new FileInputStream(input.next());
// BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(input.next())));
System.out.print("Enter the output file: ");
FileOutputStream output = new FileOutputStream(input.next());
// BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(input.next())));
int value;
while ((value = in.read()) != -1) {
output.write(value + 5);
}
input.close();
output.close();
}
}
我已经执行了常规 FileInputStream 以及 BufferedInputStream,它们都给了我相同的错误。
> Exception in thread "main" java.io.FileNotFoundException: Me.txt (The
> system cannot find the file specified) at
> java.io.FileInputStream.open(Native Method) at
> java.io.FileInputStream.<init>(FileInputStream.java:146) at
> java.io.FileInputStream.<init>(FileInputStream.java:101) at
> EncryptFiles.main(EncryptFiles.java:10) Java Result: 1
我将文件 Me.txt 放在 C: 目录中,我已放入文档,我也尝试将其放在 Java src 文件夹中。我尝试输入确切的文件路径,但没有任何效果。我已经这样做了 C:/Users/Richard/Documents/Me.txt
和 C:\Users\Richard\Documents\Me.txt
但没有无论我如何尝试,我都会遇到同样的错误。
感谢您今后的帮助:)
最佳答案
尝试将 FileInputStream
和 FileOutputStream
实例化的方式更改为
FileInputStream in = new FileInputStream(new File(input.nextLine()));
FileOutputStream in = new FileOutputStream(new File(input.nextLine()));
并使用完整路径作为输入,例如C:/Users/Richard/Documents/Me.txt
关于Java文件加密,无法让它接受文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184767/