|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- /*
- 此类用于对于文件或文件夹所有的内容进行初始化
- * */
- public class FileInit {
- String path;
- public String getPath() {
- return path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- //查询文件方法,将会返回包含文件信息的列表
- public String queryFiles() throws NoSuchAlgorithmException, IOException {
- //查询路径,构建一个File集合
- File file = new File(path);
- File[] files = file.listFiles();
- //构建一个List用于存放转化后的FileItem
- List<FileItem> fileItems = new ArrayList<>();
- //遍历files做判断
- for (File f:files){
- FileItem fileItem = new FileItem(f);
- fileItem.printFile();
- fileItems.add(fileItem);
- }
- Gson gson = new Gson();
- return gson.toJson(fileItems);
- }
- //格式化输出
- public void printFiles(String files){
- Gson gson = new Gson();
- Type type = new TypeToken<List<FileItem>>(){}.getType();
- List<FileItem> fileItems = gson.fromJson(files, type);
- // 格式化输出文件列表
- int count = 0;
- for (FileItem f :fileItems ) {
- String name;
- if (f.getFileType()==null){
- name = f.getFileName() + "/";
- }else {
- name = f.getFileName();
- }
- System.out.printf("%-40s", name);
- count++;
- if (count % 3 == 0) {
- System.out.println();
- }
- }
- System.out.println();
- }
- //更改目录命令
- public void changeDic(String addPath){
- File fileDic1 = new File(path+addPath);
- File fileDic2 = new File(addPath);
- if (addPath.equals("..")) {
- File parent = new File(path).getParentFile();
- if (parent != null) {
- this.path = parent.getPath();
- }else{
- System.out.println("上级目录不存在");
- }
- }
- else {
- if (fileDic1.exists()){
- this.path = path+addPath;
- } else if (fileDic2.exists()) {
- this.path = addPath;
- }else{
- System.out.println("输入路径非法");
- }
- }
- }
- }
复制代码- public class FileItem {
- private String fileName;
- private String fileHash;
- private String filePath;
- private long fileLength;
- private String fileType;
- //FileItem的构造方法
- //构建一个只需fileName和filePath的构造方法,根据是文件夹或文件自行判断是否计算哈希值,文件大小,文件类型
- public FileItem(String fileName,String filePath) {
- this.fileName = fileName;
- this.filePath = filePath;
- File file =new File(this.filePath+"/"+this.fileName);
- if (file.isFile()){
- try {
- //通过file内置方法获取文件大小
- this.fileLength = file.length();
- // 定义正则表达式,用于提取文件名的后缀
- String regex = "\\.(\\w+)$";
- // 编译正则表达式
- Pattern pattern = Pattern.compile(regex);
- // 创建一个Matcher对象
- Matcher matcher = pattern.matcher(fileName);
- // 如果匹配成功
- if (matcher.find()) {
- // 获取文件名的后缀
- this.fileType = matcher.group(1);
- }else{
- this.fileType = null;
- }
- //通过调用FileHash方法计算文件的Hash值
- this.fileHash=FileHash(file.getPath());
- System.out.printf(fileHash);
- System.out.print("\n");
- } catch (NoSuchAlgorithmException | IOException e) {
- throw new RuntimeException(e);
- }
- }else{
- this.fileName=fileName;
- this.fileLength=0;
- this.fileType = null;
- this.fileHash=null;
- }
- }
- //构建一个只需要json文件的构造方法
- public FileItem(String json){
- Gson gson = new Gson();
- FileItem fileItem = gson.fromJson(json,FileItem.class);
- this.fileName=fileItem.getFileName();
- this.filePath=fileItem.getFilePath();
- this.fileHash=fileItem.getFileHash();
- this.fileLength=fileItem.getFileLength();
- this.fileType=fileItem.getFileType();
- }
- //实现FileItem和File类的相互转化
- public File toFile(){
- return new File(this.filePath+"/"+this.fileName);
- }
- public FileItem(File file) throws NoSuchAlgorithmException, IOException {
- FileItem fileItem = new FileItem(file.getName(),file.getPath());
- this.fileName=fileItem.getFileName();
- this.filePath=fileItem.getFilePath();
- this.fileHash=fileItem.getFileHash();
- this.fileLength=fileItem.getFileLength();
- this.fileType=fileItem.getFileType();
- }
- //展示FileItem相关信息
- public void printFile(){
- if (fileType==null){
- System.out.println(fileName+"是文件夹");
- }else {
- System.out.println(fileName+"是"+fileType+"文件");
- }
- double fileLenOp=(double)fileLength;
- //不同的文件大小采用不同的输出方式
- if(fileLength<=1024*1024){
- System.out.printf("文件大小为:%.3fKB\n",fileLenOp/1024);
- } else if ((fileLength<1024*1024*1024)) {
- System.out.printf("文件大小为:%.3fMB\n",fileLenOp/1024/1024);
- }else {
- System.out.printf("文件大小为:%.3fGB\n",fileLenOp/1024/1024/1024);
- }
- System.out.println("文件哈希为:"+fileHash);
- }
- //FileItem的Getter和Setter方法
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- public String getFileHash() {
- return fileHash;
- }
- public void setFileHash(String fileHash) {
- this.fileHash = fileHash;
- }
- public String getFilePath() {
- return filePath;
- }
- public void setFilePath(String filePath) {
- this.filePath = filePath;
- }
- public long getFileLength() {
- return fileLength;
- }
- public void setFileLength(long fileLength) {
- this.fileLength = fileLength;
- }
- public String getFileType() {
- return fileType;
- }
- public void setFileType(String fileType) {
- this.fileType = fileType;
- }
- }
复制代码
在FileInit这个类中的queryFiles()方法中,遍历files里的每个元素f,这个f的f.isFile()始终都是false是为什么呢?
注释掉有关FileItem的内容后就会f.isFile就会正常,但是只要由FileItem的内容就始终为false. |
|