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"); } }