List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:de.tudarmstadt.ukp.wikipedia.parser.mediawiki.FrenchTemplateParser.java
public HashMap<String, String> getParseText(String path, boolean print, boolean normalize) throws FileNotFoundException { HashMap<String, String> parsing = new HashMap<String, String>(); String filePath = path;//from ww w . j a v a2s. c o m Scanner scanner = new Scanner(new File(filePath)); // On boucle sur chaque champ detect while (scanner.hasNextLine()) { String line = scanner.nextLine(); line = line.trim(); if (!line.startsWith("#") && line.length() > 0) { String name = line.replaceAll("\\[(.*)\\]", ""); String value = line.substring(name.length()); value = value.replaceAll("\\[", ""); value = value.replaceAll("\\]", ""); if (print) { System.out.println(name + "\t" + value); } parsing.put(name, value); if (normalize) { if (print) { System.out.println(name.toUpperCase() + "\t" + value); System.out.println(name.toLowerCase() + "\t" + value); } parsing.put(name.toUpperCase(), value); parsing.put(name.toLowerCase(), value); } } // faites ici votre traitement } scanner.close(); return parsing; }
From source file:ml.shifu.shifu.core.processor.PostTrainModelProcessor.java
/** * read the binary average score and update them into column list * /* w w w. j ava 2 s . c o m*/ * @param columnConfigList * input column config list * @return updated column config list * @throws IOException * for any io exception */ private List<ColumnConfig> updateColumnConfigWithBinAvgScore(List<ColumnConfig> columnConfigList) throws IOException { List<Scanner> scanners = ShifuFileUtils.getDataScanners(pathFinder.getBinAvgScorePath(), modelConfig.getDataSet().getSource()); // CommonUtils.getDataScanners(pathFinder.getBinAvgScorePath(), modelConfig.getDataSet().getSource()); for (Scanner scanner : scanners) { while (scanner.hasNextLine()) { List<Integer> scores = new ArrayList<Integer>(); String[] raw = scanner.nextLine().split("\\|"); int columnNum = Integer.parseInt(raw[0]); for (int i = 1; i < raw.length; i++) { scores.add(Integer.valueOf(raw[i])); } ColumnConfig config = columnConfigList.get(columnNum); config.setBinAvgScore(scores); } } // release closeScanners(scanners); return columnConfigList; }
From source file:ml.shifu.shifu.core.processor.PostTrainModelProcessor.java
private List<Integer> getFeatureImportance(SourceType source, String output) throws IOException { List<Integer> featureImportance = new ArrayList<Integer>(); List<Scanner> scanners = null; try {//w w w . jav a 2 s. c o m scanners = ShifuFileUtils.getDataScanners(output, source, new PathFilter() { @Override public boolean accept(Path path) { return path.toString().contains("part-r-"); } }); for (Scanner scanner : scanners) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] keyValues = line.split("\t"); String key = keyValues[0]; featureImportance.add(Integer.parseInt(key)); } } } finally { // release closeScanners(scanners); } return featureImportance; }
From source file:com.joliciel.talismane.GenericLanguageImplementation.java
private String getStringFromScanner(Scanner scanner) { StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); sb.append(line + "\n"); }// w ww . j ava 2s . c om return sb.toString(); }
From source file:org.motechproject.mobile.imp.serivce.oxd.FormDefinitionServiceImpl.java
private String getFileContent(String fileName) { StringBuilder builder = new StringBuilder(); Scanner scanner; try {//from ww w .j av a2 s .c om scanner = new Scanner(new File(fileName)); scanner.useDelimiter("\n"); while (scanner.hasNextLine()) { builder.append(scanner.nextLine() + "\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } return builder.toString(); }
From source file:com.moorestudio.seniorimageprocessing.SeniorSorter.java
public void getStudentData() throws FileNotFoundException, Exception { try {/*ww w . ja v a 2 s .c om*/ Scanner scanner = new Scanner(dataFile); //get the line count for the GUI int lineCount = getLineCount(dataFile); //the csv file does not contain headers while (scanner.hasNextLine()) { //Create the new student map String studentId; Long timestamp; String line = scanner.nextLine(); //check to make sure there arent just empty rows if (!line.replace("\"", "").replace(",", "").isEmpty()) { String[] lineInformation = line.replace("\"", "").split(","); SimpleDateFormat dateParser = new SimpleDateFormat("M/dd/yy HH:mm:ss"); studentId = lineInformation[0]; timestamp = dateParser.parse(lineInformation[3] + " " + lineInformation[2]).getTime(); timestampData.put(studentId, timestamp); } //update the GUI parent.addProgress((.125 / parent.numThreads) / lineCount); } } catch (FileNotFoundException e) { throw new FileNotFoundException("Data file at: " + dataFile.getAbsolutePath() + " does not exist!"); } catch (ParseException ex) { throw new Exception( "The date in the data file at: " + dataFile.getAbsolutePath() + " is not formatted correctly!"); } }
From source file:ml.shifu.shifu.core.processor.PostTrainModelProcessor.java
private void updateAvgScores(SourceType source, String postTrainOutputPath) throws IOException { List<Scanner> scanners = null; try {//ww w . ja v a2s. co m scanners = ShifuFileUtils.getDataScanners(postTrainOutputPath, source, new PathFilter() { @Override public boolean accept(Path path) { return path.toString().contains("part-r-"); } }); for (Scanner scanner : scanners) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] keyValues = line.split("\t"); String key = keyValues[0]; String value = keyValues[1]; ColumnConfig config = this.columnConfigList.get(Integer.parseInt(key)); List<Integer> binAvgScores = new ArrayList<Integer>(); String[] avgScores = value.split(","); for (int i = 0; i < avgScores.length; i++) { binAvgScores.add(Integer.parseInt(avgScores[i])); } config.setBinAvgScore(binAvgScores); } } } finally { // release closeScanners(scanners); } }
From source file:net.sf.jaceko.mock.util.FileReader.java
public String readFileContents(final String fileName) { final StringBuilder text = new StringBuilder(); final String newLine = System.getProperty("line.separator"); Scanner scanner = null; try {/*from www . j av a 2 s . c o m*/ final InputStream resourceAsStream = FileReader.class.getClassLoader().getResourceAsStream(fileName); if (resourceAsStream == null) { LOG.error("File not found: " + fileName); return null; } else { LOG.info(fileName + " found in classpath"); } scanner = new Scanner(resourceAsStream); while (scanner.hasNextLine()) { text.append(scanner.nextLine() + newLine); } } catch (final Exception e) { LOG.error("Problem reading file : " + fileName, e); return null; } finally { if (scanner != null) { scanner.close(); } } return text.toString(); }
From source file:FinalProject.Employee_Login.java
private void Submit_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Submit_ButtonActionPerformed String Password = Arrays.toString(Password_Input.getPassword()); String password = Password.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\, ", ""); String username = Employee_Input.getText(); try {//from ww w . j a v a2s . c om File user = new File("Username.txt"); File pass = new File("Password.txt"); Scanner scanner = new Scanner(user); FileReader frU = new FileReader(user); LineNumberReader u = new LineNumberReader(frU); FileReader frP = new FileReader(pass); LineNumberReader p = new LineNumberReader(frP); int linenumberU = 0; while (scanner.hasNextLine() && u.readLine() != null) { linenumberU++; String lineFromFile = scanner.nextLine(); if (lineFromFile.contains(username)) // a match! { break; } } String pssLine = (String) FileUtils.readLines(pass).get(linenumberU - 1); String usrLine = (String) FileUtils.readLines(user).get(linenumberU - 1); if (username.equals(usrLine) && password.equals(pssLine)) { this.setVisible(false); Employee_Interface f = new Employee_Interface(); f.setVisible(true); f.ID_Number.setText(usrLine + "!"); } else { Error_Message.setVisible(true); } } catch (FileNotFoundException ex) { } catch (IOException ex) { } }
From source file:com.chortitzer.web.admin.controller.TiemposBean.java
public void processMarcasFile() { try {/*from w w w . j a va 2 s. co m*/ FacesContext.getCurrentInstance().addMessage("form:msgs", new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Cargando...")); String fileName = "C:\\Users\\adriang\\Documents\\Synel\\SYBridge\\Data\\Collect.dat"; if (Files.exists(Paths.get(fileName))) { Path file = Paths.get(fileName); Integer countNoRegistrados = 0; Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name()); while (scanner.hasNextLine()) { String aLine = scanner.nextLine(); SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyHHmm"); Date d = formatter.parse(aLine.substring(10, 16) + aLine.substring(29, 33)); //System.out.println(d.toString()); //System.out.println(aLine.substring(18, 29)); //System.out.println(aLine.substring(16, 17)); RrhhMarcas rrhhMarcas = new RrhhMarcas(); rrhhMarcas.setFechahora(d); rrhhMarcas.setNroEmpleado(rrhhEmpleadosRepo.findOne(Integer.parseInt(aLine.substring(18, 29)))); rrhhMarcas.setEntradaSalida(Integer.parseInt(aLine.substring(17, 18))); //if (em.find(RrhhTiempos.class, rrhhTiemposPK) == null) { if (rrhhMarcas.getNroEmpleado() == null) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( FacesMessage.SEVERITY_ERROR, "Error!", "Existen registros para empleados no identificacdos en la base de datos! - Nro.: " + aLine.substring(18, 29))); countNoRegistrados += 1; System.out.println("Empleado sin registro: " + aLine.substring(18, 29)); } else { rrhhMarcasRepo.save(rrhhMarcas); } } limpiarMarcasDobles(); if (countNoRegistrados == 0) { Files.deleteIfExists(file); } } } catch (Exception ex) { LOGGER.error(Thread.currentThread().getStackTrace()[1].getMethodName(), ex); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", ex.getMessage())); } }