编写一个程序,指定一个文件夹,能自动计算出其总容量:import java.io.File;
import java.util.ArrayList; public class FileAction { ArrayList<File> fileList; File root; public FileAction(String pathName) { root = new File(pathName); fileList = new ArrayList<>(); } public void searchFiles() { File[] files = root.listFiles(); int length = files.length; for (int i = 0; i < length; i++) { if (files[i].isDirectory()) { root = files[i]; searchFiles(); } else { fileList.add(files[i]); } } } public void countFiles() { long totalSize = 0; System.out.println("文件数:" + fileList.size()); for (int i = 0; i < fileList.size(); i++) { totalSize += fileList.get(i).length(); } System.out.println("文件总大小:" + totalSize); } public static void main(String[] args) { String pathName = "E:\音乐"; FileAction counter = new FileAction(pathName); counter.searchFiles(); counter.countFiles(); } }
编写一个文件加解密程序,通过命令行完成加解密工作:
import java.io.*; public class FileCode
{ private static final int numOfEncAndDec=0x99;//加密解密密钥 private static int dataOfFile=0;//文件字节内容 public static void main(String[] args)
{ File srcFile=new File("E:\新建文件夹\poem.txt");//初始化文件 File encFile=new File("E:\新建文件夹\poem1.txt"); //加密文件 File decFile=new File("E:\新建文件夹\poem2.txt"); //解密文件 try { //EncFile(srcFile,encFile); //加密操作 //DecFile(encFile,decFile);//解密操作 EncFile(srcFile,decFile); //加密操作 DecFile(decFile,encFile); }catch(Exception e) { e.printStackTrace(); } } private static void EncFile(File srcFile,File encFile)throws Exception{ if(!srcFile.exists()) { System.out.println("source file not exixt"); } if(!encFile.exists()) { System.out.println("encrypt file created"); encFile.createNewFile();//若无加密文件,新建一个加密文件 } InputStream fis=new FileInputStream(srcFile); OutputStream fos=new FileOutputStream(encFile); while((dataOfFile=fis.read())>-1) {//当读到文件内容时 fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入 } fis.close(); fos.flush(); fos.close(); } private static void DecFile(File encFile,File decFile)throws Exception{ if(!encFile.exists()) { System.out.println("encrypt file not exixt"); } if(!decFile.exists()) { System.out.println("decrypt file created"); decFile.createNewFile(); } InputStream fis=new FileInputStream(encFile); OutputStream fos=new FileOutputStream(decFile); while((dataOfFile=fis.read())>-1) { fos.write(dataOfFile^numOfEncAndDec); } fis.close(); fos.flush(); fos.close(); } }
编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件
package filesoperation; import java.io.*;import java.nio.*;import java.util.ArrayList; public class CutFile extends SimpleFileVisitor<Path>
{ public static ArrayList<Path> smallfiles = new ArrayList<>(); public static void main(String[] args) { cut("E:\test.txt", "E:\test", 1024 * 1024 * 1); paste("E:\test", "E:\test1", 1024 * 1024 * 1); } public static void cut(String filepath, String endpath, int sizeofsmallfile) { //原文件,切割后小文件路径,小文件最大容量 FileInputStream fis = null; File file = null; try { fis = new FileInputStream(filepath); file = new File(filepath); byte[] b = new byte[sizeofsmallfile]; int read = 0; int numberofsmallfile = 1; while ((read = fis.read(b)) != -1) { String nameoflargefile = file.getName(); //获取大文件全名(带扩展名) String nameofsmallfile = nameoflargefile.substring(0, nameoflargefile.lastIndexOf(".")); //获取小文件名(不带扩展名) String extension = nameoflargefile.substring(nameoflargefile.lastIndexOf("."), nameoflargefile.length()); //获取扩展名 FileOutputStream fos = new FileOutputStream(endpath + "\" + nameofsmallfile + "-" + numberofsmallfile + extension); //创建小文件 fos.write(b, 0, read); //写入 fos.close(); numberofsmallfile++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void paste(String smallfilespath, String endpath, int sizeofsmallfile) { //小文件路径,生成的大文件路径,小文件最大容量 Path fileDirPath = Paths.get(smallfilespath); CutFile visitor = new CutFile(); try { Files.walkFileTree(fileDirPath, visitor); File file = new File(smallfiles.get(0).toString()); String nameofsmallfile = file.getName(); //获取第一个小文件全名(带扩展名) String nameoflargefile = nameofsmallfile.substring(0, nameofsmallfile.lastIndexOf("-")); //获取大文件名(不带扩展名) String extension = nameofsmallfile.substring(nameofsmallfile.lastIndexOf("."), nameofsmallfile.length()); //获取扩展名 FileOutputStream fos = new FileOutputStream(endpath + "\" + nameoflargefile + extension); //创建大文件 byte[] b = new byte[sizeofsmallfile]; for(Path p:smallfiles) { FileInputStream fis = new FileInputStream(p.toString()); int read = fis.read(b); fis.close(); fos.write(b, 0, read); //写入 } fos.close(); }catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if(attrs.isRegularFile()){ smallfiles.add(file); } return FileVisitResult.CONTINUE; } }