• 文件与流 课后作业


     一、编写一个程序,指定一个文件夹,能自动计算出其总容量
    1
    package 计算文件夹容量; 2 import java.io.File; 3 import java.util.ArrayList; 4 5 public class Size { 6 static long size=0; 7 private static ArrayList<String> filelist=new ArrayList<String>(); 8 public static void main(String[] args) { 9 Size s=new Size(); 10 String filePath="F:\JAVA项目代码"; 11 s.getFiles(filePath); 12 13 } 14 //通过递归得到某一路径下所有的目录及文件 15 void getFiles(String filePath) { 16 17 File root=new File(filePath); 18 File[] files=root.listFiles(); 19 for(File file:files) { 20 if(file.isDirectory()) { 21 getFiles(file.getAbsolutePath()); 22 filelist.add(file.getAbsolutePath()); 23 24 } 25 else { 26 size+=file.getAbsolutePath().length(); 27 } 28 } 29 System.out.println("大小是"+size); 30 31 } 32 } 33

    运行截图:

    二、编写一个文件加解密程序,通过命令行完成加解密工作
    1
    package 文件解密和加密操作; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.security.Key; 8 import java.security.SecureRandom; 9 10 import javax.crypto.Cipher; 11 import javax.crypto.CipherInputStream; 12 import javax.crypto.CipherOutputStream; 13 import javax.crypto.KeyGenerator; 14 15 public class FileCode { 16 Key key; 17 public FileCode(String str) { 18 getKey(str);//生成密匙 19 } 20 /** 21 * 根据参数生成KEY 22 */ 23 public void getKey(String strKey) { 24 try { 25 KeyGenerator _generator = KeyGenerator.getInstance("DES"); 26 _generator.init(new SecureRandom(strKey.getBytes())); 27 this.key = _generator.generateKey(); 28 _generator = null; 29 } catch (Exception e) { 30 throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); 31 } 32 } 33 34 /** 35 * 文件file进行加密并保存目标文件destFile中 36 * 37 * @param file 要加密的文件 如c:/test/srcFile.txt 38 * @param destFile 加密后存放的文件名 如c:/加密后文件.txt 39 */ 40 public void encrypt(String file, String destFile) throws Exception { 41 Cipher cipher = Cipher.getInstance("DES"); 42 // cipher.init(Cipher.ENCRYPT_MODE, getKey()); 43 cipher.init(Cipher.ENCRYPT_MODE, this.key); 44 InputStream is = new FileInputStream(file); 45 OutputStream out = new FileOutputStream(destFile); 46 CipherInputStream cis = new CipherInputStream(is, cipher); 47 byte[] buffer = new byte[1024]; 48 int r; 49 while ((r = cis.read(buffer)) > 0) { 50 out.write(buffer, 0, r); 51 } 52 cis.close(); 53 is.close(); 54 out.close(); 55 } 56 /** 57 * 文件采用DES算法解密文件 58 * 59 * @param file 已加密的文件 如c:/加密后文件.txt 60 * * @param destFile 61 * 解密后存放的文件名 如c:/ test/解密后文件.txt 62 */ 63 public void decrypt(String file, String dest) throws Exception { 64 Cipher cipher = Cipher.getInstance("DES"); 65 cipher.init(Cipher.DECRYPT_MODE, this.key); 66 InputStream is = new FileInputStream(file); 67 OutputStream out = new FileOutputStream(dest); 68 CipherOutputStream cos = new CipherOutputStream(out, cipher); 69 byte[] buffer = new byte[1024]; 70 int r; 71 while ((r = is.read(buffer)) >= 0) { 72 System.out.println(); 73 cos.write(buffer, 0, r); 74 } 75 cos.close(); 76 out.close(); 77 is.close(); 78 } 79 public static void main(String[] args){ 80 FileCode td = new FileCode("aaa"); 81 /* try { 82 td.encrypt("F:\测试加密.txt", "F:\测试加密1.txt"); 83 } catch (Exception e) { 84 // TODO Auto-generated catch block 85 e.printStackTrace(); 86 } //加密 */ 87 try { 88 td.decrypt("F:\测试加密1.txt", "F:\测试加密2.txt"); 89 } catch (Exception e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 92 } //解密 93 94 } 95 }

    运行截图:

    加密后,原本的文字会出现乱码。解密后,恢复正常。为了观察方便,将文件保存在多个文档里。

     三、编写一个文件分割工具,能把一个大文件分割成多个小的文件
    1
    package 文件分割工具; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 public class CutFile { 9 public static void main(String[] args) { 10 //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小) 11 cutFile("F:\测试加密.txt", "F:",10 * 10 * 20); 12 } 13 14 private static void cutFile(String src, String endsrc, int num) { 15 FileInputStream fis = null; 16 File file = null; 17 try { 18 fis = new FileInputStream(src); 19 file = new File(src); 20 //创建规定大小的byte数组 21 byte[] b = new byte[num]; 22 int len = 0; 23 //name为以后的小文件命名做准备 24 int name = 1; 25 //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中 26 while ((len = fis.read(b)) != -1) { 27 //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备 28 String name2 = file.getName(); 29 int lastIndexOf = name2.lastIndexOf("."); 30 String substring = name2.substring(0, lastIndexOf); 31 String substring2 = name2.substring(lastIndexOf, name2.length()); 32 FileOutputStream fos = new FileOutputStream(endsrc + "\\"+ substring + "-" + name + substring2); 33 //将byte数组写入对应的小文件中 34 fos.write(b, 0, len); 35 //结束资源 36 fos.close(); 37 name++; 38 } 39 } catch (FileNotFoundException e) { 40 e.printStackTrace(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } finally { 44 try { 45 if (fis != null) { 46 //结束资源 47 fis.close(); 48 } 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 }

    运行截图:

    只写了文件分割,还没写文件拼接。

  • 相关阅读:
    Python三维绘图--Matplotlib colorbar生成
    Python三维绘图--Matplotlib
    VIM剪切板的使用
    三维点云网络PointNet——模型及代码分析
    ECCV2018--点云匹配
    hdu 1905 小数化分数2
    hdu 1755 A Number Puzzle
    hdu 1796 How many integers can you find
    hdu 1452 Happy 2004
    hdu 2837 Calculation
  • 原文地址:https://www.cnblogs.com/zmh-980509/p/9984093.html
Copyright © 2020-2023  润新知