Example usage for java.nio.file Files newBufferedReader

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

Introduction

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

Prototype

public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException 

Source Link

Document

Opens a file for reading, returning a BufferedReader that may be used to read text from the file in an efficient manner.

Usage

From source file:de.huberlin.wbi.cuneiform.cmdline.main.Main.java

public static String readFile(Path f) throws FileNotFoundException, IOException {

    String line;//from ww  w.  jav a  2 s  .  co  m
    StringBuffer buf;

    buf = new StringBuffer();
    try (BufferedReader reader = Files.newBufferedReader(f, Charset.forName("UTF-8"))) {

        while ((line = reader.readLine()) != null)
            buf.append(line).append('\n');
    }

    return buf.toString();

}

From source file:sadl.models.PDTTAold.java

public PDTTAold(Path trebaPath, TimedInput trainingSequences) throws IOException {
    final BufferedReader inputReader = Files.newBufferedReader(trebaPath, StandardCharsets.UTF_8);
    String line = "";
    // 172 172 3 0,013888888888888892
    // from state ; to state ; symbol ; probability
    while ((line = inputReader.readLine()) != null) {
        final String[] lineSplit = line.split(" ");
        if (lineSplit.length == 4) {
            final int fromState = Integer.parseInt(lineSplit[0]);
            final int toState = Integer.parseInt(lineSplit[1]);
            final String symbol;
            if (trainingSequences == null) {
                symbol = lineSplit[2];/*from   w ww . ja va  2  s  .co  m*/
            } else {
                symbol = trainingSequences.getSymbol(Integer.parseInt(lineSplit[2]));
            }
            final double probability = Double.parseDouble(lineSplit[3]);
            addTransition(fromState, toState, symbol, probability);
        } else if (lineSplit.length == 2) {
            final int state = Integer.parseInt(lineSplit[0]);
            final double finalProb = Double.parseDouble(lineSplit[1]);
            addFinalState(state, finalProb);
        }
    }
}

From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java

private static Stream<WidgetVarLocation> findWidgetVars(Path sourceDirectory, String sourcePattern,
        ThreadPoolExecutor threadPool) throws IOException {
    BlockingQueue<WidgetVarLocation> pipe = new LinkedBlockingQueue<>();
    List<Future<?>> futures = new ArrayList<>();
    Files.walkFileTree(sourceDirectory, new FileActionVisitor(sourceDirectory, sourcePattern,
            sourceFile -> futures.add(threadPool.submit(() -> {
                try (BufferedReader br = Files.newBufferedReader(sourceFile, StandardCharsets.UTF_8)) {
                    int lineNr = 0;
                    String line;//from  w  w  w  . j av  a 2s  . c o m
                    while ((line = br.readLine()) != null) {
                        lineNr++;

                        if (line.contains("widgetVar=\"")) {
                            int startIndex = line.indexOf("widgetVar=\"") + "widgetVar=\"".length();
                            int endIndex = line.indexOf('"', startIndex);
                            String var = line.substring(startIndex, endIndex);
                            WidgetVarLocation widgetVar = new WidgetVarLocation(var, sourceFile, lineNr,
                                    startIndex, line);

                            pipe.add(widgetVar);
                        }
                    }
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }))));

    return StreamSupport.stream(new PipeSpliterator(pipe, futures), true);
}

From source file:popgenutils.dfcp.PrepareVCF4DFCP.java

/**
 * //  ww  w .j  av a 2  s .c om
 */
private void makegeno() {
    gt_string_builder = new StringBuilder();
    StringBuilder header = new StringBuilder();
    try (BufferedReader in = Files.newBufferedReader(Paths.get(filename), Charset.forName("UTF-8"))) {
        BufferedWriter out = null;
        BufferedWriter out_bgl_ref = null;
        BufferedWriter out_bgl_geno = null;

        String line = null;
        int number_of_individuals = 0;
        Set<Integer> individuals_to_genotype = new HashSet<Integer>();
        while ((line = in.readLine()) != null) {

            if (line.startsWith("#CHROM")) {
                StringBuilder bglref_header = new StringBuilder();
                StringBuilder bglgeno_header = new StringBuilder();
                String[] parts = line.split("\t");
                //samples begin at 9
                number_of_individuals = parts.length - 9;
                List<Integer> index_of_individuals = new ArrayList<Integer>();
                for (int i = 0; i < number_of_individuals; i++) {
                    index_of_individuals.add(i);
                }
                Collections.shuffle(index_of_individuals);
                if (pgeno >= 0) {
                    // convert pgeno into ageno
                    ageno = (int) Math.ceil(number_of_individuals * pgeno);
                }
                for (int i = 0; i < ageno; i++) {
                    individuals_to_genotype.add(index_of_individuals.get(i));
                }

                bglref_header.append(parts[0]);
                bglgeno_header.append(parts[0]);
                for (int i = 1; i < 9; i++) {
                    bglref_header.append("\t" + parts[i]);
                    bglgeno_header.append("\t" + parts[i]);
                }
                for (int i = 9; i < parts.length; i++) {
                    int index = i - 9;
                    if (individuals_to_genotype.contains(index)) {
                        bglgeno_header.append("\t" + parts[i]);
                    } else {
                        bglref_header.append("\t" + parts[i]);
                    }
                }
                out = Files.newBufferedWriter(
                        Paths.get(output_dir + "/" + ageno + "_geno" + Paths.get(filename).getFileName()),
                        Charset.forName("UTF-8"));
                out_bgl_ref = Files.newBufferedWriter(
                        Paths.get(
                                output_dir + "/" + ageno + "_geno_bglref" + Paths.get(filename).getFileName()),
                        Charset.forName("UTF-8"));
                out_bgl_geno = Files.newBufferedWriter(
                        Paths.get(output_dir + "/" + ageno + "_geno_bgl" + Paths.get(filename).getFileName()),
                        Charset.forName("UTF-8"));
                out.write(header.toString());
                out_bgl_ref.write(header.toString());
                out_bgl_geno.write(header.toString());
                out.write(line + System.getProperty("line.separator"));
                out_bgl_geno.write(bglgeno_header + System.getProperty("line.separator"));
                out_bgl_ref.write(bglref_header + System.getProperty("line.separator"));
            } else if (line.startsWith("#")) {
                header.append(line + System.getProperty("line.separator"));
            } else {
                // format at 8
                String[] parts = line.split("\t");
                String[] parts2 = parts[8].split(":");
                int gt_pos = 0;
                for (int i = 1; i < parts2.length; i++) {
                    if (parts2[i].equals("GT"))
                        gt_pos = i;
                }
                out.write(parts[0]);
                out_bgl_geno.write(parts[0]);
                out_bgl_ref.write(parts[0]);
                for (int i = 1; i < parts.length; i++) {
                    if (i > 8) {
                        parts2 = parts[i].split(":");
                        if (gt_pos == 0 && individuals_to_genotype.contains(i - 9)) {
                            out.write("\t" + convertToGT(parts2[0]));
                            out_bgl_geno.write("\t" + convertToGT(parts2[0]));
                        } else {
                            out.write("\t" + parts2[0]);
                            out_bgl_ref.write("\t" + parts2[0]);
                        }
                        for (int j = 1; j < parts2.length; j++) {
                            if (gt_pos == j && individuals_to_genotype.contains(i - 9)) {
                                out.write(":" + convertToGT(parts2[i]));
                                out_bgl_geno.write(":" + convertToGT(parts2[i]));
                            } else {
                                out.write(":" + parts2[i]);
                                out_bgl_ref.write(":" + parts2[i]);
                            }
                        }
                    } else {
                        out.write("\t" + parts[i]);
                        out_bgl_ref.write("\t" + parts[i]);
                        out_bgl_geno.write("\t" + parts[i]);
                    }
                }
                out.write(System.getProperty("line.separator"));
                out_bgl_ref.write(System.getProperty("line.separator"));
                out_bgl_geno.write(System.getProperty("line.separator"));
            }
        }
        out.close();
        out_bgl_ref.close();
        out_bgl_geno.close();
    } catch (IOException e) {
        System.err.println("could not read from file " + filename);
        e.printStackTrace();
    }
}

From source file:org.mycore.frontend.cli.MCRCommandLineInterface.java

/**
 * Reads a file containing a list of commands to be executed and adds them
 * to the commands queue for processing. This method implements the
 * "process ..." command.//  w  w  w.j  a v a2 s.  co  m
 * 
 * @param file
 *            The file holding the commands to be processed
 * @throws IOException
 *             when the file could not be read
 * @throws FileNotFoundException
 *             when the file was not found
 */
public static List<String> readCommandsFile(String file) throws IOException, FileNotFoundException {
    try (BufferedReader reader = Files.newBufferedReader(new File(file).toPath(), Charset.defaultCharset())) {
        output("Reading commands from file " + file);

        String line;
        List<String> list = new ArrayList<String>();
        while ((line = reader.readLine()) != null) {
            line = line.trim();

            if (line.startsWith("#") || line.isEmpty()) {
                continue;
            }
            list.add(line);
        }
        return list;
    }
}

From source file:org.apache.lucene.benchmark.byTask.tasks.WriteLineDocTaskTest.java

/** Fail by default when there's only date */
public void testJustDate() throws Exception {
    Path file = getWorkDir().resolve("one-line");
    PerfRunData runData = createPerfRunData(file, false, JustDateDocMaker.class.getName());
    WriteLineDocTask wldt = new WriteLineDocTask(runData);
    wldt.doLogic();/*from  w ww  .ja va 2  s .c  om*/
    wldt.close();

    try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
        String line = br.readLine();
        assertHeaderLine(line);
        line = br.readLine();
        assertNull(line);
    }
}

From source file:de.ncoder.studipsync.studip.jsoup.JsoupStudipAdapter.java

@SuppressWarnings("unchecked")
public void restoreCookies() throws IOException {
    if (cookiesPath != null) {
        log.info("Restore Cookies");
        try (Reader r = Files.newBufferedReader(cookiesPath, Charset.defaultCharset())) {
            Object o = new JSONParser().parse(r);
            con.request().cookies().putAll((Map) o);
        } catch (org.json.simple.parser.ParseException | ClassCastException e) {
            throw new IOException("Illegal data in cookies file " + cookiesPath, e);
        }//w w w . j  a v a2  s.  com
    }
}

From source file:org.apache.lucene.benchmark.byTask.tasks.WriteLineDocTaskTest.java

public void testLegalJustDate() throws Exception {
    Path file = getWorkDir().resolve("one-line");
    PerfRunData runData = createPerfRunData(file, false, LegalJustDateDocMaker.class.getName());
    WriteLineDocTask wldt = new WriteLineDocTask(runData);
    wldt.doLogic();/*from  w w w  .  ja va2 s. co m*/
    wldt.close();

    try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
        String line = br.readLine();
        assertHeaderLine(line);
        line = br.readLine();
        assertNotNull(line);
    }
}

From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java

private static void findWidgetVarUsages(Path sourceFile, WidgetVarLocation widgetVarLocation,
        BlockingQueue<WidgetVarLocation> foundUsages, BlockingQueue<WidgetVarLocation> skippedUsages,
        BlockingQueue<WidgetVarLocation> unusedOrAmbiguous) throws IOException {
    try (BufferedReader br = Files.newBufferedReader(sourceFile, StandardCharsets.UTF_8)) {
        int lineNr = 0;
        String line;//from w w  w  .  ja  v a 2 s . co  m
        while ((line = br.readLine()) != null) {
            lineNr++;

            int startIndex = 0;
            int endIndex = -1;

            while ((startIndex = line.indexOf(widgetVarLocation.widgetVar, endIndex + 1)) > -1) {
                endIndex = startIndex + widgetVarLocation.widgetVar.length();

                if (sourceFile.equals(widgetVarLocation.location) && lineNr == widgetVarLocation.lineNr
                        && startIndex == widgetVarLocation.columnNr) {
                    continue;
                }

                WidgetVarLocation usage = new WidgetVarLocation(widgetVarLocation.widgetVar, sourceFile, lineNr,
                        startIndex, line);

                // Only look at lines that use the word as a whole and not just as a part
                if ((startIndex == 0 || !Character.isJavaIdentifierStart(line.charAt(startIndex - 1)))
                        && (line.length() == endIndex
                                || !Character.isJavaIdentifierPart(line.charAt(endIndex)))) {
                    // We skip usages that occur as the last word of a line or usages that don't call methods directly
                    if (endIndex == line.length() || endIndex < line.length() && line.charAt(endIndex) != '.') {
                        skippedUsages.add(usage);
                    } else {
                        foundUsages.add(usage);
                    }
                } else {
                    skippedUsages.add(usage);
                }

                unusedOrAmbiguous.remove(widgetVarLocation);
            }
        }
    }
}

From source file:org.apache.lucene.benchmark.byTask.tasks.WriteLineDocTaskTest.java

public void testEmptyDoc() throws Exception {
    Path file = getWorkDir().resolve("one-line");
    PerfRunData runData = createPerfRunData(file, true, EmptyDocMaker.class.getName());
    WriteLineDocTask wldt = new WriteLineDocTask(runData);
    wldt.doLogic();//from ww  w . ja  va  2  s  .c  om
    wldt.close();

    try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
        String line = br.readLine();
        assertHeaderLine(line);
        line = br.readLine();
        assertNotNull(line);
    }
}