Example usage for java.util Scanner hasNextLine

List of usage examples for java.util Scanner hasNextLine

Introduction

In this page you can find the example usage for java.util Scanner hasNextLine.

Prototype

public boolean hasNextLine() 

Source Link

Document

Returns true if there is another line in the input of this scanner.

Usage

From source file:org.springframework.hateoas.alps.JacksonSerializationTest.java

private static String read(Resource resource) throws IOException {

    Scanner scanner = null;

    try {//from   w  w  w . ja v a 2 s .  c o  m

        scanner = new Scanner(resource.getInputStream());
        StringBuilder builder = new StringBuilder();

        while (scanner.hasNextLine()) {

            builder.append(scanner.nextLine());

            if (scanner.hasNextLine()) {
                builder.append(System.getProperty("line.separator"));
            }
        }

        return builder.toString();

    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:CommandLineInterpreter.java

/**
 *
 *
 * @param file/*from www.ja  va2  s  . com*/
 * @param guiAlert
 * @return
 */
public static boolean checkResources(final File file, final boolean guiAlert) {
    Scanner input = null;
    String message = null;
    try {
        input = new Scanner(file);
        while (input.hasNextLine()) {
            String currentFilePath = input.nextLine().trim();
            if (!currentFilePath.isEmpty()) {
                File currentFile = new File(currentFilePath);
                if (!currentFile.exists() || !currentFile.canRead() || !currentFile.canWrite()) {
                    message = "Can not read/write resource file:\n\"" + currentFile.getAbsolutePath() + "\"";
                    alert(message, guiAlert);
                    return false;
                }
            }
        }
    } catch (Exception e) {
        // TODO: logging
        e.printStackTrace();
    } finally {
        if (input != null) {
            input.close();
        }
    }

    return true;
}

From source file:org.eclipse.lyo.testsuite.server.trsutils.TestCore.java

/**
 * This method is used to read the contents
 * of a file as a String//from   w w  w.j  a  v a 2 s.c  o m
 * @param f
 * @return
 */
protected static String readFileAsString(File f) {

    StringBuilder stringBuilder = new StringBuilder();
    Scanner scanner = null;
    try {
        scanner = new Scanner(f);
    } catch (FileNotFoundException e) {
        return null;
    }

    try {
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine() + "\n");
        }
    } finally {
        scanner.close();
    }
    return stringBuilder.toString();
}

From source file:bs.ws1.dm.itg.App.java

public static List<String> readFileIntoStringList(String filename) throws FileNotFoundException {
    List<String> lines = new ArrayList<String>();
    Scanner fileScanner = null;
    fileScanner = new Scanner(new File(filename));
    while (fileScanner.hasNextLine()) {
        lines.add(fileScanner.nextLine());
    }//from   w  ww  . j  av  a  2s.com
    return lines;
}

From source file:org.lightcouch.CouchDbUtil.java

public static String readFile(String path) {
    InputStream instream = CouchDbUtil.class.getResourceAsStream(path);
    StringBuilder content = new StringBuilder();
    Scanner scanner = null;
    try {//from  w  ww  . jav  a 2s.  c  o  m
        scanner = new Scanner(instream);
        while (scanner.hasNextLine()) {
            content.append(scanner.nextLine() + LINE_SEP);
        }
    } finally {
        scanner.close();
    }
    return content.toString();
}

From source file:BitLottoVerify.java

public static Map<String, Long> getPaymentsBlockExplorer(String addr) throws Exception {
    URL u = new URL("http://blockexplorer.com/address/" + addr);
    Scanner scan = new Scanner(u.openStream());

    TreeMap<String, Long> map = new TreeMap<String, Long>();

    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        StringTokenizer stok = new StringTokenizer(line, "\"#");
        while (stok.hasMoreTokens()) {
            String token = stok.nextToken();
            if (token.startsWith("/tx/")) {
                String tx = token.substring(4);
                line = scan.nextLine();//from   w ww  .  j  av  a  2  s .  c o  m
                line = scan.nextLine();
                StringTokenizer stok2 = new StringTokenizer(line, "<>");
                stok2.nextToken();
                double amt = Double.parseDouble(stok2.nextToken());
                long amt_l = (long) Math.round(amt * 1e8);
                map.put(tx, amt_l);

            }
        }
    }
    return map;
}

From source file:org.apache.mahout.ga.watchmaker.cd.tool.CDInfosTool.java

/**
 * Load the dataset's attributes descriptors from an .info file
 * /*from   w ww  . j  a v  a 2  s  .  c om*/
 * @param inpath dataset path
 */
private static Descriptors loadDescriptors(FileSystem fs, Path inpath) throws IOException {
    // TODO should become part of FileInfoParser

    Path infpath = FileInfoParser.getInfoFile(fs, inpath);

    FSDataInputStream input = fs.open(infpath);
    Scanner reader = new Scanner(input);

    List<Character> descriptors = Lists.newArrayList();

    while (reader.hasNextLine()) {
        String c = reader.nextLine();
        descriptors.add(c.toUpperCase(Locale.ENGLISH).charAt(0));
    }

    if (descriptors.isEmpty()) {
        throw new IllegalArgumentException("Infos file is empty");
    }

    char[] desc = new char[descriptors.size()];
    for (int index = 0; index < descriptors.size(); index++) {
        desc[index] = descriptors.get(index);
    }

    return new Descriptors(desc);
}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Go through existing CSS Files and replace variable access with variable
 * values/*from   w  w  w  . ja va2 s . c  o m*/
 *
 * @param rootCSSVars All css variables you want to convert from name to value
 */
private static void editForRelease(ArrayList<CSSVar> rootCSSVars) {
    for (File cssFile : theCSSList) {

        ArrayList<CSSVar> localCSSVars = getCSSVars(cssFile, ":local");
        for (CSSVar cssVar : rootCSSVars) {
            if (!localCSSVars.contains(cssVar)) {
                localCSSVars.add((CSSVar) (cssVar.clone()));
            }
        }

        // This will store info the new text for the file we will write below
        ArrayList<String> filesNewInfo = new ArrayList<String>();
        try {

            Scanner fileReader = new Scanner(cssFile);
            while (fileReader.hasNextLine()) {

                String currentLine = fileReader.nextLine();

                // If we find variables, replace them, with their value
                if (currentLine.contains("var(")) {
                    for (CSSVar var : localCSSVars) {
                        while (currentLine.contains(var.getName())) {
                            currentLine = currentLine.replace("var(" + var.getName() + ")", var.getValue());
                        }
                    }
                }
                // Add new currentLine to be written
                filesNewInfo.add(currentLine);
            }
            fileReader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Write the new files below
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(cssFile.getPath()));

            while (!filesNewInfo.isEmpty()) {
                writer.write(filesNewInfo.get(0));
                writer.newLine();
                filesNewInfo.remove(0);
            }
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:ca.weblite.xmlvm.VtableHelper.java

/**
 * Tries to find the actual function to use for the given mangled method name in the 
 * given header file.// w  w w  .j av  a  2s .  co  m
 * @param searchPaths  The paths to search for headers.
 * @param headerFile The header file in which to start the search.  Its name is used
 * as the base for the mangled function name.
 * @param mangledMethodName The mangled method name.  This should only be the portion
 * pertaining to the method - not the class portion of a mangled function name.  It will
 * be concatenated to headerFile.getName()+"_" to form the full function name.
 * @return Either a VTable ID (which will have the form XMLVM_VTABLE_IDX_{funcname}) or
 * the actual function name if there is no vtable index constant defined.
 * @throws IOException 
 */
public static String resolveVirtualMethod(Project project, Path searchPaths, File headerFile,
        String mangledMethodName) throws IOException {
    String mangledClassName = headerFile.getName().replaceAll("\\.h$", "");
    String funcName = mangledClassName + "_" + mangledMethodName;
    String headerContents = FileUtils.readFileToString(headerFile);
    Scanner scanner = new Scanner(headerContents);
    int state = 0;
    String superClassHeader = null;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (state == 0 && line.startsWith("// Super Class:")) {
            state = 1;
        } else if (state == 1 && line.startsWith("#include \"")) {
            superClassHeader = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
            state = 0;
            break;
        }
    }

    String idxConstant = "XMLVM_VTABLE_IDX_" + funcName;
    String idxConstantSp = idxConstant + " ";
    if (headerContents.indexOf(idxConstantSp) > 0) {
        return idxConstant;
    } else if (headerContents.indexOf(funcName + "(") > 0) {
        return funcName;
    } else if (superClassHeader != null) {
        for (String p : searchPaths.list()) {
            File d = new File(p);
            File h = new File(d, superClassHeader);
            if (h.exists()) {
                return resolveVirtualMethod(project, searchPaths, h, mangledMethodName);
            }
        }
        throw new RuntimeException("Failed to find header file " + superClassHeader);
    } else {
        throw new RuntimeException(
                "Failed to find virtual method " + mangledMethodName + " in header " + headerFile);
    }
}

From source file:org.bimserver.utils.Licenser.java

public static String getCommentedLicenseText(File file) {
    if (!file.exists()) {
        return null;
    }/*from w ww  .ja v a2s.c o m*/
    try {
        String content = FileUtils.readFileToString(file);
        StringBuilder newContent = new StringBuilder();
        Scanner scanner = new Scanner(content);
        try {
            newContent.append(
                    "/******************************************************************************\n");
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                newContent.append(" * " + line + "\n");
            }
            newContent
                    .append(" *****************************************************************************/");
        } finally {
            scanner.close();
        }
        return newContent.toString();
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    return null;
}