简体   繁体   中英

Stream cutting in java8

What is a good method to cut stream? I have just started java 8 and I am stuck at this. I am reading from a file a couple of input date and then printing them. My problems is how do I cut precisely between the dates and the string ? can "tab" be used? What I have so far:

public static void main( String[] args ) throws IOException
    {

            Supplier<Stream<String>> fisier=()->{
                    try {
                        return Files.lines(Paths.get("Activities.txt"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return null;
                };
                fisier.get().forEach(System.out::println);
                //fisier.get().filter(predicate)
                //List<String>out=getFilterOutput(fisier.get(),"    ");
    }

I have thought about filtering the information, but how can I tell for what to look >? A line from the file looks like this:

2011-12-11 15:43:51 2011-12-11 21:41:48 Spare_Time/TV

You can do it like this:

    List <String> firstDate = new ArrayList<>();
    List <String> secondDate = new ArrayList<>();
    List <String> showName = new ArrayList<>();

    try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

        stream.forEach(line -> {
            String[] parts = line.split("\\s+");
            firstDate.add(parts[0] + parts[1]);
            secondDate.add(parts[2] + parts[3]);
            showName.add(parts[4]);
        });
    } catch (IOException e) {
        e.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM