I have this code for opening a file in java and I am wondering how I can use the contents of the file within other methods that I am planning to create? I would like to check aspects about the file but unsure how to pass the file content into the method.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TextAnalysis {
public static void main(String[] args) {
// read the text file
readFile();
}
public static void readFile() {
String fileName = "PoliticalSpeech.txt";
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}