java.io.FileNotFoundException

Hi All!

I’m developing a URCap in the Starter package which would need to use a txt file.
one of my classes would need to read it:

File Reader reader=new FileReader("./data.txt");
BufferedReader bufferedReader=new BufferedReader(reader);

inputLine=bufferedReader.readLine();

The mentioned data.txt is under src/main/resources but i’ve tried many different paths.
Maven compiles fine but as i put the URCap to the Simulator it can never find my file.
I also copied the classes folder from target to the programs folder on the robot and yet nothing.

Am i making an obvious stupid mistake?

Thanks in advance:
Andor

The error is thrown because java is not aware of where yout file is, so it is looking in the default location (which i think will be wherever your URCap is stored on the robot, I am not sure) in order to use a file that is stored in the resources folder and compiled as part of the URCap, I have used the following line of code:

InputStream is = getClass().getResourceAsStream("/file.txt");

I then itterate through each line of code with

Scanner scanner = new Scanner(is);
scanner.useDelimiter("\\n");
while (scanner.hasNext())
{
    writer.appendLine(scanner.next());
}
scanner.close();

Here i write each line into the ScriptWriter of a generateScript method, but you could easily store each line in a String instead.

Thanks the missing info will be the getClass().getResourceStream();
The rest is OK as follows.
Thanks Sam!

1 Like