I am trying to load a text file into java to get each individual word stored in an ArrayList of words. (which is an object that I have created that is working without problems.
import java.util.*; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class txtUtils { public ArrayList<Word> readFromText(ArrayList<Word> Words) { String file = "corpus.txt"; FileInputStream in = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { for (int j = 0; j < strLine.length(); j++){ { strLine = br.readLine(); int start = 0; for(int x= start; x < strLine.length(); x++) { if(strLine.charAt(x)== ' ' || strLine.charAt(x) == '.') { Words.add(new Word((strLine.substring(start,x- 1)).toUpperCase())); start = x; } } } } } in.close(); return Words; } } When compiling it, I get this error.
unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown It is shown for the line, FileInputStream in = new FileInputStream(file);
The file is clearly there and has even been added in my classpath, so I do not see why it keeps on throwing this exception. Is there a way to override it? If not, is there any other way to load a text file into java?