鱼C论坛

 找回密码
 立即注册
查看: 665|回复: 1

关于File类的一些问题

[复制链接]
发表于 2022-12-23 23:18:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. /*
  2. 此类用于对于文件或文件夹所有的内容进行初始化
  3. * */
  4. public class FileInit {
  5.     String path;

  6.     public String getPath() {
  7.         return path;
  8.     }

  9.     public void setPath(String path) {
  10.         this.path = path;
  11.     }

  12.     //查询文件方法,将会返回包含文件信息的列表
  13.     public String queryFiles() throws NoSuchAlgorithmException, IOException {
  14.         //查询路径,构建一个File集合
  15.         File file = new File(path);
  16.         File[] files = file.listFiles();
  17.         //构建一个List用于存放转化后的FileItem
  18.         List<FileItem> fileItems = new ArrayList<>();
  19.         //遍历files做判断
  20.         for (File f:files){
  21.             FileItem fileItem = new FileItem(f);
  22.             fileItem.printFile();
  23.             fileItems.add(fileItem);
  24.         }
  25.         Gson gson = new Gson();
  26.         return gson.toJson(fileItems);
  27.     }
  28.     //格式化输出
  29.     public void printFiles(String files){
  30.         Gson gson = new Gson();
  31.         Type type = new TypeToken<List<FileItem>>(){}.getType();
  32.         List<FileItem> fileItems = gson.fromJson(files, type);
  33.         // 格式化输出文件列表
  34.         int count = 0;
  35.         for (FileItem f :fileItems ) {
  36.             String name;
  37.             if (f.getFileType()==null){
  38.                 name = f.getFileName() + "/";
  39.             }else {
  40.                 name = f.getFileName();
  41.             }
  42.             System.out.printf("%-40s", name);
  43.             count++;
  44.             if (count % 3 == 0) {
  45.                 System.out.println();
  46.             }
  47.         }
  48.         System.out.println();
  49.     }
  50.     //更改目录命令
  51.     public void changeDic(String addPath){
  52.         File fileDic1 = new File(path+addPath);
  53.         File fileDic2 = new File(addPath);
  54.         if (addPath.equals("..")) {
  55.             File parent = new File(path).getParentFile();
  56.             if (parent != null) {
  57.                 this.path = parent.getPath();
  58.             }else{
  59.                 System.out.println("上级目录不存在");
  60.             }
  61.         }
  62.         else {
  63.             if (fileDic1.exists()){
  64.                 this.path = path+addPath;
  65.             } else if (fileDic2.exists()) {
  66.                 this.path = addPath;
  67.             }else{
  68.                 System.out.println("输入路径非法");
  69.             }
  70.         }
  71.     }
  72. }
复制代码
  1. public class FileItem {
  2.     private String fileName;
  3.     private String fileHash;
  4.     private String filePath;
  5.     private long fileLength;
  6.     private String fileType;

  7.     //FileItem的构造方法
  8.     //构建一个只需fileName和filePath的构造方法,根据是文件夹或文件自行判断是否计算哈希值,文件大小,文件类型
  9.     public FileItem(String fileName,String filePath) {
  10.         this.fileName = fileName;
  11.         this.filePath = filePath;
  12.         File file =new File(this.filePath+"/"+this.fileName);
  13.         if (file.isFile()){
  14.             try {
  15.                 //通过file内置方法获取文件大小
  16.                 this.fileLength = file.length();
  17.                 // 定义正则表达式,用于提取文件名的后缀
  18.                 String regex = "\\.(\\w+)$";
  19.                 // 编译正则表达式
  20.                 Pattern pattern = Pattern.compile(regex);
  21.                 // 创建一个Matcher对象
  22.                 Matcher matcher = pattern.matcher(fileName);
  23.                 // 如果匹配成功
  24.                 if (matcher.find()) {
  25.                     // 获取文件名的后缀
  26.                     this.fileType = matcher.group(1);
  27.                 }else{
  28.                     this.fileType = null;
  29.                 }
  30.                 //通过调用FileHash方法计算文件的Hash值
  31.                 this.fileHash=FileHash(file.getPath());
  32.                 System.out.printf(fileHash);
  33.                 System.out.print("\n");
  34.             } catch (NoSuchAlgorithmException | IOException e) {
  35.                 throw new RuntimeException(e);
  36.             }
  37.         }else{
  38.             this.fileName=fileName;
  39.             this.fileLength=0;
  40.             this.fileType = null;
  41.             this.fileHash=null;
  42.         }
  43.     }

  44.     //构建一个只需要json文件的构造方法
  45.     public FileItem(String json){
  46.         Gson gson = new Gson();
  47.         FileItem fileItem = gson.fromJson(json,FileItem.class);
  48.         this.fileName=fileItem.getFileName();
  49.         this.filePath=fileItem.getFilePath();
  50.         this.fileHash=fileItem.getFileHash();
  51.         this.fileLength=fileItem.getFileLength();
  52.         this.fileType=fileItem.getFileType();
  53.     }

  54.     //实现FileItem和File类的相互转化
  55.     public File toFile(){
  56.         return new File(this.filePath+"/"+this.fileName);
  57.     }

  58.     public FileItem(File file) throws NoSuchAlgorithmException, IOException {
  59.         FileItem fileItem = new FileItem(file.getName(),file.getPath());
  60.         this.fileName=fileItem.getFileName();
  61.         this.filePath=fileItem.getFilePath();
  62.         this.fileHash=fileItem.getFileHash();
  63.         this.fileLength=fileItem.getFileLength();
  64.         this.fileType=fileItem.getFileType();
  65.     }

  66.     //展示FileItem相关信息
  67.     public void printFile(){
  68.         if (fileType==null){
  69.             System.out.println(fileName+"是文件夹");
  70.         }else {
  71.             System.out.println(fileName+"是"+fileType+"文件");
  72.         }
  73.         double fileLenOp=(double)fileLength;
  74.         //不同的文件大小采用不同的输出方式
  75.         if(fileLength<=1024*1024){
  76.             System.out.printf("文件大小为:%.3fKB\n",fileLenOp/1024);
  77.         } else if ((fileLength<1024*1024*1024)) {
  78.             System.out.printf("文件大小为:%.3fMB\n",fileLenOp/1024/1024);
  79.         }else {
  80.             System.out.printf("文件大小为:%.3fGB\n",fileLenOp/1024/1024/1024);
  81.         }
  82.         System.out.println("文件哈希为:"+fileHash);
  83.     }


  84.     //FileItem的Getter和Setter方法

  85.     public String getFileName() {
  86.         return fileName;
  87.     }

  88.     public void setFileName(String fileName) {
  89.         this.fileName = fileName;
  90.     }

  91.     public String getFileHash() {
  92.         return fileHash;
  93.     }

  94.     public void setFileHash(String fileHash) {
  95.         this.fileHash = fileHash;
  96.     }

  97.     public String getFilePath() {
  98.         return filePath;
  99.     }

  100.     public void setFilePath(String filePath) {
  101.         this.filePath = filePath;
  102.     }

  103.     public long getFileLength() {
  104.         return fileLength;
  105.     }

  106.     public void setFileLength(long fileLength) {
  107.         this.fileLength = fileLength;
  108.     }

  109.     public String getFileType() {
  110.         return fileType;
  111.     }

  112.     public void setFileType(String fileType) {
  113.         this.fileType = fileType;
  114.     }
  115. }
复制代码

在FileInit这个类中的queryFiles()方法中,遍历files里的每个元素f,这个f的f.isFile()始终都是false是为什么呢?
注释掉有关FileItem的内容后就会f.isFile就会正常,但是只要由FileItem的内容就始终为false.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-5 16:24:17 | 显示全部楼层
看不出你的设参,你可以打断点看下File file =new File(this.filePath+"/"+this.fileName); 这个地方  括号内应该是一个文件路径,路径里面有文件类型。如果路径不对或者没有文件类型都会返回false。
问题如果解决了,请告知我,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 04:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表