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:io.stallion.dataAccess.file.TextFilePersister.java

@Override
public T doFetchOne(File file) {
    Path path = FileSystems.getDefault().getPath(file.getAbsolutePath());
    BufferedReader reader = null;
    try {//from w  ww .j  a  va  2  s  .  com
        reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    int lineCount = 0;
    StringBuffer buffer = new StringBuffer();
    for (int i : safeLoop(50000)) {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (line == null) {
            break;
        }
        buffer.append(line + "\n");
        lineCount = i;
    }
    if (lineCount < 2) {
        return null;
    }
    String fileContent = buffer.toString();
    return fromString(fileContent, path);
}

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

@Test
public void CsvLoaderUnparsableExceptionTest() throws IOException {
    File f = new File(RESOURCES_DIRECTORY + "artificialWithAnomaly/art_daily_flatmiddle.csv");

    DateTimeFormatter wrongInputDateFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH-mm-ss");

    BufferedReader reader = Files.newBufferedReader(f.toPath(), ENCODING);
    try {/*  w w w .  ja v  a  2 s  . co  m*/
        TimeSeriesCsvLoader.load(reader, true, wrongInputDateFormat);
    } catch (RuntimeException e) {
        return;
    }

    Assert.fail("Should have failed with an UnparsableException exception");
}

From source file:com.strategicgains.docussandra.controller.perf.remote.PlayByPlayRemote.java

@Override
public List<Document> getDocumentsFromFS(int numToRead) throws IOException, ParseException {
    File file = new File(System.getProperty("user.home"), path);
    logger.info("Data path: " + file.getAbsolutePath());
    List<Document> toReturn = new ArrayList<>(numToRead);
    int counter = 0;
    synchronized (this) {
        numToRead = numToRead + position.intValue();
        try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
            String line = null;//from ww w. java2  s . c o  m
            while ((line = reader.readLine()) != null) {
                if (counter < position.intValue()) {
                    //read up to where we need to go. there has to be a better way to do this, however, not worth it right now
                    counter++;
                } else if (counter >= position.intValue() && counter < numToRead) {
                    //we have a section we care about
                    //so we add the document
                    Document doc = new Document();
                    doc.table(getTb());
                    doc.setUuid(new UUID(Long.MAX_VALUE - position.intValue(), 1));//give it a UUID that we will reconize
                    doc.object(line);
                    toReturn.add(doc);
                    position.addAndGet(1);//jump the postition
                    counter++;//and the counter
                } else {
                    logger.info("Exausted all documents in the PBP.json file for this chunk position: "
                            + position.get());
                    break;//we are done
                }

            }
        }
    }
    return toReturn;
}

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

@Test
@Ignore("too long")
public void testDetection() throws IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new DetectOutliers());
    testRunner.setProperty(DetectOutliers.ROTATION_POLICY_TYPE, "by_amount");
    testRunner.setProperty(DetectOutliers.ROTATION_POLICY_AMOUNT, "100");
    testRunner.setProperty(DetectOutliers.ROTATION_POLICY_UNIT, "points");
    testRunner.setProperty(DetectOutliers.CHUNKING_POLICY_TYPE, "by_amount");
    testRunner.setProperty(DetectOutliers.CHUNKING_POLICY_AMOUNT, "10");
    testRunner.setProperty(DetectOutliers.CHUNKING_POLICY_UNIT, "points");
    testRunner.setProperty(DetectOutliers.GLOBAL_STATISTICS_MIN, "-100000");
    testRunner.setProperty(DetectOutliers.MIN_AMOUNT_TO_PREDICT, "100");
    testRunner.setProperty(DetectOutliers.ZSCORE_CUTOFFS_NORMAL, "3.5");
    testRunner.setProperty(DetectOutliers.ZSCORE_CUTOFFS_MODERATE, "5");

    testRunner.setProperty(DetectOutliers.RECORD_VALUE_FIELD, "value");
    testRunner.setProperty(DetectOutliers.RECORD_TIME_FIELD, "timestamp");
    testRunner.assertValid();/*from ww  w . j ava2s.c o  m*/

    File f = new File(RESOURCES_DIRECTORY);

    Pair<Integer, Integer>[] results = new Pair[] { new Pair(4032, 124), new Pair(4032, 8), new Pair(4032, 315),
            new Pair(4032, 0), new Pair(4032, 29), new Pair(4032, 2442), new Pair(4032, 314),
            new Pair(4032, 296)

    };

    int count = 0;
    for (File file : FileUtils.listFiles(f, new SuffixFileFilter(".csv"), TrueFileFilter.INSTANCE)) {

        if (count >= results.length)
            break;

        BufferedReader reader = Files.newBufferedReader(file.toPath(), ENCODING);
        List<Record> records = TimeSeriesCsvLoader.load(reader, true, inputDateFormat);
        Assert.assertTrue(!records.isEmpty());

        testRunner.clearQueues();
        testRunner.enqueue(records.toArray(new Record[records.size()]));
        testRunner.run();
        testRunner.assertAllInputRecordsProcessed();

        System.out.println("records.size() = " + records.size());
        System.out.println("testRunner.getOutputRecords().size() = " + testRunner.getOutputRecords().size());
        testRunner.assertOutputRecordsCount(results[count].getSecond());
        count++;

    }

}

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

public void process() throws IOException, Exception {
    TcgaGSData.printWithFlag("Compilation::process - mClinicalDir=" + mClinicalDir);
    TcgaGSData.printWithFlag("Compilation::process - mInputFiles=" + mInputFiles);
    TcgaGSData.printWithFlag("Compilation::process - mOutputFile=" + mOutputFile);
    Collection<File> results = FileUtils.listFiles(new File(mClinicalDir),
            FileFilterUtils.nameFileFilter(mInputFiles), TrueFileFilter.INSTANCE);
    ArrayList<String> headers = getHeaders(results);
    TreeSet<String> patients = new TreeSet<>();
    TreeSet<String> lines = new TreeSet<>();
    String headerLine = null;/*from  ww w . j  a v  a 2s.c  o m*/
    for (String header : headers) {
        if (null == headerLine) {
            headerLine = header;
        } else {
            headerLine = headerLine + "\t" + header;
        }
    }
    boolean headersNeeded = true;
    for (File clinFile : results) {
        TcgaGSData.printWithFlag("Compilation::process - clinFile=" + clinFile.getAbsolutePath());
        try (BufferedReader br = Files.newBufferedReader(Paths.get(clinFile.getAbsolutePath()),
                Charset.availableCharsets().get("ISO-8859-1"))) {
            String line = br.readLine();
            ArrayList<String> currentHeaders = new ArrayList<>();
            currentHeaders.addAll(Arrays.asList(line.split("\t", -1)));
            for (line = br.readLine(); null != line; line = br.readLine()) {
                String newLine = null;
                String[] splitted = line.split("\t", -1);
                for (String header : headers) {
                    String token = "NA";
                    int index = currentHeaders.indexOf(header);
                    if (index > -1) {
                        token = splitted[index];
                    }
                    if (null == newLine) {
                        newLine = token;
                    } else {
                        newLine = newLine + "\t" + token;
                    }
                }
                lines.add(newLine);
                String patient = GSStringUtils.beforeTab(newLine);
                if (false == patients.add(patient)) {
                    throw new Exception("ERROR - patient duplicated " + patient);
                }
            }
        }
    }
    try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(mOutputFile),
            Charset.availableCharsets().get("ISO-8859-1"))) {
        bw.write(headerLine);
        bw.newLine();
        for (String line : lines) {
            bw.write(line);
            bw.newLine();
        }
    }
}

From source file:net.sourceforge.pmd.docs.SidebarGenerator.java

public Map<String, Object> loadSidebar() throws IOException {
    try (Reader reader = Files.newBufferedReader(sidebarPath, StandardCharsets.UTF_8)) {
        Yaml yaml = new Yaml();
        @SuppressWarnings("unchecked")
        Map<String, Object> sidebar = (Map<String, Object>) yaml.load(reader);
        return sidebar;
    }//from  w ww  . ja v  a 2s .c  om
}

From source file:org.nuxeo.datademo.RandomUSZips.java

/**
 * Private constructor to handle the singleton.
 * /* www.j av  a  2  s  .  c om*/
 * @throws IOException
 */
private RandomUSZips() throws IOException {

    zips = new ArrayList<String>();
    states = new ArrayList<String>();
    cities = new ArrayList<String>();
    latitudes = new ArrayList<Double>();
    longitudes = new ArrayList<Double>();

    int count = 0;
    File f;
    if (pathToDataFile != null) {
        f = new File(pathToDataFile);
        try (BufferedReader reader = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8)) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                count += 1;
                if (!line.isEmpty()) {
                    String[] elements = line.split("\t");
                    if (elements.length > 4) {
                        zips.add(elements[0]);
                        states.add(elements[1]);
                        cities.add(elements[2]);
                        latitudes.add(Double.valueOf(elements[3]));
                        longitudes.add(Double.valueOf(elements[4]));
                    } else {
                        log.error("Line #" + count + " does not contain at least 5 elements");
                    }
                } else {
                    log.error("Line #" + count + " is empty");
                }
            }
        }

    } else {
        String path = getClass().getResource("/files/US-zips.txt").getFile();
        f = new File(path);

        InputStream in = null;
        try {
            in = getClass().getResourceAsStream("/files/US-zips.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {

                count += 1;
                if (!line.isEmpty()) {
                    String[] elements = line.split("\t");
                    if (elements.length > 4) {
                        zips.add(elements[0]);
                        states.add(elements[1]);
                        cities.add(elements[2]);
                        latitudes.add(Double.valueOf(elements[3]));
                        longitudes.add(Double.valueOf(elements[4]));
                    } else {
                        log.error("Line #" + count + " does not contain at least 5 elements");
                    }
                } else {
                    log.error("Line #" + count + " is empty");
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }

    }
    maxForRandom = zips.size() - 1;

    statesAndIndices = new HashMap<String, ArrayList<Integer>>();
    for (int i = 0; i <= maxForRandom; i++) {

        String theState = states.get(i);
        ArrayList<Integer> indices = statesAndIndices.get(theState);
        if (indices == null) {
            indices = new ArrayList<Integer>();
        }
        indices.add(i);
        statesAndIndices.put(theState, indices);
    }

}

From source file:org.apache.tika.cli.TikaCLIBatchIntegrationTest.java

@Test
public void testJsonRecursiveBatchIntegration() throws Exception {
    String[] params = { "-i", testInputDirForCommandLine, "-o", tempOutputDirForCommandLine, "-numConsumers",
            "10", "-J", //recursive Json
            "-t" //plain text in content
    };//from   w w  w . j  a  va 2  s  .  c om
    TikaCLI.main(params);

    Path jsonFile = tempOutputDir.resolve("test_recursive_embedded.docx.json");
    try (Reader reader = Files.newBufferedReader(jsonFile, UTF_8)) {
        List<Metadata> metadataList = JsonMetadataList.fromJson(reader);
        assertEquals(12, metadataList.size());
        assertTrue(metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).contains("human events"));
    }
}

From source file:com.fpuna.preproceso.PreprocesoTS.java

/**
 * Metodo estatico que lee el archivo y lo carga en una estructura de hash
 *
 * @param Archivo path del archivo//from   ww  w . j  av a2s.c o  m
 * @return Hash con las sessiones leida del archivo de TrainigSet
 */
public static HashMap<String, SessionTS> leerArchivo(String Archivo, String sensor) {

    HashMap<String, SessionTS> SessionsTotal = new HashMap<String, SessionTS>();
    HashMap<String, String> actividades = new HashMap<String, String>();
    Path file = Paths.get(Archivo);

    if (Files.exists(file) && Files.isReadable(file)) {

        try {
            BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset());

            String line;
            int cabecera = 0;

            while ((line = reader.readLine()) != null) {
                if (line.contentEquals("statusId | label")) {

                    //Leo todos las actividades  
                    while ((line = reader.readLine()) != null
                            && !line.contentEquals("statusId|sensorName|value|timestamp")) {
                        String part[] = line.split("\\|");
                        actividades.put(part[0], part[1]);
                        SessionTS s = new SessionTS();
                        s.setActividad(part[1]);
                        SessionsTotal.put(part[0], s);
                    }
                    line = reader.readLine();
                }

                String lecturas[] = line.split("\\|");

                if (lecturas[1].contentEquals(sensor)) {
                    Registro reg = new Registro();
                    reg.setSensor(lecturas[1]);
                    String[] values = lecturas[2].split("\\,");

                    if (values.length == 3) {
                        reg.setValor_x(Double.parseDouble(values[0].substring(1)));
                        reg.setValor_y(Double.parseDouble(values[1]));
                        reg.setValor_z(Double.parseDouble(values[2].substring(0, values[2].length() - 1)));
                    } else if (values.length == 5) {
                        reg.setValor_x(Double.parseDouble(values[0].substring(1)));
                        reg.setValor_y(Double.parseDouble(values[1]));
                        reg.setValor_z(Double.parseDouble(values[2]));
                        reg.setM_1(Double.parseDouble(values[3]));
                        reg.setM_2(Double.parseDouble(values[4].substring(0, values[4].length() - 1)));
                    }

                    reg.setTiempo(new Timestamp(Long.parseLong(lecturas[3])));

                    SessionTS s = SessionsTotal.get(lecturas[0]);
                    s.addRegistro(reg);
                    SessionsTotal.replace(lecturas[0], s);
                }
            }
        } catch (IOException ex) {
            System.err.println("Okapu");
            Logger.getLogger(PreprocesoTS.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    return SessionsTotal;
}

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

public void writeBarcodeDataFile(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::writeBarcodeDataFile - start " + theOutputFile);
    TreeSet<String> processedBarcode = new TreeSet<>();
    try (BufferedReader br = Files.newBufferedReader(Paths.get(mMetadataFile),
            Charset.availableCharsets().get("ISO-8859-1"))) {
        try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(theOutputFile),
                Charset.availableCharsets().get("ISO-8859-1"))) {
            // read header/write header
            int indexId = -1;
            int indexData = -1;
            {//from   ww w .j a va  2  s .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);
            }
            bw.write("ID\tDATA");
            bw.newLine();
            //
            for (String line = br.readLine(); null != line; line = br.readLine()) {
                String[] splitted = line.split("\t", -1);
                bw.write(splitted[indexId] + "\t" + splitted[indexData]);
                processedBarcode.add(splitted[indexId]);
                bw.newLine();
            }
            TcgaGSData.printWithFlag("Metadata::writeBarcodeDataFile - processed file " + theOutputFile);
            for (File file : theDiseaseSamplesFiles) {
                TreeSet<String> barcodes = getDiseaseSampleData(file, true);
                for (String barcode : barcodes) {
                    if (false == processedBarcode.contains(barcode)) {
                        bw.write(barcode + "\t" + MetadataTcgaNames.M_UNKNOWN);
                        processedBarcode.add(barcode);
                        bw.newLine();
                    }
                }
            }
        }
    }
    TcgaGSData.printWithFlag("Metadata::writeBarcodeDataFile - finished");
}