Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to process stream and read text file in Java 8

DZone's Guide to

How to process stream and read text file in Java 8

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

I have converted one of my old utility class using latest Java8. I use this often to print content of manifest file to check any mysterious jar file for version etc. Just run "java ztools.PrintJar /path/to/my.jar" to see output. In the new code, you will see how I use the Java 8 stream processing to filter what I need from an Enumeration list, and then get the optional result if there is any. And then the BufferedReader now comes with "lines()" method that also do streaming. It's pretty cool to see Java 8 in actions!

package ztools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Print text based resource file inside a jar file. (eg: META-INF/MANIFEST.MF)
 * @author Zemian Deng
 */
public class PrintJar {
  public static void main(String[] args) throws Exception {
  // Search given name in a jar
  JarFile jarFile = new JarFile(args[0]);
  final String searchName = (args.length >= 2) ? args[1] : "META-INF/MANIFEST.MF";
  Optional<JarEntry> searchResult = jarFile
  .stream()
  .filter(e -> e.getName().equals(searchName))
  .findFirst();
  if (!searchResult.isPresent())
  throw new RuntimeException(searchName + " not found!");
 
  // Print the JarEntry
  JarEntry entry = searchResult.get();
  try (InputStream instream = jarFile.getInputStream(entry)) {
  BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
  reader.lines().forEach(line -> System.out.println(line));
  }
  }
}

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}