Java Path File Move mio moveFile(String workspacePathOld, String workspacePathNew)

Here you can find the source of moveFile(String workspacePathOld, String workspacePathNew)

Description

move File

License

Apache License

Declaration

public static void moveFile(String workspacePathOld, String workspacePathNew)
            throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void moveFile(String workspacePathOld, String workspacePathNew)
            throws FileNotFoundException, IOException {
        createFoldersIfNecessary(workspacePathNew);
        Path pathOld = FileSystems.getDefault().getPath(workspacePathOld);
        Path pathNew = FileSystems.getDefault().getPath(workspacePathNew);
        Files.move(pathOld, pathNew);
    }/* ww w . ja va  2  s.co m*/

    public static void createFoldersIfNecessary(String workspacePath) {
        int lastIndexOf = workspacePath.lastIndexOf(File.separator);
        if (lastIndexOf > 0) {
            String directory = workspacePath.substring(0, lastIndexOf);
            createFolder(directory);
        }
    }

    public static boolean createFolder(String workspacePath) {
        File folder = new File(workspacePath);
        if (!folder.exists()) {
            return folder.mkdirs();
        }
        return true;
    }

    public static boolean exists(String repositoryName) {
        if (repositoryName == null || "".equals(repositoryName)) {
            return false;
        }
        Path path;
        try {
            path = FileSystems.getDefault().getPath(repositoryName);
        } catch (java.nio.file.InvalidPathException e) {
            return false;
        }
        return Files.exists(path);
    }
}

Related

  1. moveFile(Path from, Path to)
  2. moveFile(Path source, Path target)
  3. moveFile(Path source, Path target, boolean prompt)
  4. moveFile(Path tempPath, Path defPath)
  5. moveFile(String pathBefore, String pathAfter)