List of usage examples for java.io LineNumberReader close
public void close() throws IOException
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 w ww.j a va2s .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:org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils.java
protected List<String> getLinesList(String fileLocation, int rows, String encoding) throws IOException { List<String> lines = new ArrayList<String>(); try {/* w w w .ja va2 s .c om*/ File file = new File(fileLocation); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, encoding); LineNumberReader reader = new LineNumberReader(isr); String line; int lineNumber = 0; while ((line = reader.readLine()) != null && lineNumber < rows) { lines.add(line); lineNumber++; } reader.close(); } catch (Exception e) { log.equals(e); } return lines; }
From source file:StockForecast.Stock.java
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: try {/*from www.j ava2 s . c om*/ converttoraw(); FileReader fr = new FileReader("RawData.txt"); BufferedReader br = new BufferedReader(fr); LineNumberReader lnr = new LineNumberReader(new FileReader(new File("RawData.txt"))); lnr.skip(Long.MAX_VALUE); int lineNum = lnr.getLineNumber(); System.out.println(lineNum); lnr.close(); Connect connect = new Connect(); ResultSet rs = connect.connectSelect("1", "SELECT * FROM ROOT.STOCK"); ArrayList<String> stringList = new ArrayList<>(); while (rs.next()) { String sym = rs.getString("symbol"); System.out.println(sym); stringList.add(sym); } for (int i = 0; i < lineNum; i++) { String lineNumber = br.readLine(); String[] parts = lineNumber.split(","); String name = parts[0]; String symbol = parts[1]; if (stringList.contains(symbol)) { System.out.println(symbol + " : Is Already Added"); } else if (symbol.length() > 4) { System.out.println(symbol + " : Is Not a Stock Symbol"); } else { connect.connectInsert("INSERT INTO STOCK VALUES ('" + symbol + "','" + name + "')"); } } br.close(); fr.close(); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } try { displayStock(); } catch (Exception ex) { Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:io.fabric8.ConnectorMojo.java
private List<String> loadFile(File file) throws Exception { List<String> lines = new ArrayList<>(); LineNumberReader reader = new LineNumberReader(new FileReader(file)); String line;//w w w.j av a 2s.c om do { line = reader.readLine(); if (line != null) { lines.add(line); } } while (line != null); reader.close(); return lines; }
From source file:io.fabric8.ConnectorMojo.java
private List<String> loadFile(InputStream fis) throws Exception { List<String> lines = new ArrayList<>(); LineNumberReader reader = new LineNumberReader(new InputStreamReader(fis)); String line;// w ww .j av a 2s . co m do { line = reader.readLine(); if (line != null) { lines.add(line); } } while (line != null); reader.close(); return lines; }
From source file:com.datatorrent.stram.StramLocalClusterTest.java
/** * Verify test configuration launches and stops after input terminates. * Test validates expected output end to end. * * @throws Exception// w w w . j av a 2s . c o m */ @Test public void testLocalClusterInitShutdown() throws Exception { TestGeneratorInputOperator genNode = dag.addOperator("genNode", TestGeneratorInputOperator.class); genNode.setMaxTuples(2); GenericTestOperator node1 = dag.addOperator("node1", GenericTestOperator.class); node1.setEmitFormat("%s >> node1"); File outFile = new File( "./target/" + StramLocalClusterTest.class.getName() + "-testLocalClusterInitShutdown.out"); outFile.delete(); TestOutputOperator outNode = dag.addOperator("outNode", TestOutputOperator.class); outNode.pathSpec = outFile.toURI().toString(); dag.addStream("fromGenNode", genNode.outport, node1.inport1); dag.addStream("fromNode1", node1.outport1, outNode.inport); dag.getAttributes().put(LogicalPlan.CONTAINERS_MAX_COUNT, 2); StramLocalCluster localCluster = new StramLocalCluster(dag); localCluster.setHeartbeatMonitoringEnabled(false); localCluster.run(); Assert.assertTrue(outFile + " exists", outFile.exists()); LineNumberReader lnr = new LineNumberReader(new FileReader(outFile)); String line; while ((line = lnr.readLine()) != null) { Assert.assertTrue("line match " + line, line.matches("" + lnr.getLineNumber() + " >> node1")); } Assert.assertEquals("number lines", 2, lnr.getLineNumber()); lnr.close(); }
From source file:org.springframework.test.jdbc.SimpleJdbcTestUtils.java
/** * Execute the given SQL script.//from w ww. ja v a 2s.c o m * <p>The script will normally be loaded by classpath. There should be one statement * per line. Any semicolons will be removed. <b>Do not use this method to execute * DDL if you expect rollback.</b> * @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations * @param resource the resource (potentially associated with a specific encoding) * to load the SQL script from. * @param continueOnError whether or not to continue without throwing an * exception in the event of an error. * @throws DataAccessException if there is an error executing a statement * and continueOnError was {@code false} */ public static void executeSqlScript(SimpleJdbcTemplate simpleJdbcTemplate, EncodedResource resource, boolean continueOnError) throws DataAccessException { if (logger.isInfoEnabled()) { logger.info("Executing SQL script from " + resource); } long startTime = System.currentTimeMillis(); List<String> statements = new LinkedList<String>(); LineNumberReader reader = null; try { reader = new LineNumberReader(resource.getReader()); String script = JdbcTestUtils.readScript(reader); char delimiter = ';'; if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) { delimiter = '\n'; } JdbcTestUtils.splitSqlScript(script, delimiter, statements); for (String statement : statements) { try { int rowsAffected = simpleJdbcTemplate.update(statement); if (logger.isDebugEnabled()) { logger.debug(rowsAffected + " rows affected by SQL: " + statement); } } catch (DataAccessException ex) { if (continueOnError) { if (logger.isWarnEnabled()) { logger.warn("SQL: " + statement + " failed", ex); } } else { throw ex; } } } long elapsedTime = System.currentTimeMillis() - startTime; if (logger.isInfoEnabled()) { logger.info("Done executing SQL scriptBuilder from " + resource + " in " + elapsedTime + " ms."); } } catch (IOException ex) { throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex); } finally { try { if (reader != null) { reader.close(); } } catch (IOException ex) { // ignore } } }
From source file:org.beangle.struts2.action.EntityDrivenAction.java
/** * //w w w .j ava 2 s . c o m * * @param upload * @param clazz * @return */ protected EntityImporter buildEntityImporter(String upload, Class<?> clazz) { try { File file = get(upload, File.class); if (null == file) { logger.error("cannot get upload file {}.", upload); return null; } String fileName = get(upload + "FileName"); InputStream is = new FileInputStream(file); if (fileName.endsWith(".xls")) { EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new ExcelItemReader(is, 1)); put("importer", importer); return importer; } else { LineNumberReader reader = new LineNumberReader(new InputStreamReader(is)); if (null == reader.readLine()) { reader.close(); return null; } reader.reset(); EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new CsvItemReader(reader)); return importer; } } catch (MyException e) { logger.error("saveAndForwad failure", e); return null; } catch (Exception e) { logger.error("error", e); return null; } }
From source file:org.kuali.test.runner.execution.FileOperationExecution.java
private List<File> findFilesContainingText(List<File> inputFiles, String txt) { List<File> retval = new ArrayList<File>(); LineNumberReader lnr = null; for (File f : inputFiles) { try {//from w w w .j av a 2 s . c o m lnr = new LineNumberReader(new FileReader(f)); String line = null; while ((line = lnr.readLine()) != null) { if (line.contains(txt)) { retval.add(f); break; } } } catch (Exception ex) { } finally { try { if (lnr != null) { lnr.close(); } } catch (Exception ex) { } ; } } return retval; }
From source file:gtu._work.etc.GoogleContactUI.java
void googleTableMouseClicked(MouseEvent evt) { try {//from w w w.j a va2 s. c om JPopupMenuUtil popupUtil = JPopupMenuUtil.newInstance(googleTable).applyEvent(evt); //CHANGE ENCODE popupUtil.addJMenuItem("set encode", new ActionListener() { public void actionPerformed(ActionEvent e) { try { String code = StringUtils.defaultString(JOptionPaneUtil.newInstance().iconPlainMessage() .showInputDialog("input file encode", "ENCODE"), "UTF8"); encode = Charset.forName(code).displayName(); } catch (Exception ex) { JCommonUtil.handleException(ex); } System.err.println("encode : " + encode); } }); //SIMPLE LOAD GOOGLE CSV FILE popupUtil.addJMenuItem("open Google CSV file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile("csv", ".csv") .showOpenDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } try { if (file.getName().endsWith(".csv")) { DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); LineNumberReader reader = new LineNumberReader( new InputStreamReader(new FileInputStream(file), GOOGLE_CVS_ENCODE)); for (String line = null; (line = reader.readLine()) != null;) { if (reader.getLineNumber() == 1) { continue; } model.addRow(line.split(",")); } reader.close(); googleTable.setModel(model); JTableUtil.newInstance(googleTable).hiddenAllEmptyColumn(); } } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //SAVE CSV FILE FOR GOOGLE popupUtil.addJMenuItem("save to Google CVS file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile(".csv", ".csv") .showSaveDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } file = FileUtil.getIndicateFileExtension(file, ".csv"); try { BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(file), GOOGLE_CVS_ENCODE)); StringBuilder sb = new StringBuilder(); for (Object title : googleColumns) { sb.append(title + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); for (int row = 0; row < model.getRowCount(); row++) { sb = new StringBuilder(); for (int col = 0; col < model.getColumnCount(); col++) { String colVal = StringUtils.defaultString((String) model.getValueAt(row, col), ""); if (colVal.equalsIgnoreCase("null")) { colVal = ""; } sb.append(colVal + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); } writer.flush(); writer.close(); } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //PASTE CLIPBOARD popupUtil.addJMenuItem("paste clipboard", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_multiRowData(true); } }); popupUtil.addJMenuItem("paste clipboard to selected cell", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_singleValueToSelectedCell(); } }); JMenuItem addEmptyRowItem = JTableUtil.newInstance(googleTable).jMenuItem_addRow(false, "add row count?"); addEmptyRowItem.setText("add row"); JMenuItem removeColumnItem = JTableUtil.newInstance(googleTable).jMenuItem_removeColumn(null); removeColumnItem.setText("remove column"); JMenuItem removeRowItem = JTableUtil.newInstance(googleTable).jMenuItem_removeRow(null); removeRowItem.setText("remove row"); JMenuItem removeAllRowItem = JTableUtil.newInstance(googleTable) .jMenuItem_removeAllRow("remove all row?"); removeAllRowItem.setText("remove all row"); JMenuItem clearSelectedCellItem = JTableUtil.newInstance(googleTable) .jMenuItem_clearSelectedCell("are you sure clear selected area?"); clearSelectedCellItem.setText("clear selected area"); popupUtil.addJMenuItem(addEmptyRowItem, removeColumnItem, removeRowItem, removeAllRowItem, clearSelectedCellItem); popupUtil.show(); } catch (Exception ex) { JCommonUtil.handleException(ex); } }