Java移動文件到另外一個目錄


private void moveTotherFolders(String pathName,String fileName,String ansPath){
    String startPath = this.path + pathName + File.separator + fileName;
    String endPath = ansPath + File.separator + currentDate + File.separator;
    try {
        File startFile = new File(startPath);
        File tmpFile = new File(endPath);//獲取文件夾路徑
        if(!tmpFile.exists()){//判斷文件夾是否創建,沒有創建則創建新文件夾
            tmpFile.mkdirs();
        }
        System.out.println(endPath + startFile.getName());
        if (startFile.renameTo(new File(endPath + startFile.getName()))) {
            System.out.println("File is moved successful!");
            log.info("文件移動成功!文件名:《{}》 目標路徑:{}",fileName,endPath);
        } else {
            System.out.println("File is failed to move!");
            log.info("文件移動失敗!文件名:《{}》 起始路徑:{}",fileName,startPath);
        }
    } catch (Exception e) {
        log.info("文件移動異常!文件名:《{}》 起始路徑:{}",fileName,startPath);

    }
}

 

這個方法的作用就是,將startPath 下的文件 fileName 移動到endPath 下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM