Java Utililty Methods Scanner Read

List of utility methods to do Scanner Read

Description

The list of methods to do Scanner Read are organized into topic(s).

Method

int[]readArr(Scanner in, int n)
read Arr
int[] ar = new int[n];
for (int i = 0; n - i != 0; i++) {
    ar[i] = in.nextInt();
return ar;
StringBuilderreadCQLFile(String cqlFileName)
read CQL File
StringBuilder cypher = new StringBuilder();
try (Scanner scanner = new Scanner(
        Thread.currentThread().getContextClassLoader().getResourceAsStream(cqlFileName))) {
    scanner.useDelimiter(System.getProperty("line.separator"));
    while (scanner.hasNext()) {
        cypher.append(scanner.next()).append(' ');
return cypher;
StringreadFile(Class ownerClass, String fileName)
read File
String resourcePath = ownerClass.getSimpleName() + "/" + fileName;
try (InputStream inputStream = ownerClass.getResourceAsStream(resourcePath);
        Scanner scanner = new Scanner(inputStream)) {
    scanner.useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
StringreadFile(File f)
Read the given file with UTF-8 encoding into a string and return it.
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(new FileInputStream(f), "UTF-8");
try {
    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine()).append('\n');
} finally {
    scanner.close();
...
StringreadFile(File f)
read File
Scanner sc = new Scanner(f);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine())
    sb.append(sc.nextLine());
sc.close();
return sb.toString();
StringreadFile(File f)
This method reads the contents of a text file.
String output = "No final terminator";
try {
    output = new Scanner(f).useDelimiter("\\Z").next();
} catch (FileNotFoundException e) {
    System.err.println("Problem reading from file");
return output;
StringreadFile(File file)
read File
Scanner scanner = new Scanner(file);
String text = scanner.useDelimiter("\\A").next();
scanner.close(); 
return text;
StringreadFile(File file)
read File
String out = "";
try {
    FileInputStream r = new FileInputStream(file);
    out = readStream(r);
    r.close();
} catch (Exception e) {
    e.printStackTrace();
return out;
StringreadFile(File file)
read File
Scanner scanner = new Scanner(file);
String ret = scanner.useDelimiter(MULTILINE_START_ANCHOR_REGEX).next();
scanner.close();
return ret;
StringreadFile(File file)
Read a file and convert to String
StringBuilder result = new StringBuilder("");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    result.append(line).append("\n");
scanner.close();
return result.toString();
...