2

I have a csv file in the same path as everything else. Now, when I try to create a File object:

public void getMenu() {

    File fileMenu = new File("FastFoodMenu.csv");

    try {
        Scanner inputStream = new Scanner(fileMenu);
        while (inputStream.hasNext()) {
            String data = inputStream.next();
            System.out.println(data);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

}

it throws a FileNotFoundException.

the absolute path to all files in the project is:

C:\Users\kenyo\Documents\NetBeansProjects\OrderFastFood\src\fastfoodorderingsystem

I also checked the name a couple of times. fileMenu.exists() returns false.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
chris
  • 47
  • 2

2 Answers2

2

First, in your root/working directory (in your case it's the folder containing your project), create a folder called 'menus', here you can store all your menus (so you can play around with multi-file input).

Second, move your FastFoodMenu.csv file to that menus folder. The FastFoodMenu.csv relative path should now look like this: OrderFastFood\menus\FastFoodMenu.csv.

Third, get your working directory from the System properties. This is the folder in which your program is working in. Then, get a reference (File object) to the menus folder.

Lastly, get a reference to the file in question inside the menu folder. When you get to multi-file reading (and at some point, multi-folder reading), you're gonna want to get the files inside the menu folder as a list so that's why I say to just get the menus folder as it's own reference (or just get the file without the isolated reference to the parent aka '\menus\').

So your code should really look like this:

public void getMenu() {
    final File workingDir = File(System.getProperty("user.dir"));
    final File menusDir = File(workingDir, "menus");
    final File fastFoodMenu = File(menusDir, "FastFoodMenu.csv");

    try {
        final FileInputStream fis = new FileInputStream(fastFoodMenu);
        final BufferedInputStream bs = new BufferedInputStream(fis);
        while((l = bs.readLine()) != null) {
            System.out.println(l);
        }
    } catch(FileNotFoundException e) {
        System.out.println(e.getMessage());
        e.printStackTrace()
    }
}

This is all psuedocode but that should at least get you started. Make sure to use BufferedInputStream for efficiency, and when reading files, always pass them into FileInputStream's. It's much better than using the Scanner class. I should also mention that when creating a File object, you're not actually creating a file. What you're doing is your're creating an object, giving it the data you want it to have (such as whether it's a folder, and if it is, what child files/folders do you want it to have, whether it's protected or not, hidden or not, etc) before actually telling the system to create the file with everything else.

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
Alex Couch
  • 151
  • 4
  • I think it should be sufficient to simply use relative paths here as they will be resolved against the working directory nevertheless. So no need to query and append the system property `user.dir`. – isnot2bad Sep 20 '18 at 14:55
  • I hope you don't mind, but I've edited your code and removed the `String.format` stuff in favor of the more convenient `File` constructor that takes the parent directory as first argument. – isnot2bad Sep 20 '18 at 15:00
  • Yeah I totally forgot about that construction thanks! I knew there was a better i just totally forgot. And I wouldn't really rely on paths being resolved against the working directly. I've had problems with that in the past so I always try getting the user.dir property for insurance. But its still something to think about! – Alex Couch Sep 20 '18 at 22:33
  • Okay for everybody else reading this, I want to point out my above comment. It may be a bit hard to read but I was on mobile and my keyboard absolutely sucks. It overcorrects things. What I was trying to say was I forgot about the File(parent, path) constructor (not construction). Not only that but I was trying to say that I don't like letting the VM or my IDE resolve paths against the working directory. I'd rather do it manually so that I know for a fact it's using the working directory. Just for clarification lmao. – Alex Couch Sep 24 '18 at 22:31
0

Your csv file is probably at the wrong place. You're just specifying the file name, which is a relative path.

Relative paths are always resolved against the working directory of your application, not against the directory where your source file(s) are.

To solve the issue, you can

  • move the files to the real working directory.
  • use an absolute path (not advisable!)
  • specify the folder of your data files as program argument or in a config file (in your working directory)
  • put the files somewhere into the classpath of your application and load them from there via classloader. Note that files that are in your classpath are usually packed with your application and hence not easily modifiable by the user, so this solution doesn't work if the file must be changed by the user.
isnot2bad
  • 24,105
  • 2
  • 29
  • 50