Example usage for java.io File toPath

List of usage examples for java.io File toPath

Introduction

In this page you can find the example usage for java.io File toPath.

Prototype

public Path toPath() 

Source Link

Document

Returns a Path java.nio.file.Path object constructed from this abstract path.

Usage

From source file:com.github.riccardove.easyjasub.EasyJaSubInputFromCommand.java

private static String probeContentType(File file) {
    try {//  w w  w.j av  a  2 s  .c o m
        return Files.probeContentType(file.toPath());
    } catch (IOException ex) {
        return null;
    }
}

From source file:com.kegare.caveworld.world.WorldProviderCaveworld.java

public static void regenerate(final boolean backup) {
    final File dir = getDimDir();
    final String name = dir.getName().substring(4);
    final MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    Set<EntityPlayerMP> target = Sets.newHashSet();

    for (Object obj : server.getConfigurationManager().playerEntityList.toArray()) {
        if (obj != null && ((EntityPlayerMP) obj).dimension == CaveworldAPI.getDimension()) {
            target.add(CaveUtils.respawnPlayer((EntityPlayerMP) obj, 0));
        }/*www  .  j a  va2 s. c  o  m*/
    }

    boolean result = new ForkJoinPool().invoke(new RecursiveTask<Boolean>() {
        @Override
        protected Boolean compute() {
            IChatComponent component;

            try {
                component = new ChatComponentText(
                        StatCollector.translateToLocalFormatted("caveworld.regenerate.regenerating", name));
                component.getChatStyle().setColor(EnumChatFormatting.GRAY).setItalic(true);
                server.getConfigurationManager().sendChatMsg(component);

                if (server.isSinglePlayer()) {
                    Caveworld.network.sendToAll(new RegenerateMessage(backup));
                }

                Caveworld.network.sendToAll(new RegenerateMessage.ProgressNotify(0));

                CaveBlocks.caveworld_portal.portalDisabled = true;

                int dim = CaveworldAPI.getDimension();
                WorldServer world = DimensionManager.getWorld(dim);

                if (world != null) {
                    world.saveAllChunks(true, null);
                    world.flush();

                    MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(world));

                    DimensionManager.setWorld(dim, null);
                }

                if (dir != null) {
                    if (backup) {
                        File parent = dir.getParentFile();
                        final Pattern pattern = Pattern.compile("^" + dir.getName() + "_bak-..*\\.zip$");
                        File[] files = parent.listFiles(new FilenameFilter() {
                            @Override
                            public boolean accept(File dir, String name) {
                                return pattern.matcher(name).matches();
                            }
                        });

                        if (files != null && files.length >= 5) {
                            Arrays.sort(files, new Comparator<File>() {
                                @Override
                                public int compare(File o1, File o2) {
                                    int i = CaveUtils.compareWithNull(o1, o2);

                                    if (i == 0 && o1 != null && o2 != null) {
                                        try {
                                            i = Files.getLastModifiedTime(o1.toPath())
                                                    .compareTo(Files.getLastModifiedTime(o2.toPath()));
                                        } catch (IOException e) {
                                        }
                                    }

                                    return i;
                                }
                            });

                            FileUtils.forceDelete(files[0]);
                        }

                        Calendar calendar = Calendar.getInstance();
                        String year = Integer.toString(calendar.get(Calendar.YEAR));
                        String month = String.format("%02d", calendar.get(Calendar.MONTH) + 1);
                        String day = String.format("%02d", calendar.get(Calendar.DATE));
                        String hour = String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY));
                        String minute = String.format("%02d", calendar.get(Calendar.MINUTE));
                        String second = String.format("%02d", calendar.get(Calendar.SECOND));
                        File bak = new File(parent,
                                dir.getName() + "_bak-" + Joiner.on("").join(year, month, day) + "-"
                                        + Joiner.on("").join(hour, minute, second) + ".zip");

                        if (bak.exists()) {
                            FileUtils.deleteQuietly(bak);
                        }

                        component = new ChatComponentText(StatCollector
                                .translateToLocalFormatted("caveworld.regenerate.backingup", name));
                        component.getChatStyle().setColor(EnumChatFormatting.GRAY).setItalic(true);
                        server.getConfigurationManager().sendChatMsg(component);

                        Caveworld.network.sendToAll(new RegenerateMessage.ProgressNotify(1));

                        if (CaveUtils.archiveDirZip(dir, bak)) {
                            ClickEvent click = new ClickEvent(ClickEvent.Action.OPEN_FILE,
                                    FilenameUtils.normalize(bak.getParentFile().getPath()));

                            component = new ChatComponentText(StatCollector
                                    .translateToLocalFormatted("caveworld.regenerate.backedup", name));
                            component.getChatStyle().setColor(EnumChatFormatting.GRAY).setItalic(true)
                                    .setChatClickEvent(click);
                            server.getConfigurationManager().sendChatMsg(component);
                        } else {
                            component = new ChatComponentText(StatCollector
                                    .translateToLocalFormatted("caveworld.regenerate.backup.failed", name));
                            component.getChatStyle().setColor(EnumChatFormatting.RED).setItalic(true);
                            server.getConfigurationManager().sendChatMsg(component);
                        }
                    }

                    FileUtils.deleteDirectory(dir);
                }

                if (DimensionManager.shouldLoadSpawn(dim)) {
                    DimensionManager.initDimension(dim);

                    world = DimensionManager.getWorld(dim);

                    if (world != null) {
                        world.saveAllChunks(true, null);
                        world.flush();
                    }
                }

                CaveBlocks.caveworld_portal.portalDisabled = false;

                component = new ChatComponentText(
                        StatCollector.translateToLocalFormatted("caveworld.regenerate.regenerated", name));
                component.getChatStyle().setColor(EnumChatFormatting.GRAY).setItalic(true);
                server.getConfigurationManager().sendChatMsg(component);

                Caveworld.network.sendToAll(new RegenerateMessage.ProgressNotify(2));

                return true;
            } catch (Exception e) {
                component = new ChatComponentText(
                        StatCollector.translateToLocalFormatted("caveworld.regenerate.failed", name));
                component.getChatStyle().setColor(EnumChatFormatting.RED).setItalic(true);
                server.getConfigurationManager().sendChatMsg(component);

                Caveworld.network.sendToAll(new RegenerateMessage.ProgressNotify(3));

                CaveLog.log(Level.ERROR, e, component.getUnformattedText());
            }

            return false;
        }
    });

    if (result && (Config.hardcore || Config.caveborn)) {
        for (EntityPlayerMP player : target) {
            if (!CaveworldAPI.isEntityInCaveworld(player)) {
                CaveUtils.forceTeleport(player, CaveworldAPI.getDimension());
            }
        }
    }
}

From source file:com.roche.sequencing.bioinformatics.common.utils.FileUtil.java

public static byte[] readFileAsBytes(File file) throws IOException {
    return Files.readAllBytes(file.toPath());
}

From source file:it.baeyens.arduino.managers.Manager.java

/**
 * This method takes a json boards file url and downloads it and parses it
 * for usage in the boards manager//from w w w.  j  ava2s  .  c o m
 * 
 * @param url
 *            the url of the file to download and load
 * @param forceDownload
 *            set true if you want to download the file even if it is
 *            already available locally
 */
static private void loadPackageIndex(String url, boolean forceDownload) {
    File packageFile = getLocalFileName(url);
    if (packageFile == null) {
        return;
    }
    if (!packageFile.exists() || forceDownload) {
        packageFile.getParentFile().mkdirs();
        try {
            Files.copy(new URL(url.trim()).openStream(), packageFile.toPath(),
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            Common.log(new Status(IStatus.ERROR, Activator.getId(), "Unable to download " + url, e)); //$NON-NLS-1$
        }
    }
    if (packageFile.exists()) {
        try (Reader reader = new FileReader(packageFile)) {
            PackageIndex index = new Gson().fromJson(reader, PackageIndex.class);
            index.setOwners(null);
            packageIndices.add(index);
        } catch (Exception e) {
            Common.log(new Status(IStatus.ERROR, Activator.getId(),
                    "Unable to parse " + packageFile.getAbsolutePath(), e)); //$NON-NLS-1$
            packageFile.delete();// Delete the file so it stops damaging
        }
    }
}

From source file:model.fileloader.FileLoader.java

protected List<String> getFileContent(String filename) throws IOException {
    if (!StringUtils.hasText(filename))
        throw new IOException();

    String workDir = System.getProperty("user.dir");
    File file = new File(workDir, filename);
    Path path = file.toPath();
    return Files.readAllLines(path);
}

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 w w  . j  a  va2s  . com
            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:org.bonitasoft.web.designer.i18n.LanguagePackTest.java

@Test
public void should_convert_translation_into_json() throws Exception {
    File poFile = directory.newFile();
    write(poFile.toPath(), readResource("/i18n/simple.po"));

    assertThat(new String(languagePackFactory.create(poFile).toJson()))
            .isEqualTo("{\"francais\":{\"A page\":\"Une page\"}}");
}

From source file:org.bonitasoft.web.designer.i18n.LanguagePackTest.java

@Test
public void should_convert_plural_translations_into_json() throws Exception {
    File poFile = directory.newFile();
    write(poFile.toPath(), readResource("/i18n/plural.po"));

    assertThat(new String(languagePackFactory.create(poFile).toJson()))
            .isEqualTo("{\"francais\":{\"A page\":[\"Une page\",\"Des pages\"]}}");
}

From source file:org.bonitasoft.web.designer.i18n.LanguagePackTest.java

@Test(expected = RuntimeException.class)
public void should_throw_a_runtime_exception_if_the_po_file_does_not_contains_the_language() throws Exception {
    File poFile = directory.newFile();
    write(poFile.toPath(),
            new String(readResource("/i18n/simple.po")).replace("Language: francais", "").getBytes());

    assertThat(new String(languagePackFactory.create(poFile).toJson()))
            .isEqualTo("{\"francais\":{\"A page\":[\"Une page\",\"Des pages\"]}}");
}

From source file:fr.duminy.components.swing.form.FileTypeMapper.java

@Override
public final void setValue(@Nonnull JPath jPath, @Nullable File value) {
    jPath.setPath((value == null) ? null : value.toPath());
}