java修改文件名

public void renameFile(String oldFile, String newFile) {

  HttpServletRequest request = ServletActionContext.getRequest();
  String path =  request.getSession().getServletContext().getRealPath("/");
  String updatePath = path+"upload"+File.separator;
        File oldfile = new File(updatePath+oldFile);
       
        //檢查要重命名的文件是否存在,是否是文件
        if (!oldfile.exists() || !oldfile.isDirectory()) {

            System.out.println("File does not exist: " + oldFile);
            return;
        }

        File newfile = new File(updatePath+newFile);

        //修改文件名
        if (oldfile.renameTo(newfile)) {
            System.out.println("File has been renamed.");
        } else {
            System.out.println("Error renmaing file");
        }

    }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章