import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class SetEncrypt extends JFrame implements ActionListener{
JOptionPane jOptionPane1=new JOptionPane();
JLabel label1=new JLabel("要加密的文件:");
JTextField text1=new JTextField(16);
JButton button3=new JButton("浏览");
JButton button1=new JButton("确定");
JButton button2=new JButton("退出");
FlowLayout layout1=new FlowLayout();
SetEncrypt(){
super("加密/解密程序--邱俊");
setSize(300,200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(layout1);
//Font ft=new Font("仿宋_GB2312",Font.ITALIC,15);
//text1.setFont(ft);
this.getContentPane().add(label1);
this.getContentPane().add(text1);
this.getContentPane().add(button3);
this.getContentPane().add(button1);
this.getContentPane().add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
String b=text1.getText();
b.trim();
if(b==null){
jOptionPane1.showMessageDialog(null,"文件地址没有填写!","警告",JOptionPane.ERROR_MESSAGE);
}
else{
TransForm tf=new TransForm();
tf.sTrans(b);
}
}
else if(e.getSource()==button2){
System.exit(0);
}
else if(e.getSource()==button3){
JFileChooser jFileChooser1=new JFileChooser();
jFileChooser1.setMultiSelectionEnabled(false);
int result=jFileChooser1.showOpenDialog(null);
if(result==JFileChooser.APPROVE_OPTION){ //打开文件了
File selectedFile=jFileChooser1.getSelectedFile();
String pathFile=selectedFile.getPath();
pathFile.trim();
text1.setText(pathFile);
}
//else if(result==JFileChooser.CANCEL_OPTION){
//System.out.println("单击了打开文件对话框的取消键.");
}
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){};
SetEncrypt encrypt=new SetEncrypt();
}
}
//转换类
class TransForm{
void sTrans(String a){
String pFile=a;
String fileChange=pFile;
fileChange=fileChange.replace('\\','/');
File f_1=new File(fileChange);
//提取路径中的文件名,并加上ans-的前缀后在当前目录显示出加密后的文件名
int i=pFile.lastIndexOf('\\');
String fileSave=pFile.substring(i);
String upname="ans-";
String fileSave2=upname.concat(fileSave);
File f_2=new File(".",fileSave2);
if(!f_1.isFile())
System.out.println("file is not found!");
else{
try{
FileInputStream fins=new FileInputStream(f_1);
FileOutputStream fouts=new FileOutputStream(f_2);
int in_data=0;
while(in_data!=-1){
in_data=fins.read();
int hight=in_data/16;
int lower=in_data%16;
int out_data=lower*16+hight;
fouts.write(out_data);
}
fins.close();
fouts.close();
System.out.println("加密/解密完成");
}
catch(IOException e){
System.out.println("文件IO出错.");
}
}
}
}
评论0
最新资源