*** 作事件响应 ***
package encrypt;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
public class Hello{
Frame f =new Frame("小程序");
FileDialog d1=new FileDialog(f,"选择文件",FileDialog.LOAD);
FileDialog d2=new FileDialog(f,"保存文件",FileDialog.SAVE);
Button b1=new Button("加密");
Button b2=new Button("解密");
Label pw=new Label("password(数字)",Label.RIGHT);
TextField tpw=new TextField(20);
static int Password;
static final String Suffix=".enc";
public void inti(){
f.addWindowListener(new mylistener());
b1.addActionListener(new fisterlistener());
b2.addActionListener(new secondlistener());
tpw.addKeyListener(new thirdlistener());
f.setLayout(new GridLayout(2,2));
f.add(b1);
f.add(b2);
f.add(pw);
f.add(tpw);
f.pack();
f.setVisible(true);
}
class mylistener implements WindowListener{
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}
class fisterlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
d1.setVisible(true);
//文件路径与文件名
System.out.println(d1.getDirectory()+d1.getFile());
new Encrypt().doEncrypt(d1.getDirectory()+d1.getFile());
}
}
class secondlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
d2.setVisible(true);
System.out.println(d2.getDirectory()+d2.getFile());
new Encrypt().doDecrypt(d2.getDirectory()+d2.getFile());
}
}
class thirdlistener implements KeyListener{
public void keyTyped(KeyEvent e){
String str="";
str+=e.getKeyChar();//获取字符,一次添加到str
Password=Integer.parseInt(str);
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
public static void main(String[] args){
new Hello().inti();
}
class Encrypt{
public void doEncrypt(String path){
FileInputStream in=null;
FileOutputStream out=null;
try{
int temp=0;
File inFile=new File(path);
File outFile=new File(path+Suffix);
in =new FileInputStream(inFile);
out =new FileOutputStream(outFile);
while((temp=in.read())!=-1){
out.write(temp^Password);
}
}
catch(FileNotFoundException e){
System.out.println("文件未找到,请重试");
}
catch(IOException e){
System.out.println("读取错误");
}
finally {
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
catch(IOException e){
System.out.println("关闭失败");
}
}
}
public void doDecrypt(String path){
int dex=path.lastIndexOf(Suffix);
FileInputStream in=null;
FileOutputStream out=null;
try{
int temp=0;
String name=path.substring(0,dex);
File inFile=new File(path);
File outFile=new File(name);
in =new FileInputStream(inFile);
out =new FileOutputStream(outFile);
while((temp=in.read())!=-1){
out.write(temp^Password);
}
}
catch(FileNotFoundException e){
System.out.println("文件未找到,请重试");
}
catch(IOException e){
System.out.println("读取错误");
}
finally {
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
catch(IOException e){
System.out.println("关闭失败");
}
}
}
}
}
****就这样吧****
效果图如下:
Paste_Image.png
*** 清醒小刻 ***
****--------------------没错,这小玩意儿,大家都会---------------------------****
网友评论