• 关于文件属性(Java)


    1、编写一个程序,指定一个文件夹,能自动计算出其总容量

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

    2、编写一个文件加解密程序,通过命令行完成加解密工作

    package file_attribute_analysis;
    
    import java.io.File;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class FileAction {
        private static final int numOfEncAndDec=0x99;//加密解密密钥
        private static int dataOfFile=0;//文件字节内容
    
        public static void main(String[] args) {
        File srcFile=new File("G:\encryption_text.txt");//初始化文件
        File encFile=new File("G:\encryption_text01.txt"); //加密文件
        File decFile=new File("G:\encryption_text02.txt");  //解密文件
      
        try {
            EncFile(srcFile,encFile);  //加密操作
            DecFile(encFile,decFile);
        }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、编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件

    1.分割文件:

    复制代码
     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 
     8 public class CutFile {
     9     public static void main(String[] args) {
    10         //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
    11         cutFile("D:\file\file.txt", "D:\file2",1024 * 1024 * 20);
    12     }
    13 
    14     private static void cutFile(String src, String endsrc, int num) {
    15         FileInputStream fis = null;
    16         File file = null;
    17         try {
    18             fis = new FileInputStream(src);
    19             file = new File(src);
    20             //创建规定大小的byte数组
    21             byte[] b = new byte[num];
    22             int len = 0;
    23             //name为以后的小文件命名做准备
    24             int name = 1;
    25             //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
    26             while ((len = fis.read(b)) != -1) {
    27                 //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
    28                 String name2 = file.getName();
    29                 int lastIndexOf = name2.lastIndexOf(".");
    30                 String substring = name2.substring(0, lastIndexOf);
    31                 String substring2 = name2.substring(lastIndexOf, name2.length());
    32                 FileOutputStream fos = new FileOutputStream(endsrc + "\\"+ substring + "-" + name + substring2);
    33                 //将byte数组写入对应的小文件中
    34                 fos.write(b, 0, len);
    35                 //结束资源
    36                 fos.close();
    37                 name++;
    38             }
    39         } catch (FileNotFoundException e) {
    40             e.printStackTrace();
    41         } catch (IOException e) {
    42             e.printStackTrace();
    43         } finally {
    44             try {
    45                 if (fis != null) {
    46                     //结束资源
    47                     fis.close();
    48                 }
    49             } catch (IOException e) {
    50                 e.printStackTrace();
    51             }
    52         }
    53     }
    54 }
    复制代码

    实现结果截图:

    未操作前:

    分割后:

    文件的集合:

    复制代码
     1 package class6;
     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 GotherFile {
    10     public static void main(String[] args){
    11         //调用togetherFile()函数合并小文件到大文件   参数列表分别为 (小文件所在的父文件夹路径,所合成的大文件的路径)
    12         togetherFile("D:\file2","D:\file3\file.txt");
    13     }
    14     private static void togetherFile(String src, String endsrc){
    15         FileOutputStream fos = null;
    16         File file1 = null;
    17         File file2 = null;
    18         try {
    19             file1 = new File(endsrc);
    20             file2 = new File(src);
    21             //获得大文件的存储路径的FileOutputStream对象
    22             fos = new FileOutputStream(endsrc);
    23             //循环遍历对应文件夹中的所有小文件
    24             for(File file : file2.listFiles()){
    25 
    26                 FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    27 
    28                 byte[] b = new byte[1024*1024];
    29                 int len = 0;
    30                 //将小文件读入byte数组,之后再将byte数组写入大文件中
    31                 while((len = fis.read(b)) != -1){
    32                     fos.write(b, 0, len);
    33                 }
    34                 //结束资源
    35                 fis.close();
    36             }
    37         } catch (FileNotFoundException e) {
    38             e.printStackTrace();
    39         } catch (IOException e) {
    40             e.printStackTrace();
    41         }finally{
    42             try {
    43                 if(fos != null){
    44                     //结束资源
    45                     fos.close();
    46                 }
    47             } catch (IOException e) {
    48                 e.printStackTrace();
    49             }
    50         }
    51     }
    52 }
    复制代码

    操作前:

    集合操作后:

  • 相关阅读:
    python开发mysql:索引
    学习笔记之English
    本周学习小结(25/03
    本周学习小结(18/03
    本周学习小结(11/03
    学习笔记之Machine Learning Crash Course | Google Developers
    本周学习小结
    学习笔记之Fluent Python
    Leetcode 4. Median of Two Sorted Arrays
    学习笔记之Python爬虫
  • 原文地址:https://www.cnblogs.com/leity/p/9981285.html
Copyright © 2020-2023  润新知