List of usage examples for java.nio.file Files newBufferedReader
public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException
From source file:org.jboss.as.test.integration.security.auditing.CustomAuditProviderModuleTest.java
@Test public void testGooduser1WithBadPassword() throws Exception { assertResponse(CustomLoginModule1.GOODUSER1_USERNAME, "bogus", 401); try (BufferedReader r = Files.newBufferedReader(AUDIT_LOG_PATH, UTF_8)) { assertAuditLog(r, Pattern.quote("INFO " + CustomAuditProviderModule.class.getName() + " [Failure]")); }// ww w .ja v a 2 s . c om }
From source file:com.themodernway.server.core.io.IO.java
public static final BufferedReader toBufferedReader(final Path path) throws IOException { return Files.newBufferedReader(CommonOps.requireNonNull(path), UTF_8_CHARSET); }
From source file:de.upb.timok.run.GenericSmacPipeline.java
private void splitTrainTestFile(String timedInputFile, String timedInputTrainFile, String timedInputTestFile, double trainPercentage, double testPercentage, double anomalyPercentage, boolean isRti) throws IOException { logger.info("TimedInputFile=" + timedInputFile); final File f = new File(timedInputFile); System.out.println(f);//from www. j a v a 2 s.c o m final LineNumberReader lnr = new LineNumberReader(new FileReader(timedInputFile)); lnr.skip(Long.MAX_VALUE); int samples = lnr.getLineNumber(); lnr.close(); final int trainingSamples = (int) (samples * trainPercentage); final int testSamples = (int) (samples * testPercentage); final int anomalies = (int) (anomalyPercentage * testSamples); final int writtenTrainingSamples = 0; final int writtenTestSamples = 0; int insertedAnomalies = 0; final BufferedReader br = Files.newBufferedReader(Paths.get(timedInputFile), StandardCharsets.UTF_8); String line = null; final BufferedWriter trainWriter = Files.newBufferedWriter(Paths.get(timedInputTrainFile), StandardCharsets.UTF_8); final BufferedWriter testWriter = Files.newBufferedWriter(Paths.get(timedInputTestFile), StandardCharsets.UTF_8); final Random r = new Random(MasterSeed.nextLong()); final Random mutation = new Random(MasterSeed.nextLong()); boolean force = false; int lineIndex = 0; int linesLeft; int anomaliesToInsert; if (isRti) { br.readLine(); samples--; } while ((line = br.readLine()) != null) { if (writtenTrainingSamples < trainingSamples && writtenTestSamples < testSamples) { // choose randomly according to train/test percentage if (r.nextDouble() > testPercentage) { // write to train writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter); } else { // write to test insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line, testWriter, mutation, force); } } else if (writtenTrainingSamples >= trainingSamples) { insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line, testWriter, mutation, force); } else if (writtenTestSamples >= testSamples) { // only write trainSamples from now on writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter); } lineIndex++; linesLeft = samples - lineIndex; anomaliesToInsert = anomalies - insertedAnomalies; if (linesLeft <= anomaliesToInsert) { force = true; } } br.close(); trainWriter.close(); testWriter.close(); }
From source file:br.bireme.ngrams.NGrams.java
/** * * @param index//from w w w . j a v a 2s. com * @param schema * @param inFile * @param inFileEncoding * @param outFile * @param outFileEncoding * @throws IOException * @throws ParseException */ public static void search(final NGIndex index, final NGSchema schema, final String inFile, final String inFileEncoding, final String outFile, final String outFileEncoding) throws IOException, ParseException { if (index == null) { throw new NullPointerException("index"); } if (schema == null) { throw new NullPointerException("schema"); } if (inFile == null) { throw new NullPointerException("inFile"); } if (inFileEncoding == null) { throw new NullPointerException("inFileEncoding"); } if (outFile == null) { throw new NullPointerException("outFile"); } if (outFileEncoding == null) { throw new NullPointerException("outFileEncoding"); } final Charset inCharset = Charset.forName(inFileEncoding); final Charset outCharset = Charset.forName(outFileEncoding); final IndexSearcher searcher = index.getIndexSearcher(); final NGAnalyzer analyzer = (NGAnalyzer) index.getAnalyzer(); final Parameters parameters = schema.getParameters(); final NGramDistance ngDistance = new NGramDistance(analyzer.getNgramSize()); final Set<String> id_id = new HashSet<>(); int cur = 0; try (final BufferedReader reader = Files.newBufferedReader(new File(inFile).toPath(), inCharset); final BufferedWriter writer = Files.newBufferedWriter(new File(outFile).toPath(), outCharset)) { writer.append("rank|similarity|search_doc_id|index_doc_id|" + "ngram_search_text|ngram_index_text|search_source|" + "index_source\n"); final Set<Result> results = new HashSet<>(); while (true) { final String line = reader.readLine(); if (line == null) { break; } if (++cur % 250 == 0) { System.out.println("<<< " + cur); } results.clear(); final String tline = line.replace(':', ' ').trim(); if (!tline.isEmpty()) { final String[] split = tline.split(" *\\| *", Integer.MAX_VALUE); if (split.length != parameters.nameFields.size()) { throw new IOException("invalid number of fields: " + line); } searchRaw(parameters, searcher, analyzer, ngDistance, tline, true, id_id, results); if (!results.isEmpty()) { writeOutput(parameters, results, writer); } } } searcher.getIndexReader().close(); } }
From source file:org.eclipse.winery.repository.backend.filebased.FilebasedRepository.java
@Override public Configuration getConfiguration(RepositoryFileReference ref) { Path path = this.ref2AbsolutePath(ref); PropertiesConfiguration configuration = new PropertiesConfiguration(); if (Files.exists(path)) { try (Reader r = Files.newBufferedReader(path, Charset.defaultCharset())) { configuration.load(r);/* ww w . ja va 2s . c om*/ } catch (ConfigurationException | IOException e) { FilebasedRepository.logger.error("Could not read config file", e); throw new IllegalStateException("Could not read config file", e); } } configuration.addConfigurationListener(new AutoSaveListener(path, configuration)); // We do NOT implement reloading as the configuration is only accessed // in JAX-RS resources, which are created on a per-request basis return configuration; }
From source file:org.mda.bcb.tcgagsdata.create.ProcessFile.java
protected void processFile(File theFile, String theDisease) throws IOException { TcgaGSData.printWithFlag("ProcessFile::processFile - Start"); TcgaGSData//w w w .java2 s. c o m .printWithFlag("ProcessFile::processFile - theFile.getAbsolutePath()=" + theFile.getAbsolutePath()); Double pointOnePercentile = getPointOnePercentile(theFile.getParentFile()); try (BufferedReader br = Files.newBufferedReader(Paths.get(theFile.getAbsolutePath()), Charset.availableCharsets().get("ISO-8859-1"))) { // first line samples String line = br.readLine(); TcgaGSData.printWithFlag("ProcessFile::processFile - populateSampleLists"); populateSampleLists(theDisease, GSStringUtils.afterTab(line).split("\t", -1)); // do rest int nextIndex = 0; line = br.readLine(); int lineCnt = 0; TcgaGSData.printWithFlag("ProcessFile::processFile - before lines"); while (null != line) { String geneEq = GSStringUtils.beforeTab(line); String data = GSStringUtils.afterTab(line); nextIndex = populateGeneAndData(geneEq, data.split("\t", -1), mCurrentColumn, pointOnePercentile); line = br.readLine(); lineCnt = lineCnt + 1; if (0 == (lineCnt % 1000)) { System.out.print(" " + lineCnt); } if (0 == (lineCnt % 10000)) { TcgaGSData.printWithFlag(""); } } TcgaGSData.printWithFlag(" -"); TcgaGSData.printWithFlag("ProcessFile::processFile - after lines"); mCurrentColumn = nextIndex; } TcgaGSData.printWithFlag("ProcessFile::processFile - Finish"); }
From source file:org.caleydo.data.importer.tcga.FirehoseProvider.java
private static File parseMAF(File maf) { File out = new File(maf.getParentFile(), "P" + maf.getName()); if (out.exists()) return out; log.fine(maf.getAbsolutePath() + " parsing maf file"); final String TAB = "\t"; try (BufferedReader reader = Files.newBufferedReader(maf.toPath(), Charset.forName("UTF-8"))) { List<String> header = Arrays.asList(reader.readLine().split(TAB)); int geneIndex = header.indexOf("Hugo_Symbol"); int sampleIndex = header.indexOf("Tumor_Sample_Barcode"); // gene x sample x mutated Table<String, String, Boolean> mutated = TreeBasedTable.create(); String line = null;/*www . j av a 2 s . co m*/ while ((line = reader.readLine()) != null) { String[] columns = line.split(TAB); mutated.put(columns[geneIndex], columns[sampleIndex], Boolean.TRUE); } File tmp = new File(out.getParentFile(), out.getName() + ".tmp"); PrintWriter w = new PrintWriter(tmp); w.append("Hugo_Symbol"); List<String> cols = new ArrayList<>(mutated.columnKeySet()); for (String sample : cols) { w.append(TAB).append(sample); } w.println(); Set<String> rows = mutated.rowKeySet(); for (String gene : rows) { w.append(gene); for (String sample : cols) { w.append(TAB).append(mutated.contains(gene, sample) ? '1' : '0'); } w.println(); } w.close(); Files.move(tmp.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING); log.fine(maf.getAbsolutePath() + " parsed maf file stats: " + mutated.size() + " " + rows.size() + " " + cols.size()); return out; } catch (IOException e) { log.log(Level.SEVERE, maf.getAbsolutePath() + " maf parsing error: " + e.getMessage(), e); } return null; }
From source file:popgenutils.dfcp.PrepareVCF4DFCP.java
/** * /*from ww w .ja v a 2 s . c om*/ */ private void mask() { StringBuilder header = new StringBuilder(); try (BufferedReader in = Files.newBufferedReader(Paths.get(filename), Charset.forName("UTF-8"))) { BufferedWriter out = 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")) { //samples begin at 9 number_of_individuals = line.split("\t").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 (pmask >= 0) { // convert pgeno into ageno amask = (int) Math.ceil(number_of_individuals * pmask); } for (int i = 0; i < amask; i++) { individuals_to_genotype.add(index_of_individuals.get(i)); } out = Files.newBufferedWriter( Paths.get(output_dir + "/" + amask + "_mask" + Paths.get(filename).getFileName()), Charset.forName("UTF-8")); out.write(header.toString()); out.write(line + 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]); for (int i = 1; i < parts.length; i++) { if (i > 8) { parts2 = parts[i].split(":"); if (gt_pos == 0 && individuals_to_genotype.contains(i - 8)) { out.write("\t" + maskAlleles(parts2[0])); } else out.write(parts2[0]); for (int j = 1; j < parts2.length; j++) { if (gt_pos == j && individuals_to_genotype.contains(i - 8)) { out.write(":" + maskAlleles(parts2[i])); } else out.write(":" + parts2[i]); } } else { out.write("\t" + parts[i]); } } out.write(System.getProperty("line.separator")); } } } catch (IOException e) { System.err.println("could not read from file " + filename); e.printStackTrace(); } }
From source file:org.apache.asterix.extension.grammar.GrammarExtensionMojo.java
private void processBase() throws MojoExecutionException { try (BufferedReader reader = Files.newBufferedReader(Paths.get(base, gbase), StandardCharsets.UTF_8)) { StringBuilder identifier = new StringBuilder(); while ((position.line = reader.readLine()) != null) { if (position.line.trim().startsWith("//")) { // skip comments continue; }/*from w w w.j a va 2s. c o m*/ String[] tokens = position.line.split(REGEX_WS_PAREN); position.index = 0; int openBraceIndex = position.line.indexOf(OPEN_BRACE); int openAngularIndex = position.line.trim().indexOf(OPEN_ANGULAR); if (tokens.length > 0 && identifier.length() == 0 && KEYWORDS.contains(tokens[0])) { handleSpecialToken(tokens[0], reader); } else if (openBraceIndex >= 0 && openAngularIndex < 0) { String beforeBrace = position.line.substring(0, openBraceIndex); if (beforeBrace.trim().length() > 0) { identifier.append(beforeBrace); } else if (identifier.length() == 0) { identifier.append(lastIdentifier); } position.index = openBraceIndex; readBlock(reader, OPEN_BRACE, CLOSE_BRACE); // if next non-white space character is an open brace, then we need to append addExtensibleProduction(identifier); } else if (openAngularIndex == 0) { position.index = position.line.indexOf(OPEN_ANGULAR); readFinalProduction(identifier, reader); addFinalProduction(identifier, baseFinals); } else if (identifier.length() > 0 || position.line.trim().length() > 0) { identifier.append(position.line); identifier.append('\n'); } } } catch (Exception e) { getLog().error(e); throw new MojoExecutionException(e.getMessage(), e); } }
From source file:misc.FileHandler.java
/** * Returns the synchronization data, including the owner name, the server * path and the version number or <code>null</code>, if sync data cannot be * retrieved.// w ww .ja v a 2 s . c om * * @param clientRoot * the complete path to the client's root directory. Must exist. * @param syncRoot * the complete path to the synchronization root directory. Must * exist. * @param fileName * a valid file name which may be inexistent. Must be relative to * <code>clientRoot</code>. * @return the synchronization data, including the owner name, the server * path and the version number or <code>null</code>, if sync data * cannot be retrieved. */ public static GetSyncData getSyncData(Path clientRoot, Path syncRoot, Path fileName) { if ((clientRoot == null) || !Files.isDirectory(clientRoot)) { throw new IllegalArgumentException("clientRoot must be an existing directory!"); } if ((syncRoot == null) || !Files.isDirectory(syncRoot)) { throw new IllegalArgumentException("syncRoot must be an existing directory!"); } if (!isFileName(fileName)) { throw new IllegalArgumentException("fileName must be a valid file name!"); } GetSyncData result = null; Path accessBundleDirectory = FileHandler.getAccessBundleDirectory(clientRoot, fileName); if (accessBundleDirectory != null) { int version = 0; Path versionFile = Paths.get(syncRoot.toString(), accessBundleDirectory.toString(), SynchronizationExecutor.VERSION_FILE); // Parse the version file, if it exists. // Otherwise, we are at version 0. if (Files.exists(versionFile)) { try (BufferedReader reader = Files.newBufferedReader(versionFile, Coder.CHARSET);) { String read = reader.readLine(); version = Integer.parseInt(read); } catch (IOException | NumberFormatException e) { Logger.logError(e); } } // Parse the access bundle. if (ROOT_PATH.equals(accessBundleDirectory)) { // We deal with an owner access bundle. result = new GetSyncData(null, accessBundleDirectory, version); } else { // Get owner and server directory from the group access // bundle. Path bundlePath = Paths.get(clientRoot.toString(), accessBundleDirectory.toString(), AccessBundle.ACCESS_BUNDLE_FILENAME); try { GroupAccessBundle accessBundle = GroupAccessBundle.parse(bundlePath); if (accessBundle != null) { String owner = accessBundle.getOwner(); String serverPath = accessBundle.getFolder(); result = new GetSyncData(owner, Paths.get(serverPath).normalize(), version); } else { Logger.logError( String.format("Cannot parse group access bundle %s", bundlePath.toString())); } } catch (IOException | ParseException e) { Logger.logError(e); } } } return result; }