文件与文件夹课后作业
生活随笔
收集整理的這篇文章主要介紹了
文件与文件夹课后作业
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
總結
以上是生活随笔為你收集整理的文件与文件夹课后作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces#520 div2
- 下一篇: python生成器、迭代器、__call