1.编写一个程序,指定一个文件夹,能自动计算出其总容量
123456789
public class Think1 { public static void main(String [] args) throws IOException { File f=new File("F:\\FiveOne.txt"); System.out.println(f.length()); } }
运行结果:9
2.编写一个文件加解密程序,通过命令行完成加解密工作
import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; //编写一个文件加解密程序,通过命令行完成加解密工作 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(); } }
3.编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
package 压缩包; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; public class Test3 extends SimpleFileVisitor<Path> { public static ArrayList<Path> smallfiles = new ArrayList<>(); public static void main(String[] args) { cut("D:/java/eclipse/压缩包/test.txt", "D:/java/eclipse/压缩包/test", 1024 * 1024 * 1); paste("D:/java/eclipse/压缩包/test", "D:/java/eclipse/压缩包/test0", 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); Test3 visitor = new Test3(); 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; } }
原文:https://www.cnblogs.com/birdmmxx/p/9986737.html