问题:在JTextArea中附加加密文本
目标:尝试使用JFileCHooser调用获取所选文件'解密'方法然后将输出恢复回原始方法(decryptArea)所有代码都在下面。
问题:为什么不能胜任这项工作?我不明白,因为我们正在使用File&s;如果这不能通过并解密文件并附加它。我有一个正常的方法完成同样的事情,除了当然没有解密(正常保存文件和打开文件方法)只是试图把各个部分放在一起,不幸的是不知道我在做什么错误,然后在没有必要时最终重新设计
userFieled = JTextArea(); ***********
private void decryptArea() throws Exception {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int dialog = fc.showOpenDialog(null);
if (dialog == JFileChooser.APPROVE_OPTION) {
inputDec = fc.getSelectedFile();
inputDec.getName();
inputDec.getAbsoluteFile();
inputDec = fc.getSelectedFile();
userField.append("");
System.out.println(" File: " + inputDec);
File fd = new File (inputDec.getAbsoluteFile().getAbsolutePath());
FileReader reader = new FileReader(fd);
BufferedReader br = new BufferedReader(reader);
userField.read(br,null);
br.close();
userField.requestFocus();
// userField.append(inputDec);
userField.requestFocus();
System.out.println("Finished appending decrypted text:");
}
}
});
//解密进行的地方
public void decrypt(File inputFile, File outputFile, byte[] key) throws Exception {
Cipher cipher = getCipherDecrypt(key);
FileOutputStream fos = null;
CipherInputStream cis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(inputFile);
cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(outputFile);
byte[] data = new byte[1024];
int read = cis.read(data);
while (read != -1) {
fos.write(data, 0, read);
read = cis.read(data);
System.out.println(new String(data, "UTF-8").trim());
}
} finally {
cis.close();
fos.close();
fis.close();
}
}