黑马程序员 JAVA 五一论坛练习 —— 4(10技术分)

------- android培训java培训、期待与您交流! ---------- 

实现一个文件加密程序。要求:输入一个文件夹路径或者文件路径作为第一个参数,输入一个数字类型字符串作为密码(比如:123456),然后程序即可为该文件夹内的所有文件加密或者为该文件加密。当再次输入一个文件夹或者文件作为第一个参数,输入密码作为第二个参数的时候,然后程序可以将该文件夹下的所有文件解密或者为该文件解密。 

思路:加密 即让人看不懂原来的文件内容就行
加密的方法 根据一个数异或一个数 两次还是那个数的道理 
通过对制造一个一样的文件 把原来的文件中字节都异或1达到加密的目的
并在文件末尾通过RandomAccessFile流 添加上输入的密码来实现保存密码的问题
如何判断文件是否加过密 在每个文件加密的时候都会在文件名后加.lhy作为加密的依据 我名字的缩写
解码同上面加密的思路一样异或1即可解码 当然要输入对的密码才行 解码前同样利用RandomAccessFile流除去文件中的密码

 
package  ;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
import javax.swing.JFileChooser;
public  class LX4  {
static  String password = null; //
     /**
     *  @param  args
     */
     public  static  void main( String[] args)  throws  Exception {
         // TODO Auto-generated method stub
         boolean b  =  false; //
        JFileChooser jfc  =  new JFileChooser();
        jfc.setDialogTitle( "");
        jfc.setDialogType(JFileChooser.OPEN_DIALOG); //  JFileChooser  "Open"
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 
         int res  = jfc.showOpenDialog( null); //  "Open File" 
         if (res  == jfc.APPROVE_OPTION) { 
            File dir  = jfc.getSelectedFile();
             if(dir.isDirectory()) {jiamidirectory(dir,dir.getAbsolutePath());}
         else ispass(dir.getAbsolutePath());
             //password(dir.getAbsolutePath(),b);
            
        }
    }
     private  static  void password( String file)  throws  Exception  //
    {
         if(password == null)
    { System.out.println( "");
    Scanner reader = new Scanner( System.in);
    password =reader.nextLine();}
    RandomAccessFile raf  =  new RandomAccessFile(file,  "r"); //访 
        StringBuffer sb  =  new  StringBuffer();
        for( int i  = password.length() ; i >= 1 ; i --) //访
       {raf.seek(raf.length() -i);
       sb.append(( char)raf.read());
       }
       raf.close();
        if(password.equals(sb.toString())) jiema(file,password);
        else  System.out.println( "");
    }
    
     private  static  void addpassword( String file, String password)  throws IOException //
     //RandomAccessFile 
    {RandomAccessFile raf  =  new RandomAccessFile(file,  "rw");
    raf.seek(raf.length());
    raf.writeBytes(password);
    raf.close();
     System.out.println(file + " " + "");}
    
     private  static  void minpassword( String file, String password)  throws IOException //
     //RandomAccessFile 
    {RandomAccessFile raf  =  new RandomAccessFile(file,  "rw");
     for( int x =password.length();x > 0;x --)
    {raf.seek(raf.length() -x);
    raf.write( "\0".getBytes());}
    raf.close();}
     private   static  void jiami( String name)  throws  Exception { //
        
         if(password == null) // 
        { String s2 = null;
         System.out.println( "");
        Scanner reader = new Scanner( System.in);
        password =reader.nextLine();
         System.out.println( "");
        Scanner reader2 = new Scanner( System.in);
         if(password ==reader2.nextLine()){ System.out.println( ""); return;} //
         if(password == null){ System.out.println( ""); return;}}
        File f = new File(name);
         BufferedInputStream br  =  null;
            BufferedOutputStream bw  =  null;
                br  =  new BufferedInputStream( new FileInputStream(name));
                bw  =  new BufferedOutputStream( new FileOutputStream(name + ".lhy")); // separator便
             byte[] b  =  new  byte[br.available()]; // 
             //System.out.println(br.available());
             int x ;
            
                 while ((x  = br.read(b))  != -1) {
                     for( int z = 0;z <x;z ++)
                    { //System.out.println(x);
                        b[z] =( byte) (b[z]^ 1);}
                    bw.write(b);
                }
                bw.flush();
                bw.close();
                br.close();
                addpassword(name + ".lhy",password);
                f.delete();
                
    
    }
    
private   static  void jiema( String name, String s)  throws  Exception { // 
         // TODO Auto-generated method stub
    minpassword(name,s); //
    File f = new File(name);
     String name2 = null;
    name2 =name.replaceAll( "\\.lhy", ""); //.lhy
     BufferedInputStream br  =  null;
        BufferedOutputStream bw  =  null;
            br  =  new BufferedInputStream( new FileInputStream(name));
            bw  =  new BufferedOutputStream( new FileOutputStream(name2)); // separator便
    
             byte[] b  =  new  byte[br.available()]; //  
         int x  =  0;
                 while ((x  = br.read(b)) != -1) {
                 for( int z = 0;z <x;z ++)
                {b[z] =( byte) (b[z]^ 1);  //
                 //if(z>br.available()-s.length()) b[z]=(byte)0;F
                }           
                bw.write(b);
            }
            bw.flush();
            br.close();
            bw.close();
            f.delete();
             System.out.println(name2 + " " + "");
    }
private  static  void ispass( String file)  throws  Exception //
    {
   File f = new File(file);
   if(f.getName().matches( "(.)+\\.lhy")) password(file); ////.lhy
   else jiami(file); //
    
    }
public  static  void jiamidirectory(File dir,  String path)  throws  Exception { // 
    File[] f  = dir.listFiles();
     for ( int x  =  0; x  < f.length; x ++) {
         if (f[x].isDirectory()) { //  
            jiamidirectory(f[x], f[x].getAbsolutePath()); // 
        }  else {
            ispass(f[x].getAbsolutePath()); //
        }
    }
}
}
 
这题只得了7分 大概是代码重复性太高 阅读性差的关系吧
五一休息2天就写了这4个代码 深深感觉到自己的不足 只能自己多练练了
活动地址:
http://bbs.itheima.com/forum.php?mod=viewthread&tid=116285


------- android培训java培训、期待与您交流! ----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值