java - Cant find file using FileReader -
i'm working on project class , supposed read in file called samplesearch.txt included below code i'm working with. problem through eclipse can't seem able load file in. comes file not found though text file in same directory rest of code.
import java.util.*; import java.io.*; public class driver{ public static void main (string[] args){ // try block needed read in file try { //open file "samplesearch.txt" filereader filename = new filereader("samplesearch.txt"); scanner fileread = new scanner(filename); //read in number of rows int numrow = fileread.nextint(); fileread.nextline(); //read in number of columns int numcol = fileread.nextint(); fileread.nextline(); //create 2d array hold characters in file char[][] array = new char[numrow][numcol]; //for each row in file (int r = 0; r < numrow; r++) { //read in row string string row = fileread.nextline(); //parse string sequence of characters (int c = 0; c < numcol; c++) { //store each character in 2d array array[r][c] = row.charat(c); } } //create new instance of wordsearch class wordsearch ws = new wordsearch(array); //play game ws.play(); } catch (filenotfoundexception exception) { //error thrown if file cannot found. see directions or email me... system.out.println("file not found gribble"); } }
}
you need know in directory code searching file:
do this:
to current path: system.out.println(new file(".").getabsolutefile());
this show path java code looking files. reference file using relative path starting location. cheers
Comments
Post a Comment