Example usage for java.nio.file Files readAllBytes

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

Introduction

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

Prototype

public static byte[] readAllBytes(Path path) throws IOException 

Source Link

Document

Reads all the bytes from a file.

Usage

From source file:io.github.casnix.spawnerdropper.SpawnerStack.java

public static boolean TakeSpawnerOutOfService(String spawnerType) {
    // Read ./SpawnerDropper.SpawnerStack.json into a string

    // get the value of JSON->{spawnerType} and subtract 1 to it unless it is 0.
    // If it's zero, return false

    // Write file back to disk
    try {//from  w  ww.ja v  a  2s.c o m
        // Read entire ./SpawnerDropper.SpawnerStack.json into a string
        String spawnerStack = new String(
                Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerStack.json")));

        // get the value of JSON->{spawnerType}
        JSONParser parser = new JSONParser();

        Object obj = parser.parse(spawnerStack);

        JSONObject jsonObj = (JSONObject) obj;

        long numberInService = (Long) jsonObj.get(spawnerType);

        if (numberInService <= 0) {
            return false;
        } else {
            //            System.out.println("[SD DBG MSG] TSOOS numberInServer("+numberInService+")");

            numberInService -= 1;
            jsonObj.put(spawnerType, new Long(numberInService));

            FileWriter file = new FileWriter("./plugins/SpawnerDropper/SpawnerStack.json");
            file.write(jsonObj.toJSONString());
            file.flush();
            file.close();

            return true;
        }
    } catch (ParseException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in TakeSpawnerOutOfService(String)");
        e.printStackTrace();

        return false;
    } catch (FileNotFoundException e) {
        Bukkit.getLogger()
                .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerStack.json");
        e.printStackTrace();

        return false;
    } catch (IOException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in TakeSpawnerOutOfService(String)");
        e.printStackTrace();

        return false;
    }
}

From source file:io.fabric8.vertx.maven.plugin.ConfigConversionUtilTest.java

@Test
public void convertSimpleYamlToJson() throws Exception {
    Path yamlFile = Paths.get(this.getClass().getResource("/testconfig.yaml").toURI());
    Path jsonFilePath = Files.createTempFile("testconfig", ".json");
    assertNotNull(yamlFile);/*from ww  w  . ja v a  2  s  .  co m*/
    assertTrue(yamlFile.toFile().isFile());
    assertTrue(yamlFile.toFile().exists());
    ConfigConverterUtil.convertYamlToJson(yamlFile, jsonFilePath);
    assertNotNull(jsonFilePath);
    String jsonDoc = new String(Files.readAllBytes(jsonFilePath));
    assertNotNull(jsonDoc);
    JSONObject jsonMap = new JSONObject(jsonDoc);
    assertNotNull(jsonMap);
    assertEquals(jsonMap.get("http.port"), 8080);

}

From source file:edu.usf.cutr.obascs.io.FileUtil.java

public static String readFile(String path) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, "UTF-8");
}

From source file:com.simiacryptus.util.io.IOUtil.java

/**
 * Read json t.//from  ww w .  j av  a2  s . c om
 *
 * @param <T>  the type parameter
 * @param file the file
 * @return the t
 */
public static <T> T readJson(File file) {
    try {
        return objectMapper.readValue(new String(Files.readAllBytes(file.toPath())), new TypeReference<T>() {
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.teamgrit.grit.util.mailer.EncryptorDecryptor.java

/**
 * Instantiates a new {@link EncryptorDecryptor}.
 * /*from  w  w  w . j  a  v  a 2  s  .co  m*/
 * @throws IOException
 *             if the secretkey can not be read
 */
public EncryptorDecryptor() throws IOException {
    m_key = Files.readAllBytes(SECRETKEYDESTINATION);
}

From source file:com.sonoport.freesound.response.mapping.MapperTest.java

/**
 * Read a given JSON file into a {@link JSONObject}. Files should be stored under <code>/src/test/resources</code>.
 *
 * @param resourcePath The path to the file
 * @return {@link JSONObject} representation of the file contents
 *
 * @throws Exception Any exceptions thrown when reading the file
 *//*from   w  ww. java 2s.  com*/
protected JSONObject readJSONFile(final String resourcePath) throws Exception {
    final URI soundFileURI = getClass().getResource(resourcePath).toURI();
    final Path filePath = Paths.get(soundFileURI);
    final String jsonString = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);

    return new JSONObject(jsonString);
}

From source file:com.shadwelldacunha.byteswipe.core.Utilities.java

public static String readFileAsString(File file, Charset encoding) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
    return new String(encoded, encoding);
}

From source file:br.com.RatosDePC.Brpp.compiler.BrppCompiler.java

public static boolean compile(String path) {
    setFile(FileUtils.getBrinodirectory() + System.getProperty("file.separator") + "Arduino");
    setFile(getFile()//from ww  w .j av a  2 s . co  m
            .concat(path.substring(path.lastIndexOf(System.getProperty("file.separator")), path.length() - 5)));
    setFile(getFile()
            .concat(path.substring(path.lastIndexOf(System.getProperty("file.separator")), path.length() - 4)));
    setFile(getFile().concat("ino"));
    File ino = new File(getFile());
    if (!ino.exists()) {
        try {
            ino.getParentFile().mkdirs();
            ino.createNewFile();
        } catch (IOException e) {

        }
    }
    try {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        String code = new String(encoded);
        JSONArray Keywords = JSONUtils.getKeywords();
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> iterator = Keywords.iterator();
        while (iterator.hasNext()) {
            JSONObject key = iterator.next();
            String arg = (String) key.get("arg");
            if (arg.equals("false")) {
                code = code.replace((String) key.get("translate"), (String) key.get("arduino"));
            } else {
                code = code.replaceAll((String) key.get("translate"), (String) key.get("arduino"));
            }
        }

        try (FileWriter file = new FileWriter(getFile())) {
            file.write(code);
        }
        System.out.println(code);
        return true;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;

}

From source file:net.dv8tion.discord.commands.KanzeTodoCommand.java

public KanzeTodoCommand(JDA api) {
    this.api = api;
    try {//from  w  w w  .  j a v a  2s.c om
        jsonFile = new File("todo-list.json");
        if (jsonFile.exists()) {
            todoList = new JSONArray(new String(Files.readAllBytes(jsonFile.toPath())));
        } else {
            todoList = new JSONArray();
            Files.write(jsonFile.toPath(), todoList.toString(4).getBytes());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:name.npetrovski.jphar.DataEntry.java

/**
 * Create entry from file/*from   w w  w. j av  a2 s  .com*/
 *
 */
public static DataEntry createFromFile(File file, Compression.Type compression) throws IOException {

    EntryManifest em = new EntryManifest();
    em.setCompression(new Compression(compression));
    em.setTimestamp((int) file.lastModified() / 1000);
    em.getPath().setName(file.toPath().toString().replace("\\", "/"));

    DataEntry entry = new DataEntry(em);
    entry.setSource(file.getCanonicalFile());

    if (file.isDirectory()) {
        em.getCompression().setType(Compression.Type.NONE);
    } else {
        byte[] data = Files.readAllBytes(file.toPath());
        CRC32 crc = new CRC32();
        crc.update(data);

        em.setCRC32((int) crc.getValue());
        em.setUncompressedSize(data.length);
    }

    return entry;

}