java文件加密解密,文件对话的方式\拖动的方式载入

Example12_8.java

public class Example12_8 {
	   public static void main(String args[]) {
	      WindowReader win=new WindowReader();
	      win.setTitle("使用文件对话框读写文件"); 
	   }
	}

WindowReader.java

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.List;
public class WindowReader extends JFrame implements ActionListener {
   JFileChooser fileDialog ;//文件对话框对象
   /*组件和窗口*/
   JTextField text;//文本框
   JButton button,//文件对话框触发按钮
   			enButton,//加密触发按钮
   			deButton;//解密触发按钮
   Box baseBox,//
   		boxH1,//水平的box
   		boxH2;//水平的box
   File dir;//文件所在的目录
   String name,//文件的名字
   			houzhui;//文件的后缀
   private static final int numOfEncAndDec = 0x99;//密钥
   private static int dataOfFile = 0; //文件字节内容

   
   WindowReader() {
      init();//建立图形界面
      /*实现文件拖入*/
      new DropTarget(text, DnDConstants.ACTION_COPY_OR_MOVE,
              new DropTargetAdapter()
              {
                 @Override
                 public void drop(DropTargetDropEvent dtde)
                 {
                    try
                    {
                       // 如果拖入的文件格式受支持
                       if (dtde
                             .isDataFlavorSupported(DataFlavor.javaFileListFlavor))
                       {
                          // 接收拖拽来的数据
                          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                          @SuppressWarnings("unchecked")
                          List list = (List) (dtde.getTransferable()
                                .getTransferData(DataFlavor.javaFileListFlavor));
                          text.setText("");
                          for (File file : list)
                          {
                             text.setText(file.getAbsolutePath());
                          }
                          // 指示拖拽操作已完成
                          dtde.dropComplete(true);
                       }
                       else
                       {
                          // 拒绝拖拽来的数据
                          dtde.rejectDrop();
                       }
                    }
                    catch (Exception e)
                    {
                       e.printStackTrace();
                    }
                 }
              });
      
      setSize(300,400);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   
   /*建立图形界面*/
   void init() {
	  setLayout(new FlowLayout());
	  JLabel biaoqian= new JLabel("选择文件:");   //标签
	  text = new JTextField(15);              //文本框
	  button = new JButton("选择");           //按钮
	  enButton=new JButton("加密");
	  deButton=new JButton("解密");
	  boxH1=Box.createHorizontalBox();
  	  boxH1.add(biaoqian);
  	  boxH1.add(Box.createHorizontalStrut(8));
  	  boxH1.add(text);
  	  boxH1.add(Box.createHorizontalStrut(8));
  	  boxH1.add(button);
      
  	  boxH2=Box.createHorizontalBox();
      boxH2.add(enButton);
  	  boxH2.add(Box.createHorizontalStrut(8));
  	  boxH2.add(deButton);
  	
      baseBox=Box.createVerticalBox();
      baseBox.add(boxH1);
      baseBox.add(Box.createVerticalStrut(10));
      baseBox.add(boxH2);
      add(baseBox);      
      text.addActionListener(this);  
      button.addActionListener(this);
      enButton.addActionListener(this);
      deButton.addActionListener(this);
      fileDialog=new JFileChooser();
   }
   
   /*三个Button事件实现,文件选择,文件加密,文件解密*/
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==button) {//判断激发了什么事件
         int state=fileDialog.showSaveDialog(this);//打开目录并返回整型(表示打开了一个文件or没有打开)
         if(state==JFileChooser.APPROVE_OPTION) {
           try{ 
               File f=fileDialog.getCurrentDirectory();//所选文件的目录  
               String str1=fileDialog.getSelectedFile().getName();//所选文件的名字
               text.setText(f.getAbsolutePath()+"\\"+str1);
           }
           catch(Exception exp){}
         }      
       }
      /*else if(e.getSource()==text){
    	  mainstr=e.getActionCommand();
    	  /*int local=str.lastIndexOf("\\");
    	  String str1=str.substring(local);
    	  String g[]=str1.split("[.]");
    	  name=g[0];
    	  houzhui=g[1];
    	  this.dir=new File(str.replace(str1,""));
      }*/
      else if(e.getSource()==enButton) {
    	  try {
    		split(text.getText());
    	  	InputStream fis  = new FileInputStream(new File(dir,name+"."+houzhui));
    	  	OutputStream fos = new FileOutputStream(new File(dir,name+"的加密文件."+houzhui));
    
    	  	while ((dataOfFile = fis.read()) > -1) {
    	  		fos.write(dataOfFile^numOfEncAndDec);
    	  	}
    
    	  	fis.close();
    	  	fos.flush();
    	  	fos.close();
      	 }
    	 catch(Exception exp){}
      }
      else if(e.getSource()==deButton) {
    	  try {
    		split(text.getText());
    	  	InputStream fis  = new FileInputStream(new File(dir,name+"."+houzhui));
    	  	OutputStream fos = new FileOutputStream(new File(dir,name+"的解密文件."+houzhui));
    
    	  	while ((dataOfFile = fis.read()) > -1) {
    	  		fos.write(dataOfFile^numOfEncAndDec);
    	  	}
    
    	  	fis.close();
    	  	fos.flush();
    	  	fos.close();
      	 }
    	 catch(Exception exp){}
      }
   }
   void split(String str) {
	  int local=str.lastIndexOf("\\");
 	  String str1=str.substring(local);
 	  String g[]=str1.split("[.]");
 	  this.name=g[0];
 	  this.houzhui=g[1];
 	  this.dir=new File(str.replace(str1,""));
   }
   
}

 

你可能感兴趣的:(java)