zoukankan      html  css  js  c++  java
  • java Files 和 Path对文件操作

    1.拷贝文件

    /**
         * 拷贝文件,生成新的文件名
         * @param pathUpload
         * @return
         */
        private String converUploadFileName(String pathUpload){
            String resultPath = null;
            File file = null;
            try {
                
                file = new File(pathUpload);    
                Path path = null;
                if(file.exists()){
                    //1.创建临时文件目录
                    Path targetFilePath = Paths.get(file.getParentFile().getAbsolutePath()+"\temp");
                    path = Files.createDirectory(targetFilePath);
                    
                    if(!targetFilePath.toFile().exists()){
                    
                        //2.拷贝指定文件 生成新的文件名
                        Path srcFile = file.toPath();    //源文件
                        Path targetFile = new File(path.toFile().getAbsolutePath()+"\aa").toPath(); //生成目标文件
                        Path newFile = Files.copy(srcFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
                        resultPath = newFile.toFile().getAbsolutePath();
                    }else{
                        Path srcFile = file.toPath();    //源文件
                        Path targetFile = new File(path.toFile().getAbsolutePath()+"\aa").toPath(); //生成目标文件
                        Path newFile = Files.copy(srcFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
                        resultPath = newFile.toFile().getAbsolutePath();
                    }
                }
            } catch (IOException e) {
                resultPath = "";
                LOG.error("xxxxxxxxxxxxxx conver upload fileName 异常    xxxxxxxxxxxxxx "+e);
            }
            return resultPath;
        }

    2.删除指定文件

    /**
         * 删除上传的临时文件
         * @param path
         * @return
         */
        public int delTempFile(String path){
            int flag = 0;
            try {
                File srcFile = new File(path);
                
                //1.删除文件、目录
                Files.delete(Paths.get(srcFile.getParentFile().getAbsolutePath()+"\temp\aa"));
                Files.delete(Paths.get(srcFile.getParentFile().getAbsolutePath()+"\temp\"));
                
                //2.验证
                int size = Arrays.stream(new File(srcFile.getParentFile().getAbsolutePath()).listFiles())
                      .filter(f->f.getName().equals("temp"))
                      .collect(Collectors.toList()).size();
                
                //3.返回
                if(size == 0){
                    LOG.debug("删除文件成功:"+srcFile.getName());
                    return flag;
                }else{
                    LOG.debug("删除文件失败:"+srcFile.getName());
                    flag = 1;
                }
                
            } catch (IOException e) {
                LOG.error("xxxxxxxxxxxxxxxxxxxxxxx 删除临时文件异常 xxxxxxxxxxxxxxxxxxxxxxxx "+e);
            }
            return flag;
        }
  • 相关阅读:
    自然语言处理3.4——使用正则表达式检测词组搭配
    自然语言处理3.3——使用Unicode进行文字处理
    自然语言处理3.1——从网络和硬盘访问文本
    自然语言处理2.3——词典资源
    自然语言处理2.2——条件频率分布
    自然语言处理——NLTK中文语料库语料库
    自然语言处理2.1——NLTK文本语料库
    【转载】使用LFM(Latent factor model)隐语义模型进行Top-N推荐
    Ajax (jquery)实现智能提示搜索框(in Django)
    python操作mysql数据库
  • 原文地址:https://www.cnblogs.com/MrRightZhao/p/11673099.html
Copyright © 2011-2022 走看看