2

Trying to practice Java by doing basic functionality like reading input.

I am trying to parse movies-sample.txt found in:

C:\University\BigDataManagement\Data-Mining\hw1\src\main\resources\movies-sample.txt

Trying to reach movies-sample.txt from

C:\University\BigDataManagement\Data-Mining\hw1\src\main\java
   \univ\bigdata\course\MoviesReviewsQueryRunner.java

Using the answer found here on how to parse a large file line by line.

File file = new File("../../../../../resources/movies-sample.txt");

I am getting the following error:

The system cannot find the path specified

Given the above two paths, what am I doing incorrect?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • 1
    The thing you are doing incorrect is that Java is not a scripting language, it doesn't run from where your `class` is located and it **certainly** doesn't run from where your source is located. Just use the full path. – Boris the Spider Mar 16 '18 at 08:09
  • 3
    Your home directory is not the path of your file but the folder where your src is located in. So it's C:\University\BigDataManagement\Data-Mining\hw1 (Disclaimer: That's not always true, your home may be different based on how you run your application!) – Ben Mar 16 '18 at 08:09
  • Oh I wasn't aware of that. Thanks for the enlightenment. – Tony Tannous Mar 16 '18 at 08:10
  • 1
    @Ben that should be the answer ;) – sarkasronie Mar 16 '18 at 08:10
  • Is this file to be distributed with the app., in the Jar? – Andrew Thompson Mar 16 '18 at 08:10
  • @Ben that depends on how you run the program, it's not generally true. – Boris the Spider Mar 16 '18 at 08:10
  • @BoristheSpider That's true, which is why I placed it as a comment over an answer to just quickly get the message through without creating (too much) confusion by exluding several other options ;) – Ben Mar 16 '18 at 08:11
  • Since you file is located in the resource folder (located in the src folder) the following should also work: ClassLoader cl = getClass().getClassLoader(); File f = new File(cl.getResource("movie-samples.txt").getFile()); – Flocke Mar 16 '18 at 08:11
  • 3
    If it's a web app then the `resources` folder is your root element, otherwise it will be `src` as mentioned. So you can access the file directly byits name: `new File("movies-sample.txt");` – cнŝdk Mar 16 '18 at 08:11
  • 2
    @Flocke `File` doesn't work with opaque paths, and whilst your solution will work for running the code locally, it will break as soon as the code is packaged. That is what `ClassLoader.getResourceAsStream` is for. – Boris the Spider Mar 16 '18 at 08:13
  • 1
    @chŝdk again, this is not generally true. If people would stop using `File` for locations inside deployments it would make me a much happier person. It's not a `File`, it's a classloader resource. The two are far from the same. – Boris the Spider Mar 16 '18 at 08:14
  • @BoristheSpider Yes you are right, and as I said it will always depend on the situation. – cнŝdk Mar 16 '18 at 08:17

2 Answers2

2

If it's a web app then the resources folder is your root element, otherwise it will be the src folder as mentioned in comments.

In your case here as you are writing a standalone Java program and as your file is loacted in the resources folder, you can use CLassLoader to read the file as a stream.

This is how should be your code:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("movies-sample.txt");

Then you will be able to read the is stream line by line.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

If you run your program directly from command line, then the path must be related to your current directory.

If you run your program from an IDE, then the current directory of the runnin program depends on the IDE and the way it is configured.

You can determine what is the current directory with System.getProperty("user.dir")

Whatever, hard coding a path in an application is always a bad thing because you cannot ensure where the run was launched from. Either:

  • it is a user resource, then its path must be input in some way (open... in GUIs apps)
  • it is a resource needed by the app to run correctly and it should be embedded in some way into the app itself (look for Resource Bundle)
  • it is a kind of optional external resource (config file for example, or path specified in a config file) and its location should be computed in some way.
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69