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:divconq.util.IOUtil.java

/**
 * Read entire text file into a string./*from  w  w w .j a  va  2 s . com*/
 * 
 * @param file to read
 * @return file content if readable, otherwise null
 */
public static FuncResult<CharSequence> readEntireFile(Path file) {
    FuncResult<CharSequence> res = new FuncResult<>();

    BufferedReader br = null;

    try {
        StringBuilder sb = new StringBuilder();

        br = Files.newBufferedReader(file, Charset.forName("UTF-8"));

        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }

        res.setResult(sb);
    } catch (IOException x) {
        res.error("Unabled to read file " + file + ", error: " + x);
    } finally {
        closeQuietly(br);
    }

    return res;
}

From source file:org.zaproxy.zap.extension.jruby.VerifyScriptTemplates.java

@Override
protected void parseTemplate(Path template) throws Exception {
    try (Reader reader = Files.newBufferedReader(template, StandardCharsets.UTF_8)) {
        se.compile(reader);/*from   w w  w.  j a  v  a2s.  co m*/
    }
}

From source file:org.mda.bcb.tcgagsdata.create.Compilation.java

protected ArrayList<String> getHeaders(Collection<File> theFiles) throws IOException {
    TcgaGSData.printWithFlag("Compilation::getHeaders");
    ArrayList<String> headers = new ArrayList<>();
    for (File clinFile : theFiles) {
        try (BufferedReader br = Files.newBufferedReader(Paths.get(clinFile.getAbsolutePath()),
                Charset.availableCharsets().get("ISO-8859-1"))) {
            String line = br.readLine();
            String[] hdrs = line.split("\t", -1);
            for (String hdr : hdrs) {
                if ((null != hdr) && (false == "".equals(hdr))) {
                    if (false == headers.contains(hdr)) {
                        headers.add(hdr);
                    }/*from w w  w  . ja v  a2s.  co m*/
                }
            }
        }
    }
    return headers;
}

From source file:uk.ac.ebi.atlas.solr.admin.index.BioentityPropertiesStreamBuilder.java

public BioentityPropertiesStream build() throws IOException {
    Reader fileReader = Files.newBufferedReader(bioentityPropertiesFilePath, Charsets.UTF_8);
    CSVReader csvReader = new CSVReader(fileReader, '\t', CSVWriter.NO_QUOTE_CHARACTER);
    bioentityPropertiesBuilder.withIdentifierAsProperty(!isForReactome);
    return new BioentityPropertiesStream(csvReader, bioentityPropertiesBuilder, getSpecies());
}

From source file:com.hurence.logisland.processor.CsvLoaderTest.java

@Test
public void CsvLoaderTest() throws IOException {
    File f = new File(RESOURCES_DIRECTORY);

    for (File file : FileUtils.listFiles(f, new SuffixFileFilter(".csv"), TrueFileFilter.INSTANCE)) {
        BufferedReader reader = Files.newBufferedReader(file.toPath(), ENCODING);
        List<Record> records = TimeSeriesCsvLoader.load(reader, true, inputDateFormat);

        Assert.assertTrue(!records.isEmpty());
        //Assert.assertTrue("should be 4032, was : " + events.size(), events.size() == 4032);

        for (Record record : records) {
            Assert.assertTrue("should be sensors, was " + record.getType(), record.getType().equals("sensors"));
            Assert.assertTrue("should be 5, was " + record.getFieldsEntrySet().size(),
                    record.getFieldsEntrySet().size() == 5);
            Assert.assertTrue(record.getAllFieldNames().contains("timestamp"));
            if (!record.getAllFieldNames().contains("value"))
                System.out.println("stop");
            Assert.assertTrue(record.getAllFieldNames().contains("value"));

            Assert.assertTrue(record.getField("timestamp").getRawValue() instanceof Long);
            Assert.assertTrue(record.getField("value").getRawValue() instanceof Double);
        }//from w  ww  . j  av  a2s. c o m
    }
}

From source file:hrytsenko.gscripts.App.java

private static void executeCustomScript(GroovyShell shell, Path script) {
    Path scriptFilename = script.getFileName();
    LOGGER.info("Execute: {}.", scriptFilename);

    try (BufferedReader reader = Files.newBufferedReader(script, StandardCharsets.UTF_8)) {
        shell.evaluate(reader);//from  w w  w  . ja  v  a  2 s.c  o  m
    } catch (IOException exception) {
        throw new AppException(String.format("Cannot execute script %s.", scriptFilename), exception);
    }
}

From source file:com.intbit.util.ServletUtil.java

public static String getServerName(ServletContext context) {
    try {/*  ww  w  .  ja v a 2s.c om*/
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        String path = context.getRealPath("") + "/js/configurations.js";
        // read script file
        engine.eval(Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8));

        Invocable inv = (Invocable) engine;
        // call function from script file
        return inv.invokeFunction("getHost", "").toString();
    } catch (Exception ex) {
        return "http://clients.brndbot.com/BrndBot/";
    }
}

From source file:org.mda.bcb.tcgagsdata.create.Metadata.java

public void writePatientDataFile(String theIdColumn, String theDataColumn, String theOutputFile,
        File[] theDiseaseSamplesFiles) throws IOException {
    // TODO: theDiseaseSampleFile - disease in first column, rest of row is SAMPLE barcode
    TcgaGSData.printWithFlag("Metadata::writePatientDataFile - start " + theOutputFile);
    TreeMap<String, String> patientDisease = new TreeMap<>();
    try (BufferedReader br = Files.newBufferedReader(Paths.get(mMetadataFile),
            Charset.availableCharsets().get("ISO-8859-1"))) {
        // read header/write header
        int indexId = -1;
        int indexData = -1;
        {/* ww  w.  j a va 2s .  co  m*/
            String line = br.readLine();
            ArrayList<String> headerArray = new ArrayList<>();
            headerArray.addAll(Arrays.asList(line.split("\t", -1)));
            indexId = headerArray.indexOf(theIdColumn);
            indexData = headerArray.indexOf(theDataColumn);
        }
        //
        for (String line = br.readLine(); null != line; line = br.readLine()) {
            String[] splitted = line.split("\t", -1);
            patientDisease.put(trimToPatientId(splitted[indexId]), splitted[indexData]);
        }
        for (File file : theDiseaseSamplesFiles) {
            TreeSet<String> barcodes = getDiseaseSampleData(file, true);
            for (String barcode : barcodes) {
                String patientId = trimToPatientId(barcode);
                if (false == patientDisease.keySet().contains(patientId)) {
                    patientDisease.put(patientId, MetadataTcgaNames.M_UNKNOWN);
                }
            }
        }
    }
    try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(theOutputFile),
            Charset.availableCharsets().get("ISO-8859-1"))) {
        bw.write("ID\tDATA");
        bw.newLine();
        for (String key : patientDisease.keySet()) {
            bw.write(key + "\t" + patientDisease.get(key));
            bw.newLine();
        }
    }
    TcgaGSData.printWithFlag("Metadata::writePatientDataFile - finished " + theOutputFile);
}

From source file:com.vaushell.superpipes.transforms.done.T_Done.java

@Override
public void prepare() throws IOException {
    // Config//from   w w  w  .j  a  v a2  s .com
    path = getNode().getDispatcher().getDatas().resolve(Paths.get(getNode().getNodeID(), "done.dat"));

    Files.createDirectories(path.getParent());

    // Load previous ID
    if (Files.exists(path)) {
        try (final BufferedReader bfr = Files.newBufferedReader(path, Charset.forName("utf-8"))) {
            String line = bfr.readLine();
            while (line != null) {
                final int ind = line.indexOf(' ');
                if (ind < 0) {
                    ids.add(line);
                } else {
                    ids.add(line.substring(0, ind));
                }

                line = bfr.readLine();
            }
        }
    }

    // Load fields list
    final String fieldsStr = getProperties().getConfigString("fields", null);
    if (fieldsStr != null) {
        for (final String field : fieldsStr.split(",")) {
            final String cleanField = field.trim();
            if (!cleanField.isEmpty()) {
                fields.add(cleanField);
            }
        }
    }
}

From source file:eu.lunisolar.magma.doc.FileProcessor.java

public void processFile() throws IOException {
    BufferedReader reader = Files.newBufferedReader(file, UTF_8);

    enterContext(this::fileRootContext);

    String line;/*from   w  ww.  ja v  a 2s  .  c o  m*/
    while ((line = reader.readLine()) != null) {
        LineContext peek = stack.peek();
        peek.processLine(line);
    }

    out.flush();
    out.close();

    if (bout.size() > 0) {
        try (FileOutputStream newFile = new FileOutputStream(new File(outPath.toString()))) {
            newFile.write(bout.toByteArray());
        }
    }
}