小動(dòng)作 三

168023916108376898.jpg

*** 作事件響應(yīng) ***

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(數(shù)字)",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("文件未找到,請(qǐng)重試");
      }
      catch(IOException e){
        System.out.println("讀取錯(cuò)誤");
      }
      finally {
        try{
          if(in!=null){
            in.close();
          }
          if(out!=null){
            out.close();
          }
        }
        catch(IOException e){
          System.out.println("關(guān)閉失敗");
        }
      }

    }
    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("文件未找到,請(qǐng)重試");
      }
      catch(IOException e){
        System.out.println("讀取錯(cuò)誤");
      }
      finally {
        try{
          if(in!=null){
            in.close();
          }
          if(out!=null){
            out.close();
          }
        }
        catch(IOException e){
          System.out.println("關(guān)閉失敗");
        }
      }
    }
 }
 }


****就這樣吧****

效果圖如下:


Paste_Image.png

*** 清醒小刻 ***
****--------------------沒(méi)錯(cuò),這小玩意兒,大家都會(huì)---------------------------****

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,941評(píng)論 25 709
  • 第一部分、十道海量數(shù)據(jù)處理面試題 1、海量日志數(shù)據(jù),提取出某日訪問(wèn)百度次數(shù)最多的那個(gè)IP。 此題,在我之前的一篇文...
    零一間閱讀 1,018評(píng)論 0 5
  • 大漠之秋 葉子們都要走了穿著懷舊的衣裳在和我做最后的告別 一片落在肩膀上的胡楊葉金黃和血紅的離別之言壓得我骨骼錯(cuò)位...
    竹無(wú)心a閱讀 679評(píng)論 4 15

友情鏈接更多精彩內(nèi)容