Example usage for java.nio.file Files move

List of usage examples for java.nio.file Files move

Introduction

In this page you can find the example usage for java.nio.file Files move.

Prototype

public static Path move(Path source, Path target, CopyOption... options) throws IOException 

Source Link

Document

Move or rename a file to a target file.

Usage

From source file:lu.fisch.unimozer.Diagram.java

public void moveFile(MyClass mc, String oldPName) {
    //System.out.println(oldPName+" "+mc.getPackagename());
    // <default>
    String dir = directoryName + System.getProperty("file.separator") + "src"
            + System.getProperty("file.separator");
    String p = mc.getPackagename().replaceAll("\\.", "\\" + System.getProperty("file.separator"));
    String to = dir + p + System.getProperty("file.separator") + mc.getShortName() + ".java";
    if (mc.getPackagename().equals("<default>"))
        to = dir + mc.getShortName() + ".java";

    p = oldPName.replaceAll("\\.", "\\" + System.getProperty("file.separator"));
    String from = dir + p + System.getProperty("file.separator") + mc.getShortName() + ".java";
    if (oldPName.equals("<default>"))
        from = dir + mc.getShortName() + ".java";

    File fromF = new File(from);
    File toF = new File(to);
    if (fromF.exists())
        try {//w  w  w.  j av a  2  s  .com
            String filePath = to.substring(0, to.lastIndexOf(System.getProperty("file.separator")));
            //System.out.println(filePath);
            Files.createDirectories((new File(filePath)).toPath());
            Files.move(fromF.toPath(), toF.toPath(), REPLACE_EXISTING);
            // we need to save the file as well, because it must contain the right package information!
            FileOutputStream fos;
            Writer out;
            fos = new FileOutputStream(to);
            out = new OutputStreamWriter(fos, Unimozer.FILE_ENCODING);
            String code;
            if (allowEdit == true)
                code = mc.getJavaCode();
            else
                code = mc.getContent().getText();
            out.write(code);
            out.close();
        } catch (IOException ex) {
            System.err.println(
                    "Ups, you modified the package information of a class, but Unimozer didn't manage to move the corresponding file to theright place!");
            ex.printStackTrace();
        }
}