文件与文件夹课后作业

import java.util.ArrayList;
 
public class Size {
   static long size=0;
 private static ArrayList<String> filelist=new ArrayList<String>();
 public static void main(String[] args) {
  Size s=new Size();
  String filePath="D:\软件";
  s.getFiles(filePath);
  
 }
 //通过递归得到某一路径下所有的目录及文件
 void getFiles(String filePath) {
  
  File root=new File(filePath);
  File[] files=root.listFiles();
  for(File file:files) {
   if(file.isDirectory()) {
    getFiles(file.getAbsolutePath());
    filelist.add(file.getAbsolutePath());
    
   }
   else {
    size+=file.getAbsolutePath().length();
   }
  }
  System.out.println("大小是"+size);

  
 }
   
}

这个程序的运行结果为:

大小是431

大小是868

大小是964

大小是1054

大小是1254

大小是1322

大小是2013

大小是2259

大小是2338

大小是2338

大小是3655

package shuruliu;
import java.io.FileInputStream;  

import java.io.FileOutputStream;  

import java.io.InputStream;  

import java.io.OutputStream;  

import java.security.Key;  

import java.security.SecureRandom;  

  

import javax.crypto.Cipher;  

import javax.crypto.CipherInputStream;  

import javax.crypto.CipherOutputStream;  

import javax.crypto.KeyGenerator;  
public class JiaMi {
    Key key;   

      public void TestDES(String str) {   

        getKey(str);//生成密匙   

      }   
      public void getKey(String strKey) {   

            try {   

                KeyGenerator _generator = KeyGenerator.getInstance("DES");   

                _generator.init(new SecureRandom(strKey.getBytes()));   

                this.key = _generator.generateKey();   

                _generator = null;   

            } catch (Exception e) {   

                throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);   

            }   

          }   
      public void encrypt(String file, String destFile) throws Exception {   

            Cipher cipher = Cipher.getInstance("DES");   

            // cipher.init(Cipher.ENCRYPT_MODE, getKey());   

            cipher.init(Cipher.ENCRYPT_MODE, this.key);   

            InputStream is = new FileInputStream(file);   

            OutputStream out = new FileOutputStream(destFile);   

            CipherInputStream cis = new CipherInputStream(is, cipher);   

            byte[] buffer = new byte[1024];   

            int r;   

            while ((r = cis.read(buffer)) > 0) {   

                out.write(buffer, 0, r);   

            }   

            cis.close();   

            is.close();   

            out.close();   

          }   
      public void decrypt(String file, String dest) throws Exception {   

            Cipher cipher = Cipher.getInstance("DES");   

            cipher.init(Cipher.DECRYPT_MODE, this.key);   

            InputStream is = new FileInputStream(file);   

            OutputStream out = new FileOutputStream(dest);   

            CipherOutputStream cos = new CipherOutputStream(out, cipher);   

            byte[] buffer = new byte[1024];   

            int r;   

            while ((r = is.read(buffer)) >= 0) {   

                System.out.println();  

                cos.write(buffer, 0, r);   

            }   

            cos.close();   

            out.close();   

            is.close();   

          }   

          public static void main(String[] args){   

            TestDES td = new TestDES();   

            try {

        td.encrypt("e:/r.txt", "e:/r加密后.txt");

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        } //加密   

            try {

        td.encrypt("e:/r加密后.txt", "e:/r解密后.txt");

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        } //解密   

              

          }   

        }

先建立一个文件,然后在文件里写入这个是要加密的文件,然后加密之后,形成一堆乱码,然后解密之后,文件里的内容又变为了,这个是要加密的文件。

原文地址:https://www.cnblogs.com/zhaoxinhui/p/9980146.html