1.编写一个程序,指定一个文件夹,能自动计算出其总容量
package test; import java.io.File; import java.util.ArrayList; public class test1 { static long size=0; private static ArrayList<String> filelist=new ArrayList<String>(); public static void main(String[] args) { test1 s=new test1(); String filePath="D:\\abc"; 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); } }
2.编写一个文件加解密程序,通过命令行完成加解密工作
package test; import java.io.*; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class test2 { public static void encryption() throws IOException { BufferedInputStream sc = new BufferedInputStream(new FileInputStream("D:\\abc\\1.txt")); BufferedOutputStream scan = new BufferedOutputStream(new FileOutputStream("D:\\abc\\2.txt")); int b; while((b = sc.read()) != -1) { scan.write(b ^ 123); } sc.close(); scan.close(); } public static void main(String[] args) throws IOException { encryption(); } }
3.编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
package test; import java.io.*; public class test3 { public static void main(String args[]) { try { FileReader read = new FileReader("D:\\abc\\1.txt"); BufferedReader br = new BufferedReader(read); String row; int rownum = 1; int fileNo = 1; FileWriter fw = new FileWriter("D:/text"+fileNo +".txt"); while ((row = br.readLine()) != null) { rownum ++; fw.append(row + "\r\n"); if((rownum / 464183) > (fileNo - 1)){ fw.close(); fileNo ++ ; fw = new FileWriter("D:/text"+fileNo +".txt"); } } fw.close(); System.out.println("rownum="+rownum+";fileNo="+fileNo); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
原文:https://www.cnblogs.com/zlj843767688/p/9986730.html