Path Create Move/Delete File - Java File Path IO

Java examples for File Path IO:Path

Description

Path Create Move/Delete File

Demo Code



import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {

        try {/* www  .  j  ava2s  .c  o  m*/
            Properties props = System.getProperties();
            String homePath = props.get("user.home").toString();
            Path sampleFile = FileSystems.getDefault().getPath(homePath + "/test1.txt");
            Files.deleteIfExists(sampleFile);
            Files.createFile(sampleFile); // create an empty file
            Files.copy(sampleFile, FileSystems.getDefault().getPath(homePath + "/test2.txt"), StandardCopyOption.COPY_ATTRIBUTES.REPLACE_EXISTING);
            // Creating a link
            Path dir = FileSystems.getDefault().getPath(homePath + "/a/symlink");
            Files.deleteIfExists(dir);
            Files.createSymbolicLink(dir, sampleFile);
        } catch (IOException ex) {
            Logger.getLogger(PathCreateMoveDeleteFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Related Tutorials