Java Code Examples for java.nio.file.Files#newBufferedReader()

The following examples show how to use java.nio.file.Files#newBufferedReader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileTokenizer.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static List<String> getV1(Path path, Charset cs, String delimiter) throws IOException {

        if (path == null || delimiter == null) {
            throw new IllegalArgumentException("Path/delimiter cannot be null");
        }

        cs = Objects.requireNonNullElse(cs, StandardCharsets.UTF_8);

        String delimiterStr = Pattern.quote(delimiter);
        List<String> content = new ArrayList<>();
        try (BufferedReader br = Files.newBufferedReader(path, cs)) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] values = line.split(delimiterStr);
                content.addAll(Arrays.asList(values));
            }
        }

        return content;
    }
 
Example 2
Source File: FileTokenizer.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static List<String> getV2(Path path, Charset cs, String delimiter) throws IOException {

        if (path == null || delimiter == null) {
            throw new IllegalArgumentException("Path/delimiter cannot be null");
        }

        cs = Objects.requireNonNullElse(cs, StandardCharsets.UTF_8);

        String delimiterStr = Pattern.quote(delimiter);
        List<String> content = new ArrayList<>();
        try (BufferedReader br = Files.newBufferedReader(path, cs)) {
            String line;
            while ((line = br.readLine()) != null) {
                content.addAll(Stream.of(line.split(delimiterStr))
                        .collect(Collectors.toList()));
            }
        }

        return content;
    }
 
Example 3
Source File: CSVDataset.java    From djl-demo with Apache License 2.0 6 votes vote down vote up
CSVDataset build() throws IOException {
    Path path = Paths.get("dataset");
    Files.createDirectories(path);
    Path csvFile = path.resolve("malicious_url_data.csv");
    if (!Files.exists(csvFile)) {
        logger.info("Downloading dataset file ...");
        URL url =
                new URL(
                        "https://raw.githubusercontent.com/incertum/cyber-matrix-ai/master/Malicious-URL-Detection-Deep-Learning/data/url_data_mega_deep_learning.csv");
        Files.copy(url.openStream(), csvFile);
    }

    try (Reader reader = Files.newBufferedReader(csvFile);
            CSVParser csvParser =
                    new CSVParser(
                            reader,
                            CSVFormat.DEFAULT
                                    .withHeader("url", "isMalicious")
                                    .withFirstRecordAsHeader()
                                    .withIgnoreHeaderCase()
                                    .withTrim())) {
        dataset = csvParser.getRecords();
        return new CSVDataset(this);
    }
}
 
Example 4
Source File: ClassesListInFile.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process() {
    System.out.println("# list: " + root);
    if (!Files.exists(root)) {
        return;
    }
    try {
        try (BufferedReader reader = Files.newBufferedReader(root,
                StandardCharsets.UTF_8)) {
            String line;
            while (!isFinished() && ((line = reader.readLine()) != null)) {
                processClass(line);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: JBossLoggingPropertiesTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void logsTest() throws IOException {
    final String msg = "logTest: jboss-logging.properties message";
    final int statusCode = getResponse(msg, Collections.singletonMap("includeLevel", "true"));
    assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK);
    boolean trace = false;
    boolean fatal = false;
    String traceLine = msg + " - trace";
    String fatalLine = msg + " - fatal";
    try (final BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) {
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(traceLine)) {
                trace = true;
            }
            if (line.contains(fatalLine)) {
                fatal = true;
            }
        }
    }
    Assert.assertTrue("Log file should contain line: " + traceLine, trace);
    Assert.assertTrue("Log file should contain line: " + fatalLine, fatal);
}
 
Example 6
Source File: SimpleIniFileTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testStore() throws IOException {
  Path path = saveTestIni();
  assertTrue(Files.exists(path));
  assertTrue(Files.isRegularFile(path));

  try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    List<String> lines = br.lines().collect(Collectors.toList());
    assertEquals(8, lines.size());
    assertEquals("[section1]", lines.get(0));
    assertEquals("s1 = aaa", lines.get(1));
    assertEquals("s2 = bbb", lines.get(2));
    assertEquals("", lines.get(3));
    assertEquals("[section2]", lines.get(4));
    assertEquals("b1 = true", lines.get(5));
    assertEquals("b2 = false", lines.get(6));
    assertEquals("", lines.get(7));
  }
}
 
Example 7
Source File: AcronymExpansionsModel.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
@Override
protected AcronymExpansionsModel loadModel() throws BiomedicusException {
  LOGGER.info("Loading acronym expansions: {}", expansionsModelPath);
  Map<String, Collection<String>> expansions = new HashMap<>();
  Pattern splitter = Pattern.compile("\\|");
  try (BufferedReader bufferedReader = Files.newBufferedReader(expansionsModelPath)) {
    String acronym;
    while ((acronym = bufferedReader.readLine()) != null) {
      String[] acronymExpansions = splitter.split(bufferedReader.readLine());
      expansions.put(Acronyms.standardAcronymForm(acronym), Arrays.asList(acronymExpansions));
    }
  } catch (IOException e) {
    throw new BiomedicusException(e);
  }

  return new AcronymExpansionsModel(expansions);
}
 
Example 8
Source File: SymSpell.java    From JavaSymSpell with MIT License 5 votes vote down vote up
public boolean loadDictionary(String corpus, int termIndex, int countIndex) {
    File file = new File(corpus);
    if (!file.exists()) return false;

    BufferedReader br = null;
    try {
        br = Files.newBufferedReader(Paths.get(corpus), StandardCharsets.UTF_8);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
    if (br == null) {
        return false;
    }
    return loadDictionary(br, termIndex, countIndex);
}
 
Example 9
Source File: DataQualityProcessor.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the clean and dirty tuples in dv summary.
 *
 * @param dvReport the dv report
 * @param jumbuneRequest the jumbune request
 * @param dvName the dv name
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String setCleanTuplesInDVSummary(String dvReport, JumbuneRequest jumbuneRequest, String dvName) throws IOException {
	JsonObject reportObject = new JsonParser().parse(dvReport).getAsJsonObject();
	JsonObject dvSummaryObject = reportObject.get("DVSUMMARY").getAsJsonObject();

	String dirPath = JumbuneInfo.getHome() + Constants.JOB_JARS_LOC
			+ jumbuneRequest.getJobConfig().getJumbuneJobName() + File.separator + dvName
			+ "tuple" + File.separator;

	long totalTuples = 0;
	long cleanTuples = 0;
	long dirtyTuples = 0;

	File dir = new File(dirPath);
	File[] files = dir.listFiles();

	for (File file : files) {
		try (BufferedReader br = Files.newBufferedReader(Paths.get(file.getAbsolutePath()),
				StandardCharsets.UTF_8)) {
			if (file.getName().startsWith(mapTaskAttemptPrefix)) {
				totalTuples += Long.parseLong(br.readLine());
				cleanTuples += Long.parseLong(br.readLine());
			}

		}
	}
	dirtyTuples = totalTuples - cleanTuples;

	dvSummaryObject.add("dirtyTuples", Constants.gson.toJsonTree(dirtyTuples, Long.class));
	dvSummaryObject.add("cleanTuples", Constants.gson.toJsonTree(cleanTuples, Long.class));

	return reportObject.toString();
}
 
Example 10
Source File: GsonStorageHandler.java    From helper with MIT License 5 votes vote down vote up
@Override
protected T readFromFile(Path path) {
    try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
        return this.gson.fromJson(reader, this.type);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 11
Source File: DymolaImpactAnalysis.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
private void readSecurityIndexes(List<Contingency> contingencies, Path workingDir, ImpactAnalysisResult result) throws IOException {
    long start = System.currentTimeMillis();
    int files = 0;
    //TODO TSO INDEXES HANDLING
    for (int i = 0; i < contingencies.size(); i++) {
        Contingency contingency = contingencies.get(i);
        String prefixFile = DymolaUtil.DYMOLA_SIM_MAT_OUTPUT_PREFIX + "_" + i;
        for (String securityIndexFileName : Arrays.asList(
                prefixFile + WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME,
                prefixFile + WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME,
                prefixFile + WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME,
                prefixFile + WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME)) {
            Path file = workingDir.resolve(securityIndexFileName.replace(CommandConstants.EXECUTION_NUMBER_PATTERN, Integer.toString(i)));
            LOGGER.info("reading indexes output from file  {} ", file);
            if (Files.exists(file)) {
                try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
                    for (SecurityIndex index : SecurityIndexParser.fromXml(contingency.getId(), reader)) {
                        result.addSecurityIndex(index);
                    }
                }
                files++;
            }
        }
        //TODO
        // also scan errors in output
        //EurostagUtil.searchErrorMessage(workingDir.resolve(FAULT_OUT_GZ_FILE_NAME.replace(Command.EXECUTION_NUMBER_PATTERN, Integer.toString(i))), result.getMetrics(), i);
    }
    LOGGER.trace("{} security indexes files read in {} ms", files, System.currentTimeMillis() - start);
}
 
Example 12
Source File: FileSplitter.java    From topic-modeling-tool with Eclipse Public License 2.0 5 votes vote down vote up
public FileSplitter(Path path) {
    inputPath = path;

    try {
        inputReader = Files.newBufferedReader(
                inputPath,
                Charset.forName("UTF-8")
        );
    } catch (IOException exc) {
        System.out.println(inputPath.toString() + ": Error reading file");
        throw new RuntimeException(exc);
    }
}
 
Example 13
Source File: InfoHandler.java    From griffin with Apache License 2.0 5 votes vote down vote up
public InfoHandler() {
    try (BufferedReader br = Files.newBufferedReader(Paths.get(DirectoryCrawler.INFO_FILE),
                                                     StandardCharsets.UTF_8)) {
        LAST_PARSE_DATE = br.readLine();
    }
    catch (IOException ex) {
        Logger.getLogger(InfoHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example 14
Source File: FeaturePackLayout.java    From galleon with Apache License 2.0 5 votes vote down vote up
public FeaturePackSpec getSpec() throws ProvisioningException {
    if(spec == null) {
        try(BufferedReader reader = Files.newBufferedReader(dir.resolve(Constants.FEATURE_PACK_XML))) {
            spec = FeaturePackXmlParser.getInstance().parse(reader);
        } catch (Exception e) {
            throw new ProvisioningException(Errors.readFile(dir.resolve(Constants.FEATURE_PACK_XML)));
        }
    }
    return spec;
}
 
Example 15
Source File: JSONModelUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Same as {@link #parseJSON(String)}, but reads the source from a file on disk at the given path.
 */
public static JSONDocument loadJSON(Path path, Charset cs) throws IOException {
	try (BufferedReader in = Files.newBufferedReader(path, cs)) {
		ParseResult<JSONDocument> result = N4LanguageUtils.parseXtextLanguage(FILE_EXTENSION, null,
				JSONDocument.class, in);
		return result.errors.isEmpty() ? result.ast : null;
	}
}
 
Example 16
Source File: AnalyzerProfile.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static String getAnalysisDataDir(Path propFile) {
  Properties prop = new Properties();
  try (BufferedReader reader = Files.newBufferedReader(propFile, StandardCharsets.UTF_8)) {
    prop.load(reader);
    return prop.getProperty("analysis.data.dir", "");
  } catch (IOException e) {
    return "";
  }
}
 
Example 17
Source File: IMDBTest.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
public BufferedReader getFileReader(final String filepath) {
	Path path = Paths.get(filepath);
	if(Files.exists(path)) {
		try {
			return Files.newBufferedReader(path, StandardCharsets.UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	} else {
		return null;
	}
}
 
Example 18
Source File: Main.java    From softwarecave with GNU General Public License v3.0 5 votes vote down vote up
private static void bufferedReader2(String fileName)
        throws IOException {
    System.out.print("\nbufferedReader2:\n");
    Path path = Paths.get(fileName);
    try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    }
}
 
Example 19
Source File: FeatureGroupParsingTestCase.java    From galleon with Apache License 2.0 4 votes vote down vote up
private static FeatureGroup parseConfig(String xml) throws Exception {
    final Path path = getResource("xml/config/" + xml);
    try (BufferedReader reader = Files.newBufferedReader(path)) {
        return FeatureGroupXmlParser.getInstance().parse(reader);
    }
}
 
Example 20
Source File: ObservationDao.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
private static Observation find(String satelliteId, Path curDirectory) {
	Path dest = curDirectory.resolve(META_FILENAME);
	if (!Files.exists(dest)) {
		return null;
	}
	Observation full;
	try (BufferedReader r = Files.newBufferedReader(dest)) {
		JsonObject meta = Json.parse(r).asObject();
		full = Observation.fromJson(meta);
	} catch (Exception e) {
		LOG.error("unable to load meta from {}", dest, e);
		return null;
	}

	Path a = curDirectory.resolve(IMAGE_FILENAME);
	if (Files.exists(a)) {
		full.setImagePath(a.toFile());
		full.setaURL("/api/v1/admin/static/satellites/" + satelliteId + "/data/" + full.getId() + "/" + IMAGE_FILENAME);
	}
	Path data = curDirectory.resolve(DATA_FILENAME);
	if (Files.exists(data)) {
		full.setDataPath(data.toFile());
		full.setDataURL("/api/v1/admin/static/satellites/" + satelliteId + "/data/" + full.getId() + "/" + DATA_FILENAME);
	}
	Path wav = curDirectory.resolve(OUTPUT_WAV_FILENAME);
	if (Files.exists(wav)) {
		full.setRawPath(wav.toFile());
		full.setRawURL("/api/v1/admin/static/satellites/" + satelliteId + "/data/" + full.getId() + "/" + OUTPUT_WAV_FILENAME);
	} else {
		Path tarGz = curDirectory.resolve(OUTPUT_RAW_FILENAME);
		if (Files.exists(tarGz)) {
			full.setRawPath(tarGz.toFile());
			full.setRawURL("/api/v1/admin/static/satellites/" + satelliteId + "/data/" + full.getId() + "/" + OUTPUT_RAW_FILENAME);
		}
	}
	Path spectogram = curDirectory.resolve(SPECTOGRAM_FILENAME);
	if (Files.exists(spectogram)) {
		full.setSpectogramPath(spectogram.toFile());
		full.setSpectogramURL("/api/v1/admin/static/satellites/" + satelliteId + "/data/" + full.getId() + "/" + SPECTOGRAM_FILENAME);
	}

	return full;
}