public static void main(String[] args) throws IOException {
    Closer closer = Closer.create();
    // copy a file
    File origin = new File("join_temp");
    File copy = new File("target_temp");

    try {
      BufferedReader reader = new BufferedReader(new FileReader("join_temp"));
      BufferedWriter writer = new BufferedWriter(new FileWriter("target_temp"));

      closer.register(reader);
      closer.register(writer);

      String line;

      while ((line = reader.readLine()) != null) {
        writer.write(line);
      }
    } catch (IOException e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }

    Files.copy(origin, copy);

    File moved = new File("moved");

    // moving renaming
    Files.move(copy, moved);

    // working files as string
    List<String> lines = Files.readLines(origin, Charsets.UTF_8);

    HashCode hashCode = Files.hash(origin, Hashing.md5());
    System.out.println(hashCode);

    // file write and append
    String hamlet = "To be, or not to be it is a question\n";
    File write_and_append = new File("write_and_append");

    Files.write(hamlet, write_and_append, Charsets.UTF_8);

    Files.append(hamlet, write_and_append, Charsets.UTF_8);

    //        write_and_append.deleteOnExit();

    Files.write("OverWrite the file", write_and_append, Charsets.UTF_8);

    // ByteSource ByteSink
    ByteSource fileBytes = Files.asByteSource(write_and_append);
    byte[] readBytes = fileBytes.read();
    // equals to pre line -> Files.toByteArray(write_and_append) == readBytes

    ByteSink fileByteSink = Files.asByteSink(write_and_append);
    fileByteSink.write(Files.toByteArray(write_and_append));

    BaseEncoding base64 = BaseEncoding.base64();
    System.out.println(base64.encode("123456".getBytes()));
  }
예제 #2
0
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    checkCreate(qc);

    final Path path = toPath(0, qc);
    final B64 archive = toB64(exprs[1], qc, false);
    final TokenSet hs = entries(2, qc);

    try (ArchiveIn in = ArchiveIn.get(archive.input(info), info)) {
      while (in.more()) {
        final ZipEntry ze = in.entry();
        final String name = ze.getName();
        if (hs == null || hs.delete(token(name)) != 0) {
          final Path file = path.resolve(name);
          if (ze.isDirectory()) {
            Files.createDirectories(file);
          } else {
            Files.createDirectories(file.getParent());
            Files.write(file, in.read());
          }
        }
      }
    } catch (final IOException ex) {
      throw ARCH_FAIL_X.get(info, ex);
    }
    return null;
  }
예제 #3
0
파일: IO.java 프로젝트: nremond/boon
  public static void write(Path file, byte[] contents) {
    try {
      Files.write(file, contents);

    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
예제 #4
0
 public static void write(String fullyNamedPath, String... lines) {
   Path file = Paths.get(fullyNamedPath);
   try {
     Files.write(
         file, Arrays.asList(lines), Charset.forName("UTF-8"), StandardOpenOption.CREATE_NEW);
   } catch (IOException ioEx) {
     // TODO: Report to front-end
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug("", ioEx);
     }
   }
 }
예제 #5
0
 public static boolean write(String fullyNamedPath, String content) {
   Path file = Paths.get(fullyNamedPath);
   try {
     Files.deleteIfExists(file);
     Files.write(
         file, Arrays.asList(content), Charset.forName("UTF-8"), StandardOpenOption.CREATE_NEW);
   } catch (IOException ioEx) {
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug("", ioEx);
     }
   }
   return Files.exists(file, LinkOption.NOFOLLOW_LINKS);
 }