📄 fileencrypt.java
字号:
import javax.swing.JOptionPane;
import java.io.*;
public class FileEncrypt{
public static void main(String[] args) {
//通过用户输入获取加密码文件、被加密的文件、加密后的文件
String fdCode = InputReader.inputText("保存加密码的文件:");
String fdOrig = InputReader.inputText("被加密的文件:");
String fdResult = InputReader.inputText("加密后的文件:");
try {
//根据加密码进行文件加密
Text aText = new Text(fdCode,fdOrig,fdResult);
aText.encrypt();
String toBePrinted = "被加密的文本内容:\n";
toBePrinted += aText.getOriginalText();
toBePrinted += "\n\n加密后的文本内容:\n";
toBePrinted += aText.getEncryptedText();
JOptionPane.showMessageDialog(null, toBePrinted);
}catch(IOException e) {
System.out.println(e.toString());
}
System.exit(0);
}
}
//文本类
class Text {
final String alphabet = "abcdefghijklmnopqrstuvwxyz";
String code;
String resultFileName;
String originalText = "";
String encryptedText = "";
public Text(String codeFile, String originalFileName,
String startResultFileName) throws IOException{
resultFileName = startResultFileName;
originalText = readText(originalFileName);
code = readText(codeFile);
//加密码长度应等于26
if(code.length() != alphabet.length()) throw new IOException("非法的加密码.");
}
//获取被加密文件的文本
public String getOriginalText() {
return originalText;
}
//获取加密后文件的文本
public String getEncryptedText() {
return encryptedText;
}
public String readText(String filename) throws IOException {
FileReader readCon = new FileReader(filename);
BufferedReader reader = new BufferedReader(readCon);
String text = "";
String line = reader.readLine();
while(line != null) {
text += line + "\n";
line = reader.readLine();
}
//将文本的最后一个字符删除,即文本最后不能以CR结尾
text = text.substring(0,text.length()-1);
reader.close();
return text;
}
//根据给定的加密码进行文件加密
public void encrypt() throws IOException {
StringBuffer result = new StringBuffer();
for(int i = 0; i < originalText.length(); i++) {
boolean big = Character.isUpperCase(
originalText.charAt(i));
int pos = alphabet.indexOf(
Character.toLowerCase(originalText.charAt(i)));
if(pos > -1) {
if(big)
result.append(
Character.toUpperCase(code.charAt(pos)));
else
result.append(code.charAt(pos));
}else
result.append(originalText.charAt(i));
}
encryptedText = result.toString();
//将加密后的结果输出到文件
FileWriter resultConnection = new FileWriter(resultFileName, false);
PrintWriter resultWriter = new PrintWriter(
new BufferedWriter(resultConnection));
resultWriter.print(encryptedText);
resultWriter.close();
}
}
class InputReader {
public static String inputText(String prompt) {
String text = JOptionPane.showInputDialog(prompt);
while (text == null || text.trim().equals("")) {
JOptionPane.showMessageDialog(null, "你未输入了文件名!");
text = JOptionPane.showInputDialog(prompt);
}
return text.trim();
}
public static int inputInteger(String prompt) {
int number = 0;
boolean ok = false;
do {
String theInputText = inputText(prompt);
try {
number = Integer.parseInt(theInputText);
ok = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "无效的整数!\n");
}
} while (!ok);
return number;
}
public static double inputDecimalNumeral(String prompt) {
double number = 0;
boolean ok = false;
do {
String theInputText = inputText(prompt);
try {
number = Double.parseDouble(theInputText);
ok = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "无效的浮点数!\n");
}
} while (!ok);
return number;
}
}
/* Example run:
***Input: File containing the encryption code: code.txt
***Input: File containing the text: original.txt
***Input: File to contain the result:: result.txt
The code file:
abcdefghijklmnopqrUtuvwxyz // simple to test, only one letter is substituted
Encrypt:
Simon Thoresen
Thomas Angells gate 12
7011 Trondheim
The encrypted version of the text:
Uimon ThoreUen
ThomaU AngellU gate 12
7011 Trondheim
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -