Write a method called readEntireFile that accepts a Scanner representing an input file as its parameter, then reads that file and returns its entire text contents as a String.
// package chap456;
import java.io.FileReader;
import java.io.IOException;
public class ReadEntireFile {
public static String readEntireFile() throws IOException {
FileReader in = new FileReader("{give proper path}\\input.txt");
// use StringBuilder class as we want string to be mutable
StringBuilder sb = new StringBuilder();
// it'll stop reading if there is no character which means -1
while(in.read() != -1) {
sb.append(in.read());
}
// convert it to string and return
String contents = sb.toString();
in.close();
return contents;
}
public static void main(String[] args) throws IOException {
System.out.println(readEntireFile());
}
}