An example of parsing a file with Java8 stream and lambda expressions

Source: Internet
Author: User

Recently I want to extract the specified data from a log file, which is part of the log:
2015-01-06 11:33:03 b.s.d.task [INFO] Emitting:eventtorequestsbolt __ack_ack [-6722594615019711369- 1335723027906100557]22015-01-06 11:33:03 c.s.p.d.packagesprovider [INFO] = = =---> Loaded package com.foo.bar32015-01-06 11:33:04 b.s.d.executor [INFO] processing received message Source:eventtomanagebolt:2, stream: _ _ack_ack, ID: {}, [ -6722594615019711369-1335723027906100557]42015-01-06 11:33:04 C.s.p.d.packagesprovider [INFO] ===- --Loaded Package co.il.boo52015-01-06 11:33:04 c.s.p.d.packagesprovider [INFO] = = =---> Loaded package Dot.org.biz

I'm going to use stream and lambda expressions in Java8 to do this.

Read file

First I want the reading log file and then put each line of the log into a stream.

Stream<string> lines = Files.lines (Paths.get (args[1]));

Filter the related rows

I need to get the package name and write it to another file, not all the rows contain the data I want, so I only filter the related rows.

Lines.filter (Line-line.contains ("= = =---> Loaded package")

Parse related rows

Next, I want to parse the related rows. I split each row of data into a string array and get the last element of the array. In other words, I've been mapping two times. First from a row of data to an array and then from an array to a string.

. Map (line-Line.split ("")). Map (arr. arr[arr.length-1])

Write to output file

The final step is to write each string to the output file. This is the last operation.

. ForEach (Package-WriteToFile (FW, package));

Write ToFile is a way of customizing me. The reason for this is that the Java file system throws IOException, but you cannot use a checked exception in a lambda expression.

The following is the complete code:

Import Java.io.filewriter;import java.io.ioexception;import Java.nio.file.files;import Java.nio.file.Paths;import Java.util.arrays;import Java.util.list;import Java.util.stream.stream;public class App {public static void main (String [] args) throws IOException {stream<string> lines = null;if (args.length = = 2) {lines = Files.lines (Paths.get (args[1 ]));} else {String S1 = "2015-01-06 11:33:03 b.s.d.task [INFO] Emitting:adeventtorequestsbolt __ack_ack [-6722594615019711369- 1335723027906100557] "; String s2 = "2015-01-06 11:33:03 b.s.d.executor [INFO] processing received message Source:eventtomanagebolt:2, stream: __ Ack_ack, ID: {}, [-6722594615019711369-1335723027906100557] "; String s3 = "2015-01-06 11:33:04 c.s.p.d.packagesprovider [INFO] = = =---> Loaded package Com.foo.bar"; String S4 = "2015-01-06 11:33:04 c.s.p.d.packagesprovider [INFO] = = =---> Loaded package Co.il.boo"; String S5 = "2015-01-06 11:33:04 c.s.p.d.packagesprovider [INFO] = = =---> Loaded package dot.org.biz"; List<string> rows = arrays.aslist (S1, S2, S3, S4, S5); lines = Rows.stream ();} New App (). Parse (lines, args[0]);} private void Parse (stream<string> lines, String output) throws IOException {final FileWriter fw = new FileWriter (out put);//@formatter: Offlines.filter (line-line.contains ("= = =---> Loaded package"). Map, line-Line.split (" "). (arr[arr.length-1). ForEach (Package-WriteToFile (FW, package));//@formatter: Onfw.close (); Lines.close ();} private void WriteToFile (FileWriter FW, String package) {try {fw.write (String.Format ("%s%n", Package));} catch ( IOException e) {throw new RuntimeException (e);}}}

Original address: http://www.javacodegeeks.com/2015/01/java-8-stream-and-lambda-expressions-parsing-file-example.html

An example of parsing a file with Java8 stream and lambda expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.