用java语言实现,文件加密、粉碎类;要求该程序具有图形用户界面,给出完整源码,并且提示部分和注释部分用中文提示
时间: 2023-07-15 12:12:37 浏览: 123
这是一个使用Java语言实现的文件加密和粉碎类程序,具有图形用户界面。以下是完整的源代码和注释:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileEncrypter extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField inputFileTF, outputFileTF;
private JButton encryptBtn, shredBtn, browseInputBtn, browseOutputBtn;
private JFileChooser fileChooser;
private JLabel statusLbl;
public FileEncrypter() {
super("文件加密/粉碎");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 1));
// 创建文件选择器
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 创建输入文件路径文本框和浏览按钮
JPanel inputPanel = new JPanel(new FlowLayout());
inputPanel.add(new JLabel("输入文件路径:"));
inputFileTF = new JTextField(30);
inputPanel.add(inputFileTF);
browseInputBtn = new JButton("浏览");
browseInputBtn.addActionListener(this);
inputPanel.add(browseInputBtn);
add(inputPanel);
// 创建输出文件路径文本框和浏览按钮
JPanel outputPanel = new JPanel(new FlowLayout());
outputPanel.add(new JLabel("输出文件路径:"));
outputFileTF = new JTextField(30);
outputPanel.add(outputFileTF);
browseOutputBtn = new JButton("浏览");
browseOutputBtn.addActionListener(this);
outputPanel.add(browseOutputBtn);
add(outputPanel);
// 创建加密和粉碎按钮
JPanel buttonPanel = new JPanel(new FlowLayout());
encryptBtn = new JButton("加密");
encryptBtn.addActionListener(this);
buttonPanel.add(encryptBtn);
shredBtn = new JButton("粉碎");
shredBtn.addActionListener(this);
buttonPanel.add(shredBtn);
add(buttonPanel);
// 创建状态标签
statusLbl = new JLabel("就绪");
add(statusLbl);
pack();
setLocationRelativeTo(null); // 将窗口居中
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == browseInputBtn) { // 点击输入文件浏览按钮
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
inputFileTF.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (e.getSource() == browseOutputBtn) { // 点击输出文件浏览按钮
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
outputFileTF.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (e.getSource() == encryptBtn) { // 点击加密按钮
String inputPath = inputFileTF.getText();
String outputPath = outputFileTF.getText();
try {
encryptFile(inputPath, outputPath);
statusLbl.setText("加密成功");
} catch (IOException ex) {
ex.printStackTrace();
statusLbl.setText("加密失败");
}
} else if (e.getSource() == shredBtn) { // 点击粉碎按钮
String inputPath = inputFileTF.getText();
try {
shredFile(inputPath);
statusLbl.setText("粉碎成功");
} catch (IOException ex) {
ex.printStackTrace();
statusLbl.setText("粉碎失败");
}
}
}
// 加密文件
private void encryptFile(String inputPath, String outputPath) throws IOException {
FileInputStream in = new FileInputStream(inputPath);
FileOutputStream out = new FileOutputStream(outputPath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
for (int i = 0; i < bytesRead; i++) {
buffer[i] = (byte) ~buffer[i]; // 取反
}
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
}
// 粉碎文件
private void shredFile(String filePath) throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
byte[] buffer = new byte[1024];
long length = file.length();
long n = length / 1024;
int r = (int) (length % 1024);
for (long i = 0; i < n; i++) {
file.seek(i * 1024);
file.write(buffer);
}
if (r > 0) {
file.seek(n * 1024);
file.write(buffer, 0, r);
}
file.setLength(0);
file.close();
}
public static void main(String[] args) {
new FileEncrypter();
}
}
```
阅读全文