Example usage for java.io FileSystem rename

List of usage examples for java.io FileSystem rename

Introduction

In this page you can find the example usage for java.io FileSystem rename.

Prototype

public abstract boolean rename(File f1, File f2);

Source Link

Document

Rename the file or directory denoted by the first abstract pathname to the second abstract pathname, returning true if and only if the operation succeeds.

Usage

From source file:org.apache.hadoop.hbase.io.MapFile.java

/** Renames an existing map directory. 
 * @param fs //from www . j  av a  2s . c o m
 * @param oldName 
 * @param newName 
 * @throws IOException
 */
public static void rename(FileSystem fs, String oldName, String newName) throws IOException {
    Path oldDir = new Path(oldName);
    Path newDir = new Path(newName);
    if (!fs.rename(oldDir, newDir)) {
        throw new IOException("Could not rename " + oldDir + " to " + newDir);
    }
}

From source file:org.apache.jxtadoop.fs.FsShell.java

/**
 * Move files that match the file pattern <i>srcf</i>
 * to a destination file.//  w w  w .  j  a  v  a2s .  c o  m
 * When moving mutiple files, the destination must be a directory. 
 * Otherwise, IOException is thrown.
 * @param srcf a file pattern specifying source files
 * @param dstf a destination local file/directory 
 * @throws IOException  
 * @see org.apache.jxtadoop.fs.FileSystem#globStatus(Path)
 */
void rename(String srcf, String dstf) throws IOException {
    Path srcPath = new Path(srcf);
    Path dstPath = new Path(dstf);
    FileSystem srcFs = srcPath.getFileSystem(getConf());
    FileSystem dstFs = dstPath.getFileSystem(getConf());
    URI srcURI = srcFs.getUri();
    URI dstURI = dstFs.getUri();
    if (srcURI.compareTo(dstURI) != 0) {
        throw new IOException("src and destination filesystems do not match.");
    }
    Path[] srcs = FileUtil.stat2Paths(srcFs.globStatus(srcPath), srcPath);
    Path dst = new Path(dstf);
    if (srcs.length > 1 && !srcFs.isDirectory(dst)) {
        throw new IOException("When moving multiple files, " + "destination should be a directory.");
    }
    for (int i = 0; i < srcs.length; i++) {
        if (!srcFs.rename(srcs[i], dst)) {
            FileStatus srcFstatus = null;
            FileStatus dstFstatus = null;
            try {
                srcFstatus = srcFs.getFileStatus(srcs[i]);
            } catch (FileNotFoundException e) {
                throw new FileNotFoundException(srcs[i] + ": No such file or directory");
            }
            try {
                dstFstatus = dstFs.getFileStatus(dst);
            } catch (IOException e) {
            }
            if ((srcFstatus != null) && (dstFstatus != null)) {
                if (srcFstatus.isDir() && !dstFstatus.isDir()) {
                    throw new IOException(
                            "cannot overwrite non directory " + dst + " with directory " + srcs[i]);
                }
            }
            throw new IOException("Failed to rename " + srcs[i] + " to " + dst);
        }
    }
}

From source file:org.apache.nutch.crawl.CrawlDb.java

public static void install(JobConf job, Path crawlDb) throws IOException {
    Path newCrawlDb = FileOutputFormat.getOutputPath(job);
    FileSystem fs = new JobClient(job).getFs();
    Path old = new Path(crawlDb, "old");
    Path current = new Path(crawlDb, CURRENT_NAME);
    if (fs.exists(current)) {
        if (fs.exists(old))
            fs.delete(old, true);//from   w  w w  .  j  a  v a 2 s.  co  m
        fs.rename(current, old);
    }
    fs.mkdirs(crawlDb);
    fs.rename(newCrawlDb, current);
    if (fs.exists(old))
        fs.delete(old, true);
    Path lock = new Path(crawlDb, LOCK_NAME);
    LockUtil.removeLockFile(fs, lock);
}