1.编写一个程序,指定一个文件夹,能自动计算出其总容量;
1 package com.test; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 6 public class Test2 { 7 static long size = 0; 8 private static ArrayList<String> filelist = new ArrayList<String>(); 9 10 public static void main(String[] args) { 11 Test2 s = new Test2(); 12 String filePath = "D:\\java"; 13 s.getFiles(filePath); 14 15 } 16 17 // 通过递归得到某一路径下所有的目录及文件 18 void getFiles(String filePath) { 19 20 File root = new File(filePath); 21 File[] files = root.listFiles(); 22 for (File file : files) { 23 if (file.isDirectory()) { 24 getFiles(file.getAbsolutePath()); 25 filelist.add(file.getAbsolutePath()); 26 } else { 27 size += file.getAbsolutePath().length(); 28 } 29 } 30 System.out.println("大小是" + size); 31 32 } 33 34 }
2.编写一个文件加解密程序,通过命令行完成加解密工作;
1 package com.test; 2 3 import java.io.File; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 9 public class Test2 { 10 private static final int numOfEncAndDec = 0x99;// 加密解密密钥 11 private static int dataOfFile = 0;// 文件字节内容 12 13 public static void main(String[] args) { 14 File srcFile = new File("D:\\file.txt");// 初始化文件 15 File encFile = new File("D:\\file1.txt"); // 加密文件 16 File decFile = new File("D:\\file2.txt"); // 解密文件 17 18 try { 19 EncFile(srcFile, encFile); // 加密操作 20 DecFile(encFile, decFile); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 } 25 26 private static void EncFile(File srcFile, File encFile) throws Exception { 27 if (!srcFile.exists()) { 28 System.out.println("source file not exixt"); 29 } 30 if (!encFile.exists()) { 31 System.out.println("encrypt file created"); 32 encFile.createNewFile();// 若无加密文件,新建一个加密文件 33 } 34 InputStream fis = new FileInputStream(srcFile); 35 OutputStream fos = new FileOutputStream(encFile); 36 37 while ((dataOfFile = fis.read()) > -1) {// 当读到文件内容时 38 fos.write(dataOfFile ^ numOfEncAndDec);// 将读出的内容加密后写入 39 } 40 fis.close(); 41 fos.flush(); 42 fos.close(); 43 } 44 45 private static void DecFile(File encFile, File decFile) throws Exception { 46 if (!encFile.exists()) { 47 System.out.println("encrypt file not exixt"); 48 } 49 if (!decFile.exists()) { 50 System.out.println("decrypt file created"); 51 decFile.createNewFile(); 52 } 53 InputStream fis = new FileInputStream(encFile); 54 OutputStream fos = new FileOutputStream(decFile); 55 56 while ((dataOfFile = fis.read()) > -1) { 57 fos.write(dataOfFile ^ numOfEncAndDec); 58 } 59 fis.close(); 60 fos.flush(); 61 fos.close(); 62 } 63 64 }
3.编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
1 package com.test; 2 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 9 public class Test2 { 10 public static void main(String[] args) { 11 //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小) 12 cutFile("D:\\file\\file.txt", "D:\\file2",1024 * 1024 * 20); 13 } 14 15 private static void cutFile(String src, String endsrc, int num) { 16 FileInputStream fis = null; 17 File file = null; 18 try { 19 fis = new FileInputStream(src); 20 file = new File(src); 21 //创建规定大小的byte数组 22 byte[] b = new byte[num]; 23 int len = 0; 24 //name为以后的小文件命名做准备 25 int name = 1; 26 //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中 27 while ((len = fis.read(b)) != -1) { 28 //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备 29 String name2 = file.getName(); 30 int lastIndexOf = name2.lastIndexOf("."); 31 String substring = name2.substring(0, lastIndexOf); 32 String substring2 = name2.substring(lastIndexOf, name2.length()); 33 FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2); 34 //将byte数组写入对应的小文件中 35 fos.write(b, 0, len); 36 //结束资源 37 fos.close(); 38 name++; 39 } 40 } catch (FileNotFoundException e) { 41 e.printStackTrace(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } finally { 45 try { 46 if (fis != null) { 47 //结束资源 48 fis.close(); 49 } 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } 53 } 54 } 55 }