List of usage examples for java.nio.file Files newBufferedReader
public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException
From source file:eu.itesla_project.dymola.DymolaImpactAnalysis.java
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(Command.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); }/* w ww . j a v a 2s .c o m*/ } 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)); }
From source file:popgenutils.dfcp.PrepareVCF4DFCP.java
/** * //www.j a v a 2 s .c om */ private void filterhet() { StringBuilder header = new StringBuilder(); try (BufferedReader in = Files.newBufferedReader(Paths.get(filename), Charset.forName("UTF-8"))) { BufferedWriter out = null; String line = null; while ((line = in.readLine()) != null) { if (line.startsWith("#CHROM")) { //samples begin at 9 out = Files.newBufferedWriter( Paths.get( output_dir + "/" + hetindex + "_hetfilter" + 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; } parts2 = parts[9 + hetindex].split("\\||/"); if (!parts2[0].equals(parts2[1])) out.write(line + System.getProperty("line.separator")); } } out.close(); } catch (IOException e) { System.err.println("could not read from file " + filename); e.printStackTrace(); } }
From source file:sadl.models.PDFA.java
public PDFA(Path trebaPath, TimedInput trainingSequences) throws IOException { try (BufferedReader inputReader = Files.newBufferedReader(trebaPath, StandardCharsets.UTF_8)) { this.alphabet = trainingSequences; 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 (alphabet == null) { symbol = lineSplit[2]; } else { symbol = trainingSequences.getSymbol(Integer.parseInt(lineSplit[2])); }/*from ww w . ja v a2s . c om*/ 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); } } inputReader.close(); } }
From source file:org.wso2.carbon.pc.core.transfer.ProcessImport.java
/** * Add the process associations (sub processes, successors, predecessors) to the registry * * @param processName name of the process * @param processVersion process version * @param processDirPath process directory path * @param processAssetPath process path//from ww w .ja v a 2 s . c o m * @throws ProcessCenterException */ private void setProcessAssociations(String processName, String processVersion, String processDirPath, String processAssetPath) throws ProcessCenterException { try { Path procAssociationsJSONFilePath = Paths .get(processDirPath + "/" + ProcessCenterConstants.PROCESS_ASSOCIATIONS_FILE); if (Files.exists(procAssociationsJSONFilePath)) { String procAssociationsFileContent = ""; try (BufferedReader reader = Files.newBufferedReader(procAssociationsJSONFilePath, Charsets.US_ASCII)) { String line; while ((line = reader.readLine()) != null) { procAssociationsFileContent += line; } } catch (IOException e) { String errMsg = "Error in reading process's process_associations.json file"; throw new IOException(errMsg, e); } JSONObject procAssociations = new JSONObject(procAssociationsFileContent); //set sub process JSONArray subProcesses = procAssociations.getJSONArray(ProcessCenterConstants.SUBPROCESSES); for (int i = 0; i < subProcesses.length(); i++) { JSONObject subProcess = subProcesses.getJSONObject(i); String subProcessPath = subProcess.getString("path"); addSubProcessAssociation(processAssetPath, subProcessPath); } //set successor process JSONArray successors = procAssociations.getJSONArray(ProcessCenterConstants.SUCCESSORS); for (int i = 0; i < successors.length(); i++) { JSONObject successor = successors.getJSONObject(i); String successorPath = successor.getString("path"); addSuccessorAssociation(processAssetPath, successorPath); } //set predecessor process associations JSONArray predecessors = procAssociations.getJSONArray(ProcessCenterConstants.PREDECESSORS); for (int i = 0; i < predecessors.length(); i++) { JSONObject predecessor = predecessors.getJSONObject(i); String predecessorPath = predecessor.getString("path"); addPredecessorAssociation(processAssetPath, predecessorPath); } } } catch (IOException | RegistryException | JSONException e) { String errMsg = "Error in setting process associations for the process :" + processName + "-" + processVersion; log.error(errMsg, e); throw new ProcessCenterException(errMsg, e); } }
From source file:it.tidalwave.bluemarine2.persistence.impl.DefaultPersistenceTest.java
/******************************************************************************************************************* * ******************************************************************************************************************/ private static void createPreviousStorageFrom(final @Nonnull Path path) throws Exception { final Sail sail = new NativeStore(TEST_STORAGE_FOLDER.toFile()); final Repository repository = new SailRepository(sail); repository.initialize();//from ww w . j a v a 2s . co m try (final RepositoryConnection connection = repository.getConnection(); final Reader reader = Files.newBufferedReader(TEST_IMPORT_FILE, UTF_8)) { connection.add(reader, TEST_IMPORT_FILE.toUri().toString(), RDFFormat.N3); connection.commit(); } repository.shutDown(); }
From source file:de.huberlin.wbi.cuneiform.core.cre.LocalThread.java
@Override public void run() { Path scriptFile, location, successMarker, reportFile, callLocation, stdErrFile, stdOutFile; // Path lockMarker; Process process;//w w w .j a v a 2 s.co m int exitValue; Set<JsonReportEntry> report; JsonReportEntry entry; String line; StringBuffer buf; Path srcPath, destPath; ProcessBuilder processBuilder; Ticket ticket; String script, stdOut, stdErr; long tic, toc; JSONObject obj; Message msg; Charset cs; int trial; boolean suc; Exception ex; if (log.isDebugEnabled()) log.debug("Starting up local thread for ticket " + invoc.getTicketId() + "."); if (invoc == null) throw new NullPointerException("Invocation must not be null."); ticket = invoc.getTicket(); process = null; stdOut = null; stdErr = null; // lockMarker = null; script = null; successMarker = null; cs = Charset.forName("UTF-8"); try { callLocation = Paths.get(System.getProperty("user.dir")); location = buildDir.resolve(String.valueOf(invoc.getTicketId())); // lockMarker = location.resolve( Invocation.LOCK_FILENAME ); successMarker = location.resolve(Invocation.SUCCESS_FILENAME); reportFile = location.resolve(Invocation.REPORT_FILENAME); script = invoc.toScript(); // if( Files.exists( lockMarker ) ) // throw new IOException( "Lock held on ticket "+invoc.getTicketId() ); if (!Files.exists(successMarker)) { deleteIfExists(location); Files.createDirectories(location); // Files.createFile( lockMarker ); scriptFile = invoc.getExecutablePath(location); Files.createFile(scriptFile, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-x---"))); // write executable script try (BufferedWriter writer = Files.newBufferedWriter(scriptFile, cs, StandardOpenOption.CREATE)) { writer.write(script); } // write executable log entry try (BufferedWriter writer = Files.newBufferedWriter(reportFile, cs, StandardOpenOption.CREATE)) { writer.write(ticket.getExecutableLogEntry().toString()); writer.write('\n'); } for (String filename : invoc.getStageInList()) { if (filename.charAt(0) == '/') throw new UnsupportedOperationException("Absolute path encountered '" + filename + "'."); srcPath = centralRepo.resolve(filename); destPath = location.resolve(filename); if (!Files.exists(srcPath)) { srcPath = callLocation.resolve(filename); if (log.isTraceEnabled()) log.trace("Resolving relative path '" + srcPath + "'."); } else if (log.isTraceEnabled()) log.trace("Resolving path to central repository '" + srcPath + "'."); if (log.isTraceEnabled()) log.trace("Trying to create symbolic link from '" + srcPath + "' to '" + destPath + "'."); if (!Files.exists(destPath.getParent())) Files.createDirectories(destPath.getParent()); Files.createSymbolicLink(destPath, srcPath); } // run script processBuilder = new ProcessBuilder(invoc.getCmd()); processBuilder.directory(location.toFile()); stdOutFile = location.resolve(Invocation.STDOUT_FILENAME); stdErrFile = location.resolve(Invocation.STDERR_FILENAME); processBuilder.redirectOutput(stdOutFile.toFile()); processBuilder.redirectError(stdErrFile.toFile()); trial = 1; suc = false; ex = null; tic = System.currentTimeMillis(); do { try { process = processBuilder.start(); suc = true; } catch (IOException e) { ex = e; if (log.isWarnEnabled()) log.warn("Unable to start process on trial " + (trial++) + " Waiting " + WAIT_INTERVAL + "ms: " + e.getMessage()); Thread.sleep(WAIT_INTERVAL); } } while (suc == false && trial <= MAX_TRIALS); if (process == null) { ticketSrc.sendMsg(new TicketFailedMsg(cre, ticket, ex, script, null, null)); // Files.delete( lockMarker ); return; } exitValue = process.waitFor(); toc = System.currentTimeMillis(); try (BufferedWriter writer = Files.newBufferedWriter(reportFile, cs, StandardOpenOption.APPEND)) { obj = new JSONObject(); obj.put(JsonReportEntry.LABEL_REALTIME, toc - tic); entry = invoc.createJsonReportEntry(tic, JsonReportEntry.KEY_INVOC_TIME, obj); writer.write(entry.toString()); writer.write('\n'); try (BufferedReader reader = Files.newBufferedReader(stdOutFile, cs)) { buf = new StringBuffer(); while ((line = reader.readLine()) != null) buf.append(line).append('\n'); stdOut = buf.toString(); if (!stdOut.isEmpty()) { entry = invoc.createJsonReportEntry(JsonReportEntry.KEY_INVOC_STDOUT, stdOut); writer.write(entry.toString()); writer.write('\n'); } } try (BufferedReader reader = Files.newBufferedReader(stdErrFile, cs)) { buf = new StringBuffer(); while ((line = reader.readLine()) != null) buf.append(line).append('\n'); stdErr = buf.toString(); if (!stdErr.isEmpty()) { entry = invoc.createJsonReportEntry(JsonReportEntry.KEY_INVOC_STDERR, stdErr); writer.write(entry.toString()); writer.write('\n'); } } if (exitValue == 0) Files.createFile(successMarker); else { ticketSrc.sendMsg(new TicketFailedMsg(cre, ticket, null, script, stdOut, stdErr)); // Files.delete( lockMarker ); return; } } } // gather report report = new HashSet<>(); try (BufferedReader reader = Files.newBufferedReader(reportFile, cs)) { while ((line = reader.readLine()) != null) { line = line.trim(); if (line.isEmpty()) continue; entry = new JsonReportEntry(line); // If the report comes from the hard cache then the run id // is different from the run id of this invocation. This is // corrected here. entry.setRunId(invoc.getRunId()); report.add(entry); } } invoc.evalReport(report); // create link in central data repository for (String f : invoc.getStageOutList()) { srcPath = location.resolve(f); destPath = centralRepo.resolve(f); if (Files.exists(destPath)) continue; if (log.isTraceEnabled()) log.trace("Creating link from " + srcPath + " to " + destPath + "."); Files.createSymbolicLink(destPath, srcPath); } ticketSrc.sendMsg(new TicketFinishedMsg(cre, invoc.getTicket(), report)); if (log.isTraceEnabled()) log.trace("Local thread ran through without exception."); // Files.deleteIfExists( lockMarker ); } catch (InterruptedException e) { if (log.isTraceEnabled()) log.trace("Local thread has been interrupted."); } catch (Exception e) { if (log.isTraceEnabled()) log.trace("Something went wrong. Deleting success marker if present."); if (successMarker != null) try { Files.deleteIfExists(successMarker); } catch (IOException e1) { e1.printStackTrace(); } msg = new TicketFailedMsg(cre, ticket, e, script, stdOut, stdErr); ticketSrc.sendMsg(msg); } finally { if (process != null) { if (log.isDebugEnabled()) log.debug("Stopping local thread for ticket " + invoc.getTicketId() + "."); process.destroy(); } } }
From source file:org.jboss.as.test.integration.logging.profiles.AbstractLoggingProfilesTestCase.java
@Test public void testProfiles() throws Exception { // Test the first profile try {/*w ww.j a va2 s . com*/ deploy(RUNTIME_NAME1, PROFILE1); // make some logs int statusCode = getResponse("DummyProfile1: Test log message from 1"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); } finally { undeploy(RUNTIME_NAME1); } // Test the next profile try { deploy(RUNTIME_NAME2, PROFILE2); // make some logs int statusCode = getResponse("DummyProfile2: Test log message from 2"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); } finally { undeploy(RUNTIME_NAME2); } // Check that only one log record is in the first file Assert.assertTrue("dummy-profile1.log was not created", Files.exists(dummyLog1)); try (final BufferedReader reader = Files.newBufferedReader(dummyLog1, StandardCharsets.UTF_8)) { String line = reader.readLine(); Assert.assertNotNull("Log file dummy-profile1.log is empty and should not be.", line); Assert.assertTrue( "\"LoggingServlet is logging error message\" should be presented in dummy-profile1.log", line.contains("DummyProfile1: Test log message from 1")); // Read lines until we expect no more for (int i = 1; i < profile1LogCount; i++) { Assert.assertNotNull(String.format("Expected %d log records but only got %d", profile1LogCount, i), reader.readLine()); } Assert.assertTrue("Only " + profile1LogCount + " log should be found in dummy-profile1.log", reader.readLine() == null); } // Check that only one log record is in the second file Assert.assertTrue("dummy-profile2.log was not created", Files.exists(dummyLog2)); try (final BufferedReader reader = Files.newBufferedReader(dummyLog2, StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { // The UndertowService also logs one line, this line should be ignored if (line.contains("UndertowService")) continue; if (!line.contains("DummyProfile2: Test log message from 2")) { Assert.fail("dummy-profile2 should not contains this line: " + line); } } } // Check a file that should not have been written to try (final BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { if (line.contains("Test log message from")) { Assert.fail( "LoggingServlet messages should be presented only in files specified in profiles, but found: " + line); } } } // Change the file for the first profile try { deploy(RUNTIME_NAME1, PROFILE1); // Change logging level of file handler on dummy-profile1 from ERROR to // INFO final ModelNode address = createAddress("logging-profile", PROFILE1, "periodic-rotating-file-handler", "DUMMY1"); final ModelNode file = new ModelNode(); file.get("relative-to").set("jboss.server.log.dir"); file.get("path").set(CHANGED_LOG_NAME); ModelNode op = Operations.createWriteAttributeOperation(address, "file", file); executeOperation(op); // make some logs int statusCode = getResponse("DummyProfile1: Changed test message 1"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); // check logs, after logging level change we should see also INFO and // ... messages try (final BufferedReader reader = Files.newBufferedReader(dummyLog1Changed, StandardCharsets.UTF_8)) { String line = reader.readLine(); Assert.assertNotNull("Log file " + CHANGED_LOG_NAME + " is empty and should not be.", line); Assert.assertTrue( "\"LoggingServlet is logging error message\" should be presented in " + CHANGED_LOG_NAME, line.contains("DummyProfile1: Changed test message 1")); // Read lines until we expect no more for (int i = 1; i < profile1LogCount; i++) { Assert.assertNotNull( String.format("Expected %d log records but only got %d", profile1LogCount, i), reader.readLine()); } Assert.assertNull("Only " + profile1LogCount + " log should be found in " + CHANGED_LOG_NAME, reader.readLine()); } } finally { undeploy(RUNTIME_NAME1); } }
From source file:org.jboss.as.test.integration.logging.profiles.LoggingProfilesTestCase.java
@Test public void testProfiles() throws Exception { // Test the first profile try {// w w w . j a va2 s . co m deploy(RUNTIME_NAME1, PROFILE1); // make some logs int statusCode = getResponse("DummyProfile1: Test log message from 1"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); } finally { undeploy(RUNTIME_NAME1); } // Test the next profile try { deploy(RUNTIME_NAME2, PROFILE2); // make some logs int statusCode = getResponse("DummyProfile2: Test log message from 2"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); } finally { undeploy(RUNTIME_NAME2); } // Check that only one log record is in the first file Assert.assertTrue("dummy-profile1.log was not created", Files.exists(dummyLog1)); try (final BufferedReader reader = Files.newBufferedReader(dummyLog1, StandardCharsets.UTF_8)) { String line = reader.readLine(); Assert.assertTrue( "\"LoggingServlet is logging fatal message\" should be presented in dummy-profile1.log", line.contains("DummyProfile1: Test log message from 1")); Assert.assertTrue("Only one log should be found in dummy-profile1.log", reader.readLine() == null); } // Check that only one log record is in the second file Assert.assertTrue("dummy-profile2.log was not created", Files.exists(dummyLog2)); try (final BufferedReader reader = Files.newBufferedReader(dummyLog2, StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { // The UndertowService also logs one line, this line should be ignored if (line.contains("UndertowService")) continue; if (!line.contains("DummyProfile2: Test log message from 2")) { Assert.fail("dummy-profile2 should not contains this line: " + line); } } } // Check a file that should not have been written to try (final BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { if (line.contains("Test log message from")) { Assert.fail( "LoggingServlet messages should be presented only in files specified in profiles, but found: " + line); } } } // Change the file for the first profile try { deploy(RUNTIME_NAME1, PROFILE1); // Change logging level of file handler on dummy-profile1 from FATAL to // INFO final ModelNode address = createAddress("logging-profile", PROFILE1, "periodic-rotating-file-handler", "DUMMY1"); final ModelNode file = new ModelNode(); file.get("relative-to").set("jboss.server.log.dir"); file.get("path").set(CHANGED_LOG_NAME); ModelNode op = Operations.createWriteAttributeOperation(address, "file", file); executeOperation(op); // make some logs int statusCode = getResponse("DummyProfile1: Changed test message 1"); assertTrue("Invalid response statusCode: " + statusCode, statusCode == HttpStatus.SC_OK); // check logs, after logging level change we should see also INFO and // ... messages try (final BufferedReader reader = Files.newBufferedReader(dummyLog1Changed, StandardCharsets.UTF_8)) { String line = reader.readLine(); Assert.assertTrue( "\"LoggingServlet is logging fatal message\" should be presented in dummy-profile1.log", line.contains("DummyProfile1: Changed test message 1")); Assert.assertTrue("Only one log should be found in " + CHANGED_LOG_NAME, reader.readLine() == null); } } finally { undeploy(RUNTIME_NAME1); } }
From source file:it.tidalwave.bluemarine2.persistence.impl.DefaultPersistence.java
/******************************************************************************************************************* * * Imports the repository from the given file. * * @param path where to import the data from * @throws RDFHandlerException/*ww w. j a v a 2 s.co m*/ * @throws IOException * @throws RepositoryException * ******************************************************************************************************************/ private void importFromFile(final @Nonnull Path path) throws IOException, RepositoryException, RDFParseException { try (final RepositoryConnection connection = repository.getConnection(); final Reader reader = Files.newBufferedReader(path, UTF_8)) { log.info("Importing repository from {} ...", path); connection.add(reader, path.toUri().toString(), RDFFormat.N3); connection.commit(); } }
From source file:net.fatlenny.datacitation.service.GitCitationDBService.java
private Query getQueryFromFile(Path path) throws CitationDBException { Properties properties = new Properties(); try {/* w ww . j ava 2 s . c o m*/ properties.load(Files.newBufferedReader(path, StandardCharsets.UTF_8)); } catch (IOException e) { throw new CitationDBException("Error loading query file. ", e); } Revision rev = new DefaultRevision(properties.getProperty(COMMIT_KEY)); String pidIdentifier = properties.getProperty(PID_KEY); PID pid = new DefaultPID.PIDBuilder(pidIdentifier).setName(pidIdentifier).build(); String queryString = properties.getProperty(QUERY_KEY); String datasetString = properties.getProperty(DATASET_KEY); long time = Long.parseLong(properties.getProperty(TIME_KEY)); String description = properties.getProperty(DESCRIPTION_KEY); Query query = new DefaultQuery.QueryBuilder(pid, queryString, datasetString, rev).setTime(time) .setDescription(description).build(); return query; }