File renameTo - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for File renameTo method.

This code example shows how to use the following methods:canWrite, equals, exists, renameTo, toString
        // Is this a rollover file? 
        String filename=file.getName();
        int i=filename.toLowerCase().indexOf(YYYY_MM_DD);
        if (i>=0)
        { 
            file=new File(dir,
                          filename.substring(0,i)+
                          _fileDateFormat.format(now)+
                          filename.substring(i+YYYY_MM_DD.length()));
        } 
             
        if (file.exists()&&!file.canWrite())
            throw new IOException("Cannot write log file "+file);
 
        // Do we need to change the output stream? 
        if (out==null || !file.equals(_file))
        { 
            // Yep 
            _file=file;
            if (!_append && file.exists())

9
CodeRank
This code example shows how to use the following methods:delete, deleteOnExit, exists, renameTo
            StreamUtils.safeClose(in);
        } 
    } 
 
    static void rename(File file, File to) throws IOException {
        if (!file.renameTo(to) && file.exists()) {
            copyFile(file, to);
        } 
    } 
 
    static void moveTempFileToMain(File tempFileName, File fileName) throws ConfigurationPersistenceException {
        //Rename the temp file written to the target file 
        try { 
            FilePersistenceUtils.rename(tempFileName, fileName);
            //Only delete the temp file if all went well, to give people the chance to manually recover it if something really weird happened 
            deleteFile(tempFileName);
        } catch (Exception e) {
            throw MESSAGES.failedToRenameTempFile(e, tempFileName, fileName);
        } 
    } 

8
CodeRank
This code example shows how to use the following methods:exists, renameTo
        migrate(saveDir, "schematics"); 
        migrate("drawings", "draw.js images"); 
    } 
 
    private void migrate(String file, String name) {
        File fromDir = new File(".", file);
        File toDir = new File(getWorkingDirectory(), file);
        if (fromDir.exists() & !toDir.exists()) {
            if (fromDir.renameTo(toDir)) {
                plugin.getLogger().info("Migrated " + name + " folder '" + file +
                        "' from server root to plugin data folder."); 
            } else { 
                plugin.getLogger().warning("Error while migrating " + name + " folder!");
            } 
        } 
    } 
 
    @Override 
    public File getWorkingDirectory() {
        return plugin.getDataFolder();
8
CodeRank
This code example shows how to use the following methods:exists, getParentFile, renameTo
 
    File libraryFolder = new File(_destinationFolder, "Library");
    if (libraryFolder.exists()) {
      throw new IOException("The folder '" + libraryFolder + "' already exists.");
    } 
    if (!_systemLibrariesFolder.renameTo(libraryFolder)) {
      throw new IOException("Failed to move '" + _systemLibrariesFolder + "' to '" + libraryFolder + "'.");
    } 
    _systemLibrariesFolder = libraryFolder;
 
    if (!_systemLibrariesFolder.getParentFile().delete()) {
      throw new IOException("Failed to delete '" + _systemLibrariesFolder.getParentFile() + ".");
    } 
    System.out.println("Done");
  } 
} 
8
CodeRank
This code example shows how to use the following methods:exists, getAbsolutePath, renameTo
 
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                } 
            } else { 
                File oldPrefFile = new File(prefsDirectory(context), oldPrefsFileName
                        + FILE_EXTENSION);
                if (oldPrefFile.exists()) {
                    // Commit any changes left 
                    SharedPreferences.Editor prefs = context.getSharedPreferences(oldPrefsFileName,
                            MODE_PRIVATE).edit();
                    if (prefs != null) {
                        prefs.commit();
                        prefs = null;
                    } 
 
                    isRenamed = oldPrefFile.renameTo(newPrefFile);
 
                    Log.v(TAG, "The prefs file '" + oldPrefFile.getAbsolutePath() + "' was "
                            + (isRenamed ? "" : "not ") + " renamed");

7
CodeRank
This code example shows how to use the following methods:getPath, isFile, mkdirs, renameTo
 
    public static File deploy(File file, String outPath) {
 
        String fileName = FileUtils.getFilePrefix(file);
 
        File dir = new File(FileUtils.appendFileSeparator(outPath) + fileName);
 
        if (dir.isFile()) {
            dir.renameTo(new File(dir.getPath() + ".backup"));
        } else { 
            dir.mkdirs();
        } 
 
        return dir;
    } 
 
    public static Lock getRunningQueueLock() {
        return queueLock;
    } 
 

7
CodeRank
This code example shows how to use the following methods:exists, renameTo
import simpleserver.config.xml.Config; 
 
public class LegacyPermissionConfig { 
 
  public static Config load() {
    File file = new File("simpleserver", "permissions.xml");
 
    if (!file.exists()) {
      return null; 
    } 
 
    XMLReader xml;
    try { 
      xml = XMLReaderFactory.createXMLReader();
    } catch (Exception e) {
      // TODO Auto-generated catch block 
      e.printStackTrace();
      return null; 
    } 
 

7
CodeRank
This code example shows how to use the following methods:exists, renameTo
      e.printStackTrace();
    } 
 
    if (!success) {
      System.out.println("Failed to save " + getFilename() + "!");
    } 
  } 
 
  @Override 
  public void load() { 
    boolean success = false;
 
    File inFile = getFile();
    try { 
      BufferedReader reader = new BufferedReader(new UnicodeReader(new FileInputStream(inFile), "UTF-8"));
      try { 
        String line;
        while ((line = reader.readLine()) != null) {
          if (!line.startsWith("#")) {
            loadLine(line);
7
CodeRank
This code example shows how to use the following methods:getName, renameTo
		p.reloadConfig();
	} 
	 
	public boolean moveFile(File ff){
		SurvivalGames.$("Moving outdated config file. "+f.getName());
		String name = ff.getName();
		File ff2 = new File(SurvivalGames.getPluginDataFolder(), getNextName(name, 0));
		return ff.renameTo(ff2);
	} 
	 
	public String getNextName(String name, int n){
		File ff = new File(SurvivalGames.getPluginDataFolder(), name+".old"+n);
		if(!ff.exists()){
			return ff.getName();
		} 
		else{ 
			return getNextName(name, n+1);
		} 
	} 
	 

7
CodeRank
This code example shows how to use the following methods:canWrite, equals, exists, getName, getParent
        throws IOException 
    { 
        // Check directory 
        File file = new File(_filename);
        _filename=file.getCanonicalPath();
        file=new File(_filename);
        File dir= new File(file.getParent());
        if (!dir.isDirectory() || !dir.canWrite())
            throw new IOException("Cannot write log directory "+dir);
             
        Date now=new Date();
         
        // Is this a rollover file? 
        String filename=file.getName();
        int i=filename.toLowerCase().indexOf(YYYY_MM_DD);
        if (i>=0)
        { 
            file=new File(dir,
                          filename.substring(0,i)+
                          _fileDateFormat.format(now)+

7
CodeRank

Related File renameTo Questions & Answers:

renameTo Method Overview
Renames the file denoted by this abstract pathname.
See Code Examples for other Java 8 File Methods: