Home > database >  No such file or directory in Intellij while working with filereader
No such file or directory in Intellij while working with filereader

Time:08-06

I'm new to IntelliJ. I'm running into problems on running very basic File I/O programs.

import java.io.*;
import java.util.Scanner;

public class NamePlaces {
    public static void main(String[] args) throws FileNotFoundException{
        String nameTxt = args[0];
        String placeTxt = args[1];

        Scanner nameScanner = null;
        Scanner placeScanner = null;
        try{
            FileReader names = new FileReader(nameTxt);
            FileReader places = new FileReader(placeTxt);

            nameScanner = new Scanner(new BufferedReader(names));
            placeScanner = new Scanner(new BufferedReader(places));

            while(nameScanner.hasNext() && placeScanner.hasNext()){

                System.out.format("%s lives in %s \n",nameScanner.next(),placeScanner.next());
        }

    }finally {
        if (nameScanner != null){
            nameScanner.close();
        }
        if (placeScanner != null){
            placeScanner.close();
        }
    }
}

here's my project structure enter image description here

and here's my output

   /usr/lib64/jvm/java/bin/java -javaagent:/home/abhishek/intellij/lib/idea_rt.jar=42185:/home/abhishek/intellij/bin -Dfile.encoding=UTF-8 -classpath /home/abhishek/Documents/code/ideaprojects/files/out/production/files NamePlaces names.txt places.txt
        Exception in thread "main" java.io.FileNotFoundException: names.txt (No such file or directory)
            at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
            at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
            at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
            at java.base/java.io.FileReader.<init>(FileReader.java:60)
            at NamePlaces.main(NamePlaces.java:12)
        
        Process finished with exit code 1

So I've been wrestling with this problem for quite some time now. I do not understand why it won't read the file names.txt when it is available in the src folder as well as the out/production/files folder too. What am I missing. I've tried changing directories, classpath seems right too. I'm out of options here. Any help is appreciated. thanks.

CodePudding user response:

In Java if you are not using class.getResource() it will be looking in the project/surrounding folder. If you compiled the .class file by hand it would work in its current state. But to fix the issue moving names.txt and places.txt to the files project folder will fix it.

CodePudding user response:

I am able to run the program. I however could not figure out a simpler way than this.

So I changed my code by adding

import java.nio.file.Path;
import java.nio.file.Paths;

to the imports.

And inside the main(), I added the following statement to get the path.

Path path = Paths.get('path/to/directory/');

And then I used this path to hard code the path into the call to the FileReader such as

FileReader names = new FileReader(path.toString() "/" namesTxt);

This got the program running. I know this is not the optimum or the desired solution but at the moment, I wanted the program to run.

I will update the answer once I learn more about how the IntelliJ works in identifying files because hard coding paths is not a good software design practice.

  • Related