Example usage for java.nio.file StandardCopyOption REPLACE_EXISTING

List of usage examples for java.nio.file StandardCopyOption REPLACE_EXISTING

Introduction

In this page you can find the example usage for java.nio.file StandardCopyOption REPLACE_EXISTING.

Prototype

StandardCopyOption REPLACE_EXISTING

To view the source code for java.nio.file StandardCopyOption REPLACE_EXISTING.

Click Source Link

Document

Replace an existing file if it exists.

Usage

From source file:pm.filemanager.operations.FileOperations.java

public static void moveFolder(String source, String destination) throws IOException {

    Path sourcePath = Paths.get(source);
    Path targetPath = Paths.get(destination, source);
    try {/*from ww w . ja v a 2s . com*/
        Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        System.out.println("You must select a valid directory to move the folder into!");
    }
}

From source file:ninja.uploads.DiskFileItemProvider.java

@Override
public FileItem create(FileItemStream item) {

    File tmpFile = null;//  w  w  w.  j a  v a2s  .c o  m

    // do copy
    try (InputStream is = item.openStream()) {
        tmpFile = File.createTempFile("nju", null, tmpFolder);
        Files.copy(is, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new RuntimeException("Failed to create temporary uploaded file on disk", e);
    }

    // return
    final String name = item.getName();
    final File file = tmpFile;
    final String contentType = item.getContentType();
    final FileItemHeaders headers = item.getHeaders();

    return new FileItem() {
        @Override
        public String getFileName() {
            return name;
        }

        @Override
        public InputStream getInputStream() {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                throw new RuntimeException("Failed to read temporary uploaded file from disk", e);
            }
        }

        @Override
        public File getFile() {
            return file;
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public FileItemHeaders getHeaders() {
            return headers;
        }

        @Override
        public void cleanup() {
            // try to delete temporary file, silently fail on error
            try {
                file.delete();
            } catch (Exception e) {
            }
        }
    };

}

From source file:resources.AnalysisResource.java

@POST
@Produces(MediaType.APPLICATION_JSON)/*from  w w w  .  ja v  a  2  s  .c  om*/
public Response analyze(@FormParam("image") String image, @FormParam("filter") String filter,
        @FormParam("type") String type) {
    try {
        if (filter == null || filter.isEmpty()) {
            filter = "Default";
        }
        System.out.println(image);
        String fileName = UUID.randomUUID().toString();
        java.nio.file.Path path = FileSystems.getDefault().getPath("tmp", fileName);
        if (!path.toFile().getParentFile().exists()) {
            path.toFile().mkdirs();
        }
        path.toFile().createNewFile();

        BufferedImage bufferedImage = null;
        if (image.startsWith("data:image")) {
            String base64Image = image.split(",")[1];
            byte[] decoded = Base64.decodeBase64(base64Image.getBytes());
            bufferedImage = ImageIO.read(new ByteArrayInputStream(decoded));
        } else {
            URL website = new URL(image);
            Files.copy(website.openStream(), path, StandardCopyOption.REPLACE_EXISTING);
            bufferedImage = ImageIO.read(new FileInputStream(path.toFile()));
        }

        ImagePlus imagePlus = new ImagePlus("theTitle", bufferedImage);

        Measurement measurementModel = new Measurement();
        List<String> selectedMeasurements = measurementModel.getMeasurementList();
        ApplicationMain applicationMain = new ApplicationMain(
                selectedMeasurements.toArray(new String[selectedMeasurements.size()]), filter, imagePlus,
                fileName);

        Result result = null;
        if (type == null || type.isEmpty() || type.equals("Analyze")) {
            result = applicationMain.analyseImage();
        } else if (type.equals("Count")) {
            result = applicationMain.countParticles();
        } else {
            return Response.status(Response.Status.BAD_REQUEST).entity("bad type provided").build();
        }

        Logger.getLogger(AnalysisResource.class.getName()).log(Level.INFO, "Deleting tmp file:{0}", fileName);
        path.toFile().delete();
        System.out.println("Number of ParticleResults:" + result.getParticleResults().size());
        return Response.ok(result.getParticleResults()).build();
    } catch (MalformedURLException ex) {
        Logger.getLogger(AnalysisResource.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.BAD_REQUEST).entity("bad uri provided").build();
    } catch (IOException ex) {
        Logger.getLogger(AnalysisResource.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

From source file:ms.dew.devops.kernel.plugin.appkind.frontend_node.FrontendNodePrepareFlow.java

protected void postPrepareBuild(FinalProjectConfig config, String flowBasePath) throws IOException {
    FileUtils.deleteDirectory(new File(flowBasePath + "dist"));
    Files.move(Paths.get(config.getDirectory() + "dist"), Paths.get(flowBasePath + "dist"),
            StandardCopyOption.REPLACE_EXISTING);
}

From source file:org.phenotips.variantstore.input.vcf.VCFManager.java

@Override
public void addIndividual(String id, Path path) throws InputException {
    try {/*from w  w w .j  a  v a  2  s  . c  o  m*/
        Files.copy(path, this.getIndividual(id), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new InputException("Error copying VCF for storage.", e);
    }
}

From source file:com.excelsiorjet.api.util.Utils.java

public static void copyFile(Path source, Path target) throws IOException {
    if (!target.toFile().exists()) {
        Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
    } else if (source.toFile().lastModified() != target.toFile().lastModified()) {
        //copy only files that were changed
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
    }//from   ww  w .  ja va  2  s  .  co  m

}

From source file:org.wte4j.examples.showcase.server.hsql.ShowCaseDbInitializer.java

void createDateBaseFiles(Path dataBaseLocation, boolean overide) {
    Path hsqlScript = dataBaseLocation.resolve("wte4j-showcase.script");
    if (overide || !Files.exists(hsqlScript)) {
        try {/*  w ww  .  j  a va 2s  .  c  o m*/
            if (!Files.exists(dataBaseLocation)) {
                Files.createDirectories(dataBaseLocation);
            }
            Resource[] resources = resourceLoader.getResources("classpath:/db/*");
            for (Resource resource : resources) {
                Path filePath = dataBaseLocation.resolve(resource.getFilename());
                File source = resource.getFile();
                Files.copy(source.toPath(), filePath, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("can not copy database files", e);
        }
    }
}

From source file:mx.eersya.beans.CreateItemBean.java

public void savePicture() {
    try {//from w w w  . j  av a 2 s .c  om
        Path path = Paths.get("/home/eersya/Projects/W/");
        String filename = FilenameUtils.getBaseName(picture.getFileName());
        String extension = FilenameUtils.getExtension(picture.getFileName());
        Path file = Files.createTempFile(path, filename, "." + extension);
        picturePath = file.toString();
        try (InputStream in = picture.getInputstream()) {
            Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException exa) {
            System.err.println(exa);
        }
        picturePath = file.toString();
        System.out.println("Picture:" + picturePath);
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:org.phenotips.variantstore.input.exomiser6.tsv.Exomiser6TSVManager.java

@Override
public void addIndividual(String id, Path path) throws InputException {
    try {/*from  w w  w  . jav a2s . c om*/
        Files.copy(path, this.getIndividual(id), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new InputException("Error copying TSV for storage.", e);
    }
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromFile() throws IOException {
    try (InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) {
        File file = folder.newFile();
        Files.copy(stream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        PDImageXObject ximage = JPEGFactory.createFromFile(file);
        validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());

        doWritePDF(new PDDocument(), ximage, testResultsDir, "jpegrgbstream.pdf");
        checkJpegStream(testResultsDir, "jpegrgbstream.pdf",
                JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
    }// w  w  w .ja va  2 s. c  o m
}