Code example for File

Methods: existsgetAbsolutePath

0
        // move uploaded file to destination directory 
        // note: uploadDir and rootDir must be in the same volume 
        IO.mkdirs(targetDir, "directory to move a file to"); 
 
        // get ready to move uploaded file to destination directory 
        File oldFile = new File(sourceDir, filename); 
        File newFile = new File(targetDir, filename); 
 
        context.info("Source : " + oldFile.getAbsolutePath()); 
        context.info("Destin : " + newFile.getAbsolutePath()); 
 
        if (!oldFile.exists()) { 
            throw new Exception("File upload unsuccessful " 
                    + oldFile.getAbsolutePath() + " does not exist"); 
        } 
 
        // check if file already exists and do whatever overwrite wants 
        if (newFile.exists() && overwrite.equals("no")) { 
            throw new Exception("File upload unsuccessful because " 
                    + newFile.getName() 
                    + " already exists and overwrite was not permitted"); 
        } 
 
        // move uploaded file to destination directory - have two goes 
        try { 
            FileUtils.moveFile(oldFile, newFile); 
        } catch (Exception e) { 
            context.warning("Cannot move uploaded file"); 
            context.warning(" (C) Source : " + oldFile.getAbsolutePath()); 
            context.warning(" (C) Destin : " + newFile.getAbsolutePath()); 
            IO.delete(oldFile, false, context); 
            throw new Exception( 
                    "Unable to move uploaded file to destination directory"); 
        } 
    } 
}