Example usage for java.nio.file Files readAllLines

List of usage examples for java.nio.file Files readAllLines

Introduction

In this page you can find the example usage for java.nio.file Files readAllLines.

Prototype

public static List<String> readAllLines(Path path) throws IOException 

Source Link

Document

Read all lines from a file.

Usage

From source file:rgu.jclos.foldbuilder.FoldBuilder.java

/**
* Generates K folds and writes them to disk
* @param inputFile The CSV file from which the data comes from.
* @param outputDirectory The directory in which the folds will be written.
* @param separator The separating character in the CSV file.
* @param indexLabel The index of the labels in the CSV file. Used for stratification of the folds.
* @param k The number of folds to generates.
* @param speak Whether to print some status messages along the way.
* @return A pair containing a list of folds with ids of documents, and a dictionary that allows the user to retrieve aformentioned documents using the ids, in order to save space.
* @throws IOException If something stops the program from reading or writing the files.
*///ww w  .j a v  a  2s .  com
private static Pair<List<Set<String>>, Map<String, Instance>> getFolds(String inputFile, String outputDirectory,
        String separator, String indexLabel, int k, boolean speak) throws IOException {
    Random rng = new Random();
    Map<String, Instance> dictionary = new HashMap<>();
    Map<String, Integer> classes = new HashMap<>();
    Map<String, List<String>> reversedDictionary = new HashMap<>();
    int id = 0;

    List<String> lines = Files.readAllLines(new File(inputFile).toPath());
    String[] elts = lines.get(0).split(separator);
    int labIndex = indexLabel.equals("first") ? 0
            : indexLabel.equals("last") ? elts.length - 1 : Integer.parseInt(indexLabel);

    for (String line : Files.readAllLines(new File(inputFile).toPath())) {
        Instance inst = new Instance();
        String[] elements = line.split(separator);
        inst.content = line;
        inst.label = elements[labIndex];
        String iid = "inst" + id;
        dictionary.put(iid, inst);
        classes.put(inst.label, classes.getOrDefault(inst.label, 0) + 1);
        if (reversedDictionary.containsKey(inst.label)) {
            reversedDictionary.get(inst.label).add(iid);
        } else {
            List<String> ids = new ArrayList<>();
            ids.add(iid);
            reversedDictionary.put(inst.label, ids);
        }
        id++;
    }

    int numberOfInstances = id;
    int sizeOfEachFold = (int) Math.floor(numberOfInstances / k);
    Map<String, Double> classRatios = new HashMap<>();
    for (Map.Entry<String, Integer> classFrequency : classes.entrySet()) {
        classRatios.put(classFrequency.getKey(),
                (double) classFrequency.getValue() / (double) numberOfInstances);
    }

    List<Set<String>> folds = new ArrayList<>();
    for (int i = 0; i < k; i++) {
        Set<String> fold = new HashSet<>();
        for (Map.Entry<String, List<String>> c : reversedDictionary.entrySet()) {
            int currentSize = fold.size();
            int numberRequired = (int) Math.floor(classRatios.get(c.getKey()) * sizeOfEachFold);
            while (fold.size() < currentSize + numberRequired && c.getValue().size() > 0) {
                int nextPick = rng.nextInt(c.getValue().size());
                fold.add(c.getValue().get(nextPick));
                c.getValue().remove(nextPick);
            }
        }
        folds.add(fold);
        if (speak)
            System.out.println("Finished computing fold " + (i + 1) + " of size " + fold.size());
    }

    if (speak)
        System.out.println("Writing folds on disk");

    return Pair.of(folds, dictionary);
}

From source file:grakn.core.console.ConsoleSession.java

/**
 * Open the user's preferred editor to write a query
 *
 * @return the string written in the editor
 *//*  w  w w .java  2 s. c om*/
private String openTextEditor() throws IOException, InterruptedException {
    File tempFile = new File(System.getProperty("java.io.tmpdir") + EDITOR_FILE);
    tempFile.createNewFile();

    ProcessBuilder builder;

    if (isWindows()) {
        String editor = Optional.ofNullable(System.getenv().get("EDITOR")).orElse(WINDOWS_EDITOR_DEFAULT);
        builder = new ProcessBuilder("cmd", "/c", editor + " " + tempFile.getAbsolutePath());
    } else {
        String editor = Optional.ofNullable(System.getenv().get("EDITOR")).orElse(UNIX_EDITOR_DEFAULT);
        // Run the editor, pipe input into and out of tty so we can provide the input/output to the editor via Graql
        builder = new ProcessBuilder("/bin/bash", "-c",
                editor + " </dev/tty >/dev/tty " + tempFile.getAbsolutePath());
    }

    builder.start().waitFor();
    return String.join("\n", Files.readAllLines(tempFile.toPath()));
}

From source file:org.languagetool.rules.spelling.suggestions.SuggestionChangesTest.java

public void testText() throws IOException {
    File configFile = new File(System.getProperty("config", "SuggestionChangesTestConfig.json"));
    ObjectMapper mapper = new ObjectMapper(new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS));
    SuggestionChangesTestConfig config = mapper.readValue(configFile, SuggestionChangesTestConfig.class);
    SuggestionsChanges.init(config, null);

    Path inputFile = Paths.get(System.getProperty("input"));
    String text = String.join("\n", Files.readAllLines(inputFile));
    File ngrams = new File(SuggestionsChanges.getInstance().getConfig().ngramLocation);
    Language lang = Languages.getLanguageForShortCode(config.language);
    List<SuggestionChangesExperiment> experiments = SuggestionsChanges.getInstance().getExperiments();
    for (SuggestionChangesExperiment experiment : experiments) {
        SuggestionsChanges.getInstance().setCurrentExperiment(experiment);
        JLanguageTool lt = new JLanguageTool(lang);
        lt.activateLanguageModelRules(ngrams);
        List<RuleMatch> matches = lt.check(text);
        System.out.printf("%nExperiment %s running...%n", experiment);
        for (RuleMatch match : matches) {
            if (!match.getRule().isDictionaryBasedSpellingRule()) {
                continue;
            }//  w ww .  jav a  2 s  .c o m

            String covered = text.substring(match.getFromPos(), match.getToPos());
            System.out.printf("Correction: '%s' -> %s%n", covered, match.getSuggestedReplacements());
        }
    }
}

From source file:com.sumzerotrading.intraday.trading.strategy.ReportGeneratorTest.java

@Test
public void testWriteRoundTrip() throws Exception {
    Path path = Files.createTempFile("ReportGeneratorUnitTest", ".txt");
    reportGenerator.outputFile = path.toString();
    String expected = "2016-03-19T07:01:10,LONG,ABC,100,100.23,0,2016-03-20T06:01:10,101.23,0";

    Ticker longTicker = new StockTicker("ABC");
    Ticker shortTicker = new StockTicker("XYZ");
    int longSize = 100;
    int shortSize = 50;
    double longEntryFillPrice = 100.23;
    double longExitFillPrice = 101.23;
    ZonedDateTime entryTime = ZonedDateTime.of(2016, 3, 19, 7, 1, 10, 0, ZoneId.systemDefault());
    ZonedDateTime exitTime = ZonedDateTime.of(2016, 3, 20, 6, 1, 10, 0, ZoneId.systemDefault());

    TradeOrder longEntry = new TradeOrder("123", longTicker, longSize, TradeDirection.BUY);
    longEntry.setFilledPrice(longEntryFillPrice);
    longEntry.setOrderFilledTime(entryTime);
    TradeReferenceLine entryLine = new TradeReferenceLine();
    entryLine.correlationId = "123";
    entryLine.direction = Direction.LONG;
    entryLine.side = Side.ENTRY;//from  w  w  w.j  a  v  a  2s  .c o  m

    TradeOrder longExit = new TradeOrder("123", longTicker, longSize, TradeDirection.SELL);
    longExit.setFilledPrice(longExitFillPrice);
    longExit.setOrderFilledTime(exitTime);
    TradeReferenceLine exitLine = new TradeReferenceLine();
    exitLine.correlationId = "123";
    exitLine.direction = LONG;
    exitLine.side = Side.EXIT;

    RoundTrip roundTrip = new RoundTrip();
    roundTrip.addTradeReference(longEntry, entryLine);
    roundTrip.addTradeReference(longExit, exitLine);

    System.out.println("Writing out to file: " + path);

    reportGenerator.writeRoundTripToFile(roundTrip);

    List<String> lines = Files.readAllLines(path);
    assertEquals(1, lines.size());
    assertEquals(expected, lines.get(0));

    Files.deleteIfExists(path);

}

From source file:org.sonar.java.it.JavaRulingTest.java

private static void dumpServerLogLastLines(File logFile) throws IOException {
    List<String> logs = Files.readAllLines(logFile.toPath());
    int nbLines = logs.size();
    if (nbLines > LOGS_NUMBER_LINES) {
        logs = logs.subList(nbLines - LOGS_NUMBER_LINES, nbLines);
    }//from  w  w w .  j a v  a 2 s .  com
    LOG.error("=================================== START " + logFile.getName()
            + " ===================================");
    LOG.error(System.lineSeparator() + logs.stream().collect(Collectors.joining(System.lineSeparator())));
    LOG.error("===================================== END " + logFile.getName()
            + " ===================================");
}

From source file:com.sumzerotrading.eod.trading.strategy.ReportGeneratorTest.java

@Test
public void testWriteRoundTrip() throws Exception {
    Path path = Files.createTempFile("ReportGeneratorUnitTest", ".txt");
    reportGenerator.outputFile = path.toString();
    String expected = "2016-03-19T07:01:10,Long,ABC,100,100.23,0,2016-03-20T06:01:10,101.23,0,Short,XYZ,50,250.34,0,251.34,0";

    Ticker longTicker = new StockTicker("ABC");
    Ticker shortTicker = new StockTicker("XYZ");
    int longSize = 100;
    int shortSize = 50;
    double longEntryFillPrice = 100.23;
    double longExitFillPrice = 101.23;
    double shortEntryFillPrice = 250.34;
    double shortExitFillPrice = 251.34;
    ZonedDateTime entryTime = ZonedDateTime.of(2016, 3, 19, 7, 1, 10, 0, ZoneId.systemDefault());
    ZonedDateTime exitTime = ZonedDateTime.of(2016, 3, 20, 6, 1, 10, 0, ZoneId.systemDefault());

    TradeOrder longEntry = new TradeOrder("123", longTicker, longSize, TradeDirection.BUY);
    longEntry.setFilledPrice(longEntryFillPrice);
    longEntry.setOrderFilledTime(entryTime);

    TradeOrder longExit = new TradeOrder("123", longTicker, longSize, TradeDirection.SELL);
    longExit.setFilledPrice(longExitFillPrice);
    longExit.setOrderFilledTime(exitTime);

    TradeOrder shortEntry = new TradeOrder("123", shortTicker, shortSize, TradeDirection.SELL);
    shortEntry.setFilledPrice(shortEntryFillPrice);
    shortEntry.setOrderFilledTime(entryTime);

    TradeOrder shortExit = new TradeOrder("123", shortTicker, shortSize, TradeDirection.BUY);
    shortExit.setFilledPrice(shortExitFillPrice);
    shortExit.setOrderFilledTime(exitTime);

    RoundTrip roundTrip = new RoundTrip();
    roundTrip.longEntry = longEntry;//from   w  ww  . j  a va2s .co m
    roundTrip.longExit = longExit;
    roundTrip.shortEntry = shortEntry;
    roundTrip.shortExit = shortExit;

    System.out.println("Writing out to file: " + path);

    reportGenerator.writeRoundTripToFile(roundTrip);

    List<String> lines = Files.readAllLines(path);
    assertEquals(1, lines.size());
    assertEquals(expected, lines.get(0));

    Files.deleteIfExists(path);

}

From source file:org.moe.cli.ParameterParserTest.java

@Test
public void generateGibridBindings() throws Exception {

    File project = tmpDir.newFolder();
    ClassLoader cl = this.getClass().getClassLoader();
    URL header = cl.getResource("natives/AppViewController.h");
    CommandLine argc = parseArgs(new String[] { "--path-to-project", project.getPath(), "--headers",
            header.getPath(), "--package-name", "org", "--create-from-prototype" });
    IExecutor executor = ExecutorManager.getExecutorByParams(argc);
    assertNotNull(executor);/*from  ww w .j av a  2  s . c  om*/
    assertTrue(executor instanceof GenerateBindingExecutor);

    // generate binding
    executor.execute();

    // check if file exist
    File genJava = new File(project, "src/main/java/org/AppViewController.java");
    assertTrue(genJava.exists());

    // check content
    // @property (weak, nonatomic) IBOutlet UIButton *helloButton;
    // @property (weak, nonatomic) IBOutlet UILabel *statusText;
    // - (IBAction)BtnPressedCancel_helloButton:(NSObject*)sender;

    Pattern helloButtonPattern = Pattern.compile(".*@Selector\\(\"helloButton\"\\).*");
    Pattern statusTextPattern = Pattern.compile(".*@Selector\\(\"statusText\"\\).*");
    Pattern helloButtonActionPattern = Pattern.compile(".*@Selector\\(\"BtnPressedCancel_helloButton:\"\\).*");
    Pattern objCClassBindingPattern = Pattern.compile(".*@ObjCClassName\\(\"AppViewController\"\\).*");

    List<String> lineArray = Files.readAllLines(Paths.get(genJava.getPath()));
    boolean isHelloButton = false;
    boolean isStatusText = false;
    boolean isHelloButtonAction = false;
    boolean isObjCClassName = false;

    for (String line : lineArray) {
        Matcher hellowButtonMatcher = helloButtonPattern.matcher(line);
        Matcher statusTextMatcher = statusTextPattern.matcher(line);
        Matcher helloButtonActionMatcher = helloButtonActionPattern.matcher(line);
        Matcher objCMatcher = objCClassBindingPattern.matcher(line);

        isHelloButton |= hellowButtonMatcher.find();
        isStatusText |= statusTextMatcher.find();
        isHelloButtonAction |= helloButtonActionMatcher.find();
        isObjCClassName |= objCMatcher.find();
    }

    project.delete();

    assertTrue(isHelloButton);
    assertTrue(isStatusText);
    assertTrue(isHelloButtonAction);
    assertTrue(isObjCClassName);

}

From source file:org.apdplat.superword.rule.TextAnalysis.java

public static Map<String, List<String>> findEvidence(Path dir, List<String> words) {
    LOGGER.info("?" + dir);
    Map<String, List<String>> data = new HashMap<>();
    try {//from  w ww  .  jav  a  2  s  .c om
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String fileName = file.toFile().getAbsolutePath();
                if (file.toFile().getName().startsWith(".")) {
                    return FileVisitResult.CONTINUE;
                }
                if (!fileName.endsWith(".txt")) {
                    LOGGER.info("??txt" + fileName);
                    return FileVisitResult.CONTINUE;
                }

                LOGGER.info("?" + fileName);
                List<String> lines = Files.readAllLines(file);
                for (int i = 0; i < lines.size(); i++) {
                    final String line = lines.get(i);
                    final int index = i;
                    words.forEach(word -> {
                        if (line.toLowerCase().contains(word)) {
                            data.putIfAbsent(word, new ArrayList<>());
                            data.get(word).add(line + " <u><i>" + file.toFile().getName().replace(".txt", "")
                                    + "</i></u>");
                        }
                    });
                }

                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:edu.usu.sdl.openstorefront.usecase.AttributeImport.java

private AttributeTypeView loadJARMESArch() {
    AttributeTypeView attributeTypeView = new AttributeTypeView();

    CSVParser parser = new CSVParser();
    Path path = Paths.get(FileSystemManager.getDir(FileSystemManager.IMPORT_DIR) + "/jarmesl.csv");
    try {// w  ww.j a v  a2 s.  co  m
        List<String> lines = Files.readAllLines(path);
        //read type
        try {
            String data[] = parser.parseLine(lines.get(1));

            attributeTypeView.setAttributeType(data[0].trim());
            attributeTypeView.setDescription(data[1].trim());
            attributeTypeView.setArchitectureFlg(Convert.toBoolean(data[2].trim()));
            attributeTypeView.setVisibleFlg(Convert.toBoolean(data[3].trim()));
            attributeTypeView.setImportantFlg(Convert.toBoolean(data[4].trim()));
            attributeTypeView.setRequiredFlg(Convert.toBoolean(data[5].trim()));
            attributeTypeView.setAllowMultipleFlg(Convert.toBoolean(data[6].trim()));

            for (int i = 2; i < lines.size(); i++) {
                if (StringUtils.isNotBlank(lines.get(i))) {
                    data = parser.parseLine(lines.get(i));
                    AttributeCodeView attributeCodeView = new AttributeCodeView();
                    if (StringUtils.isNotBlank(data[0].trim())) {
                        attributeCodeView.setCode(data[0].toUpperCase().trim());
                        attributeCodeView.setLabel(data[0].toUpperCase().trim() + " " + data[1].trim());
                        attributeTypeView.getCodes().add(attributeCodeView);
                    }
                }
            }

        } catch (IOException ex) {
            log.log(Level.SEVERE, null, ex);
        }

    } catch (IOException ex) {

        log.log(Level.SEVERE, null, ex);
    }

    return attributeTypeView;
}

From source file:com.sumzerotrading.reporting.csv.ReportGeneratorTest.java

@Test
public void testWriteRoundTrip() throws Exception {
    Path path = Files.createTempFile("ReportGeneratorUnitTest", ".txt");
    reportGenerator.outputFile = path.toString();
    String expected = "2016-03-19T07:01:10,Long,ABC,100,100.23,0,2016-03-20T06:01:10,101.23,0,Short,XYZ,50,250.34,0,251.34,0";

    Ticker longTicker = new StockTicker("ABC");
    Ticker shortTicker = new StockTicker("XYZ");
    int longSize = 100;
    int shortSize = 50;
    double longEntryFillPrice = 100.23;
    double longExitFillPrice = 101.23;
    double shortEntryFillPrice = 250.34;
    double shortExitFillPrice = 251.34;
    ZonedDateTime entryTime = ZonedDateTime.of(2016, 3, 19, 7, 1, 10, 0, ZoneId.systemDefault());
    ZonedDateTime exitTime = ZonedDateTime.of(2016, 3, 20, 6, 1, 10, 0, ZoneId.systemDefault());

    TradeOrder longEntry = new TradeOrder("123", longTicker, longSize, TradeDirection.BUY);
    longEntry.setFilledPrice(longEntryFillPrice);
    longEntry.setOrderFilledTime(entryTime);

    TradeOrder longExit = new TradeOrder("123", longTicker, longSize, TradeDirection.SELL);
    longExit.setFilledPrice(longExitFillPrice);
    longExit.setOrderFilledTime(exitTime);

    TradeOrder shortEntry = new TradeOrder("123", shortTicker, shortSize, TradeDirection.SELL);
    shortEntry.setFilledPrice(shortEntryFillPrice);
    shortEntry.setOrderFilledTime(entryTime);

    TradeOrder shortExit = new TradeOrder("123", shortTicker, shortSize, TradeDirection.BUY);
    shortExit.setFilledPrice(shortExitFillPrice);
    shortExit.setOrderFilledTime(exitTime);

    PairTradeRoundTrip roundTrip = new PairTradeRoundTrip();
    roundTrip.longEntry = longEntry;/*  w  ww  . j  a  va  2  s . c  o m*/
    roundTrip.longExit = longExit;
    roundTrip.shortEntry = shortEntry;
    roundTrip.shortExit = shortExit;

    System.out.println("Writing out to file: " + path);

    reportGenerator.writeRoundTripToFile(roundTrip);

    List<String> lines = Files.readAllLines(path);
    assertEquals(1, lines.size());
    assertEquals(expected, lines.get(0));

    Files.deleteIfExists(path);

}