Example usage for java.io File renameTo

List of usage examples for java.io File renameTo

Introduction

In this page you can find the example usage for java.io File renameTo.

Prototype

public boolean renameTo(File dest) 

Source Link

Document

Renames the file denoted by this abstract pathname.

Usage

From source file:eu.stratosphere.nephele.fs.file.LocalFileSystem.java

/**
 * {@inheritDoc}/*w ww .java 2 s  .  c  o m*/
 */
@Override
public boolean rename(final Path src, final Path dst) throws IOException {

    final File srcFile = pathToFile(src);
    final File dstFile = pathToFile(dst);

    return srcFile.renameTo(dstFile);
}

From source file:DropReceivedLines.java

/** Process one file given only its name */
public void process(String fileName) throws IOException {
    File old = new File(fileName);
    String newFileName = fileName + ".TMP";
    File newf = new File(newFileName);
    BufferedReader is = new BufferedReader(new FileReader(fileName));
    PrintWriter p = new PrintWriter(new FileWriter(newFileName));
    process(is, p); // call other process(), below
    p.close();//from w  w  w.java2  s  . c  o  m
    old.renameTo(tempFile);
    newf.renameTo(old);
    tempFile.delete();
}

From source file:Manager.java

public void loadMovieFiles() throws SQLException {
    File folder = new File(Configuration.UPLOAD_FOLDER);
    File[] files = folder.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String movieName = this.getFilm((i + 1) * 8);
        movieName = movieName.trim().toLowerCase().replace(" ", "_");

        System.out.println(movieName);
        file.renameTo(new File(Configuration.UPLOAD_FOLDER + File.separator + movieName + ".mp4"));
    }/*from  w  ww .  j a  v  a2 s .  c  o  m*/
    System.out.println("Renamed :" + files.length + "Files");
}

From source file:com.at.lic.LicenseControl.java

private void generateLicense(String licenseCheckCode) throws Exception {

    String lcc = licenseCheckCode;
    String lic_code = DigestUtils.md5Hex(lcc);
    String date = License.getSimpleDateFormat().format(new Date());

    StringBuilder sb = new StringBuilder();
    sb.append(lcc);/*from ww  w  .  j a v a  2  s  . c  o  m*/
    sb.append(lic_code);
    sb.append(date);

    byte[] signature = RSAKeyManager.sign(sb.toString());
    String sign_code = Base64.encodeBase64String(signature);

    License lic = new License();
    lic.setCheckCode(lcc);
    lic.setLicenseCode(lic_code);
    lic.setSignCode(sign_code);
    lic.setSignDate(date);

    File f = new File("cfg/grgbanking.lic");
    if (!f.exists()) {
        File pf = f.getParentFile();
        if (!pf.exists()) {
            pf.mkdirs();
        }
    } else {
        f.renameTo(new File(f.getAbsolutePath() + ".bak"));
    }
    Serializer serializer = new Persister();
    serializer.write(lic, f);

    log.info("Generating License Successful.");
}

From source file:com.parse.ParseFileUtils.java

/**
 * Moves a file./*from w ww  . ja va2s.co m*/
 * <p>
 * When the destination file is on another file system, do a "copy and delete".
 *
 * @param srcFile the file to be moved
 * @param destFile the destination file
 * @throws NullPointerException if source or destination is {@code null}
 * @throws FileExistsException if the destination file exists
 * @throws IOException if source or destination is invalid
 * @throws IOException if an IO error occurs moving the file
 * @since 1.4
 */
public static void moveFile(final File srcFile, final File destFile) throws IOException {
    if (srcFile == null) {
        throw new NullPointerException("Source must not be null");
    }
    if (destFile == null) {
        throw new NullPointerException("Destination must not be null");
    }
    if (!srcFile.exists()) {
        throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
    }
    if (srcFile.isDirectory()) {
        throw new IOException("Source '" + srcFile + "' is a directory");
    }
    if (destFile.exists()) {
        throw new IOException("Destination '" + destFile + "' already exists");
    }
    if (destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' is a directory");
    }
    final boolean rename = srcFile.renameTo(destFile);
    if (!rename) {
        copyFile(srcFile, destFile);
        if (!srcFile.delete()) {
            ParseFileUtils.deleteQuietly(destFile);
            throw new IOException(
                    "Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'");
        }
    }
}

From source file:com.altoukhov.svsync.fileviews.LocalFileSpace.java

@Override
public boolean moveFile(String oldPath, String newPath) {
    File oldFile = new File(toAbsolutePath(oldPath));
    File newFile = new File(toAbsolutePath(newPath));
    return oldFile.renameTo(newFile);
}

From source file:com.inkubator.hrm.service.impl.BioSertifikasiServiceImpl.java

@Override
@Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void save(BioSertifikasi entity, UploadedFile documentFile) throws Exception {
    entity.setId(Long.parseLong(RandomNumberUtil.getRandomNumber(9)));
    BioData biodata = bioDataDao.getEntiyByPK(entity.getBioData().getId());
    OccupationType occupationType = occupationTypeDao.getEntiyByPK(entity.getOccupationType().getId());
    EducationNonFormal educationNonFormal = educationNonFormalDao
            .getEntiyByPK(entity.getEducationNonFormal().getId());

    entity.setBioData(biodata);/* w  w w  .  j  a  v  a2s  .  c om*/
    entity.setOccupationType(occupationType);
    entity.setEducationNonFormal(educationNonFormal);
    entity.setCreatedBy(UserInfoUtil.getUserName());
    entity.setCreatedOn(new Date());
    bioSertifikasiDao.save(entity);

    if (documentFile != null) {
        String uploadPath = getUploadPath(entity.getId(), documentFile);
        facesIO.transferFile(documentFile);
        File file = new File(facesIO.getPathUpload() + documentFile.getFileName());
        file.renameTo(new File(uploadPath));

        entity.setUploadPath(uploadPath);
        bioSertifikasiDao.update(entity);
    }
}

From source file:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

/**
 * Adds a new file to the {@link ZipOutputStream}.
 * //ww w. j a  v  a  2s .com
 * @param fileName
 * @param zos
 * @param originalFileName
 * @throws FileNotFoundException
 * @throws IOException
 */
private void addToZipFile(final String fileName, final ZipOutputStream zos, final String originalFileName)
        throws IOException {

    // File is opened.
    final File file = new File(fileName);

    // File is renamed.
    final String newName = fileName.substring(fileName.indexOf(originalFileName), fileName.length());
    final File renamedFile = new File(newName);
    file.renameTo(renamedFile);

    // File is included into ZIP.
    final FileInputStream fis = new FileInputStream(renamedFile);
    final ZipEntry zipEntry = new ZipEntry(newName);
    zos.putNextEntry(zipEntry);

    final byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    // Closing elements.
    zos.closeEntry();
    fis.close();

    // File is deleted
    this.fileUtils.delete(newName);
}

From source file:com.taobao.tanggong.DataFilePersisterImpl.java

private void saveHash2UrlMap() {
    if (null != dataDirectory && !this.uri2HashCodes.isEmpty()) {
        File url2HashMapFile = new File(this.dataDirectory, "index.txt");

        File url2HashMapBackupFile = new File(this.dataDirectory, "index.txt.bak");

        if (url2HashMapFile.exists()) {
            url2HashMapFile.renameTo(url2HashMapBackupFile);
        }//from   w w w  .ja  va2  s. c om

        OutputStream output = null;
        try {
            output = new FileOutputStream(url2HashMapFile);

            StringBuilder sb = new StringBuilder();
            for (String k : this.uri2HashCodes.keySet()) {

                sb.append(k).append("=");
                Collection<String> vs = this.uri2HashCodes.get(k);
                if (!vs.isEmpty()) {
                    for (String v : vs) {
                        sb.append(v).append(",");
                    }
                    sb.delete(sb.length() - 1, sb.length());
                    sb.append("\n");
                }

            }

            IOUtils.write(sb.toString(), output);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(output);
        }
    }
}

From source file:com.inkubator.hrm.service.impl.BioSertifikasiServiceImpl.java

@Override
@Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void update(BioSertifikasi entity, UploadedFile documentFile) throws Exception {
    BioSertifikasi bioSertifikasi = bioSertifikasiDao.getEntiyByPK(entity.getId());
    String uploadPath = bioSertifikasi.getUploadPath();

    if (documentFile != null) {
        //remove old file if exist
        if (null != bioSertifikasi.getUploadPath()) {
            File oldFile = new File(bioSertifikasi.getUploadPath());
            oldFile.delete();//from   w  w  w. ja va2s  .co m
        }

        //added new file
        uploadPath = getUploadPath(bioSertifikasi.getId(), documentFile);
        facesIO.transferFile(documentFile);
        File file = new File(facesIO.getPathUpload() + documentFile.getFileName());
        file.renameTo(new File(uploadPath));
    }

    //BioData bioData = bioDataDao.getEntiyByPK(entity.getBioData().getId());
    EducationNonFormal educationNonFormal = educationNonFormalDao
            .getEntiyByPK(entity.getEducationNonFormal().getId());
    OccupationType occupationType = occupationTypeDao.getEntiyByPK(entity.getOccupationType().getId());

    bioSertifikasi.setEducationNonFormal(educationNonFormal);
    //bioSertifikasi.setBioData(bioData);
    bioSertifikasi.setOccupationType(occupationType);
    bioSertifikasi.setNamaSertifikasi(entity.getNamaSertifikasi());
    bioSertifikasi.setDocumentTitle(entity.getDocumentTitle());
    bioSertifikasi.setNomorDokumen(entity.getNomorDokumen());
    bioSertifikasi.setUploadPath(uploadPath);
    bioSertifikasi.setUpdatedBy(UserInfoUtil.getUserName());
    bioSertifikasi.setUpdatedOn(new Date());

    bioSertifikasiDao.update(bioSertifikasi);
}