0

I am new to Java and am having a difficult time with this particular problem. I created a data.txt file, put the info into it, and placed it in the same package I am working in. When I try to run the code, I get a "System cannot find the file specified" error. I can't for the life of me figure out what I am doing wrong. The ultimate goal is to have the information read from the data file, tokenized, and then printed out using the format I have in a separate control file. Any assistance would be greatly appreciated.

package cs520.hw3.part2;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;


public class Test {

public static void main(String[] args)
{

    String inputFileName = "data.txt";
    FileReader fileReader = null;
    try
    {
        fileReader = new FileReader(inputFileName);
    } 
    catch (FileNotFoundException e)
    {

        e.printStackTrace();
    }

BufferedReader reader = new BufferedReader(fileReader);
String input;

try {
    input = reader.readLine();
    while (input != null)
    {
        processInputData(input);
        input = reader.readLine();
    }
} catch (IOException e) {
    e.printStackTrace();
}
try {
    fileReader.close();
} catch (IOException e) {
    e.printStackTrace();
}
}       

private static void processInputData(String data)
{
    StringTokenizer st = new StringTokenizer(data, ",");
    String senName      = st.nextToken();
    String senParty     = st.nextToken();
    String senState     = st.nextToken();
    String senYears     = st.nextToken();
    //Senator senatorDetails = 
        //new Senator(senName, senParty, senState, senYears);
    //System.out.println(senatorDetails);
}
}   
Affekonig
  • 11
  • 2
  • You need to provide the absolute path of the file like `C:\\YOUR_FOLDER\\data.txt` (in Windows OS) – Vasu Apr 01 '17 at 18:54
  • While @javaguy means well, I strongly beg to differ. No, you **don't** want to use the absolute path as this guarantees that your code will work on your system and your system alone. Better to 1) use resources or 2) read up on and understand relative path and where Java looks for files. – Hovercraft Full Of Eels Apr 01 '17 at 18:56
  • Please check out [these similar questions and their answers for more](https://www.google.com/#q=site:stackoverflow.com+java+System+cannot+find+the+file+specified&*) – Hovercraft Full Of Eels Apr 01 '17 at 18:56
  • I read the thread marked as the same just now (weird that it never showed up during my search) and maybe I'm just confused on the syntax. I edited the line to read String inputFileName = "/src/cs520.hw3.part2/data.txt"; FileReader fileReader = null; but still receive the error. – Affekonig Apr 01 '17 at 19:10

0 Answers0