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

StringreadFile(String filePath)
read File
return readFile(new File(filePath));
StringreadFile(String filePath)
Converts the contents of a file into a String.
Scanner scanner = null;
try {
    scanner = new Scanner(new File(filePath)).useDelimiter("\\A");
    return scanner.next();
} finally {
    if (scanner != null) {
        scanner.close();
StringreadFile(String filePath)
Reads the file with the specified path as a String.
BufferedReader br = new BufferedReader(new FileReader(filePath));
Scanner sc = new Scanner(br);
try {
    return sc.useDelimiter("\\Z").next();
} finally {
    sc.close();
    br.close();
StringreadFile(String path)
read File
return readFile(new File(path));
char[]readFile(String pathname)
This method reads a file and returns the file content as char array.
File file = new File(pathname);
String str = "";
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
    str += scanner.nextLine();
return str.toLowerCase().toCharArray();
ScannerreadFile(String project, boolean judge)
read File
if (judge)
    return new Scanner(new File("E:\\novice\\judgedata\\" + project + ".dat"));
else
    return new Scanner(new File("sampledata\\" + project + ".dat"));
StringreadFileFully(InputStream stream)
read File Fully
StringBuilder stringBuilder = new StringBuilder();
try (Scanner scanner = new Scanner(stream)) {
    while (scanner.hasNext()) {
        stringBuilder.append(scanner.next());
return stringBuilder.toString();
ListreadFileToList(File file)
read File To List
ArrayList<String> list = new ArrayList<>();
try {
    Scanner scan = new Scanner(file);
    while (scan.hasNextLine()) {
        list.add(scan.nextLine());
} catch (FileNotFoundException ex) {
    return null;
...
SetreadFileWithoutComments(File input)
read File Without Comments
Set<String> adsList;
try (Scanner scanner = new Scanner(input)) {
    adsList = new HashSet<String>(10000);
    final String COMMENT_MARKER = "#";
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine().trim();
        if (line.startsWith(COMMENT_MARKER) || line.isEmpty()) {
            continue;
...
StringreadFromReader(Scanner scanner)
This method is used to read input from buffered reader.
String input;
input = scanner.nextLine().trim();
return input;