Beispiel #1
0
  public static void main(String[] args) throws IOException {
    Path path = Paths.get("../alice.txt");
    String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

    Stream<String> words = Stream.of(contents.split("[\\P{L}]+"));
    show("words", words);
    Stream<String> song = Stream.of("gently", "down", "the", "stream");
    show("song", song);
    Stream<String> silence = Stream.empty();
    silence = Stream.<String>empty(); // Explicit type specification
    show("silence", silence);

    Stream<String> echos = Stream.generate(() -> "Echo");
    show("echos", echos);

    Stream<Double> randoms = Stream.generate(Math::random);
    show("randoms", randoms);

    Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n -> n.add(BigInteger.ONE));
    show("integers", integers);

    Stream<String> wordsAnotherWay = Pattern.compile("[\\P{L}]+").splitAsStream(contents);
    show("wordsAnotherWay", wordsAnotherWay);

    try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
      show("lines", lines);
    }
  }
Beispiel #2
0
 /**
  * Checks if a given file exists and, if not, create it by copying a default template from
  * resources; used to create default conf files.
  *
  * @param file The path of the file that needs to exist
  * @param template The path of the template for the file
  */
 public static void ensureFileExists(final String file, final String template) {
   if (LAUNCHED_FROM_JAR && !Files.exists(Paths.get(file))) {
     if (Debug.on) printDebug("[Meta] " + file + " does not exist: creating a default one.");
     InputStream stream = Meta.class.getResourceAsStream(template);
     if (stream == null) {
       printDebug(
           "[ WARNING ] template for "
               + template
               + " not found. Won't create a default "
               + file
               + ".");
     } else {
       int readBytes;
       byte[] buffer = new byte[4096];
       try (OutputStream outStream = new FileOutputStream(new File(file))) {
         while ((readBytes = stream.read(buffer)) > 0) {
           outStream.write(buffer, 0, readBytes);
         }
         if (Debug.on)
           printDebug("[Meta] created default file " + file + " from " + template + ".");
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
         try {
           stream.close();
         } catch (IOException ignore) {
         }
       }
     }
   } else {
     if (Meta.LAUNCHED_FROM_JAR && Debug.on) printDebug("[Meta] file exists: " + file + ".");
   }
 }
Beispiel #3
0
 static {
   String tmp = Meta.class.getProtectionDomain().getCodeSource().getLocation().getPath();
   // Strip the leading slash if on windows
   if (tmp.matches("^/[A-Z]:/.*")) {
     tmp = tmp.substring(1);
   }
   cwd = Paths.get(tmp);
 }
Beispiel #4
0
 /**
  * Searches for the directory 'dirPath'; if not found, tries to create it, then returns a File
  * object for that directory.
  */
 public static File ensureDirExists(final String dirPath) {
   File dirpath = new File(dirPath);
   if (!dirpath.isDirectory()) {
     if (!dirpath.exists()) {
       printDebug("[Meta] " + dirpath + " does not exist: creating it...");
       try {
         Files.createDirectories(Paths.get(dirpath.getPath()));
         return dirpath;
       } catch (IOException e) {
         printDebug("[Meta] Exception while creating directory:");
         e.printStackTrace();
         return null;
       }
     } else {
       printDebug(
           "[Meta] Error: path `"
               + dirpath
               + "' is not a valid directory path, and could not create it.");
       return null;
     }
   } else return dirpath;
 }