Scanner scanner = new Scanner(myString); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // process the line } scanner.close();
/** * Reads the Label file, the file which contains the Mary context features, creates an scanner object and calls getTargets * * @param LabFile * LabFile * @param htsData * htsData * @throws Exception * Exception * @return targets */ public static List<Target> getTargetsFromFile(String LabFile, HMMData htsData) throws Exception { List<Target> targets = null; Scanner s = null; try { /* parse text in label file */ s = new Scanner(new BufferedReader(new FileReader(LabFile))); targets = getTargets(s, htsData); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); } finally { if (s != null) s.close(); } return targets; }
final Scanner scan = new Scanner(new File("/path/to/StudentsList")); final List<Student> students = new ArrayList<Student>(); while (scan.hasNextLine()) { final Scanner line = new Scanner(scan.nextLine()); line.useDelimiter("%"); students.add(new Student(line.next(), line.nextInt(), line.nextInt())); line.close(); } scan.close();
/** * A revision starts with the keyword "rv", followed by a natural number * * @param rawRevision String containing the revision number * * @return revision number * * @throws InvalidRevisionException if not matching "rv" followed by a natural number. */ private int parseRevision( @Nonnull final String rawRevision ) throws InvalidRevisionException { if( !PATTERN_VALID_REVISION.matcher( rawRevision ).matches() ) { throw new InvalidRevisionException(); } final Scanner scanner = new Scanner( rawRevision ); final int revision = scanner.useDelimiter( PATTERN_REVISION ).nextInt(); scanner.close(); return revision; }
/** * Reads the Label file, the file which contains the Mary context features, creates an scanner object and calls getTargets * * @param LabFile * LabFile * @param htsData * htsData * @throws Exception * Exception * @return targets */ public static List<Target> getTargetsFromFile(String LabFile, HMMData htsData) throws Exception { List<Target> targets = null; Scanner s = null; try { /* parse text in label file */ s = new Scanner(new BufferedReader(new FileReader(LabFile))); targets = getTargets(s, htsData); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); } finally { if (s != null) s.close(); } return targets; }
private static String checkGpuAnalyzerVersion(){ try { ProcessBuilder pb = new ProcessBuilder("GPUShaderAnalyzer", "-ListModules"); Process p = pb.start(); Scanner scan = new Scanner(p.getInputStream()); String ln = scan.nextLine(); scan.close(); p.destroy(); return ln; } catch (IOException ex) { logger.log(Level.SEVERE, "IOEx", ex); } return null; }
/** * A build is just a natural number * * @param rawBuild String containing the build number * * @return build number * * @throws InvalidBuildException if not a natural number. */ private int parseBuild( @Nonnull final String rawBuild ) throws InvalidBuildException { if( !PATTERN_NATURAL.matcher( rawBuild ).matches() ) { throw new InvalidBuildException(); } final Scanner scanner = new Scanner( rawBuild ); final int build = scanner.useDelimiter( PATTERN_BUILD ).nextInt(); scanner.close(); return build; } }
private String convertStreamToString(java.io.InputStream is) { java.util.Scanner scanner = null; String result = ""; try { scanner = new java.util.Scanner(is).useDelimiter("\\A"); result = scanner.hasNext() ? scanner.next() : ""; scanner.close(); } finally { if (scanner != null) scanner.close(); } return result; }
private static String checkCgCompilerVersion(){ try { ProcessBuilder pb = new ProcessBuilder("cgc", "--version"); Process p = pb.start(); Scanner scan = new Scanner(p.getErrorStream()); String ln = scan.nextLine(); scan.close(); p.waitFor(); String versionNumber = ln.split("\\s")[2]; return versionNumber.substring(0, versionNumber.length()-1); } catch (IOException ex) { logger.log(Level.SEVERE, "IOEx", ex); } catch (InterruptedException ex){ } return null; }
private String convertStreamToString(java.io.InputStream is) { java.util.Scanner scanner = null; String result=""; try { scanner = new java.util.Scanner(is).useDelimiter("\\A"); result = scanner.hasNext() ? scanner.next() : ""; scanner.close(); } finally { if(scanner!=null) scanner.close(); } return result; }
/** * removes trailing whitespace */ public void load(InputStream fis) throws IOException { Scanner in = new Scanner(fis); ByteArrayOutputStream out = new ByteArrayOutputStream(); while (in.hasNext()) { out.write(in.nextLine().trim().getBytes()); out.write("\n".getBytes()); } in.close(); InputStream is = new ByteArrayInputStream(out.toByteArray()); super.load(is); } }