JAVA程序设计“txt文件保密程序”.doc_第1页
JAVA程序设计“txt文件保密程序”.doc_第2页
JAVA程序设计“txt文件保密程序”.doc_第3页
JAVA程序设计“txt文件保密程序”.doc_第4页
JAVA程序设计“txt文件保密程序”.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA程序设计“txt文件保密程序”一、程序代码package Protecter;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.filechooser.FileFilter;import java.io.File;import java.util.*;class ProtecterJFrame extends JFrame implements ActionListener private static final long serialVersionUID = 1L;public ProtecterJFrame(String tName)super(tName);private JMenuItem jmi,jmiExit,jmiAbout;private File getPath;private JButton b1,b2,b3,jb1,jb2;private JTable jTable;private VectorVector rowData=new VectorVector();private Vector columnNames=new Vector();public void setWindow()Container c=getContentPane();JMenuBar jmb=new JMenuBar();JMenu jm,jmHelp;jm=new JMenu(文件);jmHelp=new JMenu(帮助);jmi=new JMenuItem(打开);jmi.setActionCommand(添加);jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);jmiExit= new JMenuItem(退出);jmiExit.setActionCommand(退出);jmiAbout=new JMenuItem(关于);jmiAbout.setActionCommand(关于);jm.add(jmi);jm.addSeparator();jm.add(jmiExit);jmHelp.add(jmiAbout);jmb.add(jm);jmb.add(jmHelp);JPanel jp=new JPanel(new BorderLayout();JPanel jp1=new JPanel(new FlowLayout();b1=new JButton(添加文件);b1.setActionCommand(添加);b2=new JButton(撤消文件);b2.setActionCommand(撤消);b3=new JButton(显示文件);b3.setActionCommand(显示);jp1.add(b1);jp1.add(b2);jp1.add(b3);String column=编号,地址,大小(字节);for(int i=0;icolumn.length;i+)columnNames.add(columni);jTable=new JTable(rowData,columnNames)private static final long serialVersionUID =1L;public boolean isCellEditable(int row, int column) return false;jTable.setRowHeight(25);JPanel jp2=new JPanel(new GridLayout();jp2.add(new JScrollPane(jTable);JPanel jp3=new JPanel(new FlowLayout();jb1=new JButton(加密);jb1.setActionCommand(加密);jb2=new JButton(解密);jb2.setActionCommand(解密);jp3.add(jb1);jp3.add(jb2);jp.add(jp1,BorderLayout.NORTH);jp.add(jp2,BorderLayout.CENTER);jp.add(jp3,BorderLayout.SOUTH);c.add(jp);this.setJMenuBar(jmb);this.setBounds(400,200,500,300);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void listenActions()b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);jb1.addActionListener(this);jb2.addActionListener(this);jmi.addActionListener(this);jmiExit.addActionListener(this);jmiAbout.addActionListener(this);public boolean getFileAddress()JFileChooser fileCh=new JFileChooser();fileCh.setCurrentDirectory(new File(.);fileCh.setMultiSelectionEnabled(true);fileCh.setFileFilter(new FileFilter()public boolean accept(File f)return f.getName().toLowerCase().endsWith(.txt)|f.isDirectory();public String getDescription()return 文本文档 *.txt*;);if(fileCh.showOpenDialog(null)=JFileChooser.CANCEL_OPTION)return false;getPath=fileCh.getSelectedFiles();return true;public void addRow(File filePath)Vector row=new Vector(3);int last=jTable.getRowCount()-1;int no=last0?1:(Integer.parseInt(+rowData.get(last).get(0)+1);row.add(+no);row.add(+filePath);row.add(+filePath.length();rowData.add(row);public void transposition(int a)int temp;for(int i=0;ia.length/2;i+)temp=ai;ai=aa.length-1-i;aa.length-1-i=temp;public void removeRows()int x=new intjTable.getSelectedRowCount();x=jTable.getSelectedRows();transposition(x);for(int i=0;ix.length;i+)rowData.remove(xi);if(!rowData.isEmpty()jTable.setRowSelectionInterval(0,0);public void showFiles()int a=new intjTable.getSelectedRowCount();a=jTable.getSelectedRows();int x=500,y=300;for(int j=0;ja.length;j+)File filePath=new File(+rowData.get(aj).get(1);String fileWords = ;tryBufferedReader brf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath); String str=brf.readLine();while(str!=null)fileWords=fileWords+str+rn;str=brf.readLine();catch(IOException e)e.printStackTrace();JFrame text=new JFrame(filePath.getName();JPanel textJp=new JPanel(new GridLayout();JTextArea textArea=new JTextArea();textArea.setEditable(false);textArea.setText(fileWords);textJp.add(new JScrollPane(textArea);text.add(textJp);text.setBounds(x,y,500,300);text.setVisible(true);text.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);x+=20;y+=20;private static final String mark=/*PrinceChen*/;public void encryption()for(int j=0;jjTable.getRowCount();j+)File filePath=new File(+rowData.get(j).get(1);tryBufferedReader brf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath); String strLine=brf.readLine();String fileWords=;String lastLine=;while(strLine!=null)fileWords=fileWords+strLine+rn;lastLine=strLine;strLine=brf.readLine();if(lastLine.startsWith(mark)continue;StringBuffer file=new StringBuffer(fileWords);for(int i=0;ifile.length();i+)char ch=fileWords.charAt(i);if(ch!=r)ch+=11; file.setCharAt(i,ch);elsei+=1;fileWords=new String(file);fileWords+=mark;filePath.setWritable(true);FileOutputStream fileO=new FileOutputStream(filePath);ObjectOutputStream os=new ObjectOutputStream(fileO);os.writeObject(fileWords);os.close();filePath.setReadOnly();catch(IOException e)e.printStackTrace();public void deciphering()for(int j=0;jjTable.getRowCount();j+)File filePath=new File(+rowData.get(j).get(1);tryBufferedReader brf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath); String strLine=brf.readLine();String lastLine=;while(strLine!=null)lastLine=strLine;strLine=brf.readLine();if(!lastLine.startsWith(mark)continue;FileInputStream fileI=new FileInputStream(filePath);ObjectInputStream IS=new ObjectInputStream(fileI);String strFile=null;trystrFile=(String)IS.readObject();IS.close();catch(ClassNotFoundException e)e.printStackTrace();StringBuffer fileWords=new StringBuffer(strFile);int first=fileWords.indexOf(mark);fileWords.delete(first,first+mark.length();for(int i=0;ifileWords.length();i+)char ch=fileWords.charAt(i);if(ch!=r)ch-=11; fileWords.setCharAt(i,ch);elsei+=1;filePath.setWritable(true);FileOutputStream f=new FileOutputStream(filePath);PrintWriter pw=new PrintWriter(f,true);pw.print(fileWords);pw.close();catch(IOException e)e.printStackTrace();public void actionPerformed(ActionEvent e)String strAction=e.getActionCommand();if(strAction=添加)if(getFileAddress()for(int i=0;igetPath.length;i+)String path;int j;for(j=0;j=jTable.getRowCount()addRow(getPathi);jTable.updateUI();else if(strAction=撤消&!rowData.isEmpty()if(jTable.getSelectedRowCount()=0)JOptionPane.showMessageDialog(null,请选中要撤消的行!,提示,JOptionPane.INFORMATION_MESSAGE);elseint select=JOptionPane.showConfirmDialog(null,确定撤消选中行吗?,撤消,JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);if(select=JOptionPane.OK_OPTION)removeRows();jTable.updateUI();else if(strAction=显示&!rowData.isEmpty()if(jTable.getSelectedRowCount()=0)JOptionPane.showMessageDialog(null,请选中要显示的行!,提示,JOptionPane.INFORMATION_MESSAGE);elseshowFiles();else if(strAction=加密)if(jTable.getRowCount()=0)JOp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论