Java Path File Read nio readFile(String path)

Here you can find the source of readFile(String path)

Description

read File

License

Open Source License

Declaration

public static List<String> readFile(String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static List<String> readFile(String path) {
        List<String> contexts = new ArrayList<>();
        try {/* w  w w . j a v a  2 s.com*/
            Stream<String> content = Files.lines(Paths.get(path));
            Stream<String> tuples = content.flatMap(line -> {

                //Separating label and values
                return Arrays.stream(line.split(";")).map(t -> {

                    String[] values = t.split(",");
                    String label = values[0];
                    String[] predicates = values[1].split("_");

                    /*    predicates[0] = "battery=" + predicates[0];
                    predicates[1] = "bright=" + predicates[1];
                    predicates[2] = "screen=" + predicates[2];
                    predicates[3] = "power=" + predicates[3];
                    predicates[4] = "headset=" + predicates[4];
                    predicates[5] = "app=" + predicates[5];
                    predicates[6] = "period=" + predicates[6];
                    predicates[7] = "time=" + predicates[7];
                    predicates[8] = "gps=" + predicates[8];*/

                    /*for(int i =0; i < predicates.length; i++)
                    predicates[i] = predicates[i] + "=1";*/

                    //                    return label + "\t" + String.join(" ", predicates); // It has been used when the generated model is using DocumentCategorizer
                    return String.join(" ", predicates) + " " + label; // Use it When generating GIS or L-BFGs models
                });

            });

            contexts.addAll(tuples.collect(Collectors.toList()));
            content.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Total: " + contexts);
        return contexts;
    }
}

Related

  1. readFile(Path directory, String... parts)
  2. readFile(Path file)
  3. readFile(Path path)
  4. readFile(Path path)
  5. readFile(String path)
  6. readFile(String path)
  7. readFile(String path)
  8. readFile(String path)
  9. readFileAsString(String filePath)