1. 程式人生 > >05-檔案與流-課後作業

05-檔案與流-課後作業

1.編寫一個程式,指定一個檔案壓,能自動算出其總容量。

package wenjian;

import java.io.File;
import java.util.ArrayList;

public class ceshi1 {
    long size = 0;
    static ArrayList<String> filelist = new ArrayList<String>();
    
    void getFiles(String filelujing) {
        File root = new File(filelujing);
        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); } public static void main(String[] args) { ceshi1 c1 = new ceshi1(); String filelujing = "D:\\谷歌"; c1.getFiles(filelujing); } }

上述程式碼的執行結果為:

2.編寫一個檔案加解密程式,通過命令列完成加解密工作。

package wenjian;

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 ceshi2 { Key key; public ceshi2(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: " + e); } } public void encrypt(String file, String destFile) throws Exception { Cipher cipher = Cipher.getInstance("DES"); 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){ ceshi2 td = new ceshi2("aaa"); try { td.encrypt("D:/text.txt", "D:/檔案text加密後.txt"); } catch (Exception e) { e.printStackTrace(); } try { td.decrypt("D:/檔案text加密後.txt", "D:/檔案text解密後.txt"); } catch (Exception e) { e.printStackTrace(); } } }

執行結果為:

3.編寫一個檔案分割工具,能把一個大檔案分割成多個小檔案,並且能再次把他們合併起來得到完整檔案。

 

package wenjian;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ceshi3 {
    private static void fenge(String src, String endsrc, int num) {
        FileInputStream fis = null;
        File file = null;
        try {
            fis = new FileInputStream(src);
            file = new File(src);
            //建立規定大小的byte陣列
            byte[] b = new byte[num];
            int len = 0;
            //name為以後的小檔案命名做準備
            int name = 1;
            //遍歷將大檔案讀入byte陣列中,當byte陣列讀滿後寫入對應的小檔案中
            while ((len = fis.read(b)) != -1) {
                //分別找到原大檔案的檔名和檔案型別,為下面的小檔案命名做準備
                String name2 = file.getName();
                int lastIndexOf = name2.lastIndexOf(".");
                String substring = name2.substring(0, lastIndexOf);
                String substring2 = name2.substring(lastIndexOf, name2.length());
                FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
                //將byte陣列寫入對應的小檔案中
                fos.write(b, 0, len);
                //結束資源
                fos.close();
                name++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    //結束資源
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void hebing(String src, String endsrc){
        FileOutputStream fos = null;
        File file1 = null;
        File file2 = null;
        try {
            file1 = new File(endsrc);
            file2 = new File(src);
            //獲得大檔案的儲存路徑的FileOutputStream物件
            fos = new FileOutputStream(endsrc);
            //迴圈遍歷對應資料夾中的所有小檔案
            for(File file : file2.listFiles()){

                FileInputStream fis = new FileInputStream(file.getAbsolutePath());

                byte[] b = new byte[1024*1024];
                int len = 0;
                //將小檔案讀入byte陣列,之後再將byte陣列寫入大檔案中
                while((len = fis.read(b)) != -1){
                    fos.write(b, 0, len);
                }
                //結束資源
                fis.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fos != null){
                    //結束資源
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
        public static void main(String[] args){
            fenge("C:\\Users\\Desktop\\wenjianWork.txt", "C:\\Users\\何祺琪\\Desktop",2);
            //呼叫togetherFile()函式合併小檔案到大檔案   引數列表分別為 (小檔案所在的父資料夾路徑,所合成的大檔案的路徑)
            hebing("C:\\Users\\Desktop","C:\\Users\\Desktop\\wenjianWork.txt");
        }
}