Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

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

Prototype

public Scanner(ReadableByteChannel source) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:Main.java

private static String readTemplate(String templatePath) {
    try {//from  w ww .  j  a v a 2s  .c om
        return new Scanner(new File(templatePath)).useDelimiter("\\Z").next();
    } catch (FileNotFoundException e) {
        System.err.println("Template \"" + templatePath + "\" not found");
    }

    return null;
}

From source file:Main.java

private static Scanner wordScanner(String text) {
    Scanner s = new Scanner(text);
    s.useDelimiter(WHITESPACE_PATTERN);
    return s;
}

From source file:Main.java

public static List<String> asList(String source, String separator) {
    Scanner scanner = new Scanner(source);
    scanner.useDelimiter(separator);//from w  ww.  j a  v  a 2s.c  om
    List<String> list = new ArrayList<String>();
    while (scanner.hasNext()) {
        list.add(scanner.next());
    }
    return list;
}

From source file:Main.java

public static String readFileAsset(Context ctx, String filename) throws IOException {
    StringBuilder builder = new StringBuilder();

    Scanner s = new Scanner(ctx.getAssets().open(filename));
    while (s.hasNextLine()) {
        builder.append(s.nextLine());//ww  w . j av  a  2s.  co m
    }
    return builder.toString();
}

From source file:Main.java

public static String readFile(String pathname) {
    File file = new File(pathname);
    StringBuilder fileContents = new StringBuilder((int) file.length());
    String lineSeparator = System.getProperty("line.separator");

    try {/*  w  w w .j a v a  2 s  .  co m*/
        try (Scanner scanner = new Scanner(file)) {
            while (scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine()).append(lineSeparator);
            }
            return fileContents.toString();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static float[][] readMatrix(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {/*  ww  w  . jav a 2 s .  c o m*/
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    float[][] mat = new float[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Float val = Float.parseFloat(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Main.java

public static double[][] readMatrixDouble(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {/* w w  w. j  av a  2 s .c o m*/
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    double[][] mat = new double[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Double val = Double.parseDouble(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Main.java

public static double[] readVectorDouble(String path) throws FileNotFoundException {
    ArrayList<Double> arr = new ArrayList<Double>();
    System.err.println("Load vector from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }/*from w  w w . jav a  2  s  . co  m*/
        if (line.startsWith("%%")) { // detect matrix market format
            System.err.println(line);
            while (sc.nextLine().startsWith("%"))
                ; // skip the comment line, and the dimension info.
            continue;
        }
        arr.add(Double.parseDouble(line));
    }
    System.err.println("Length: " + arr.size());
    double[] ret = new double[arr.size()];
    for (int i = 0; i < arr.size(); i++)
        ret[i] = arr.get(i);
    System.err.println("Done.");
    return ret;
}

From source file:Main.java

public static String[][] tsvToArray(File f, int columns, boolean useFirstLine) throws FileNotFoundException {
    List<String[]> rows = new LinkedList<String[]>();
    Scanner in = new Scanner(f);
    if (!useFirstLine && in.hasNextLine())
        in.nextLine();/* w w  w  .j a v  a 2 s .  c  o m*/

    while (in.hasNextLine()) {
        String[] tokens = in.nextLine().split("\t");
        if (columns > 0 && tokens.length < columns)
            continue;
        rows.add(tokens);
    }
    in.close();
    return rows.toArray(new String[0][]);
}

From source file:Main.java

/**
 * Reads a file relative to the dir this app was started from.
 * @param filename relative filename to load
 * @return entire file as a String//from   ww  w .  j  a v a 2s.c o m
 * @throws FileNotFoundException if file not found!
 */
public static String readFile(String filename) throws FileNotFoundException {

    String startDir = System.getProperty("user.dir");
    File propertyFile = new File(startDir, filename);

    Scanner scan = new Scanner(new FileInputStream(propertyFile));
    scan.useDelimiter("\\Z");
    String content = scan.next();
    scan.close();

    return content;
}