This question already has answers here:
Java, reading a file from current directory?
(8 answers)
FileNotFoundException even when the file is there
(3 answers)
Closed 9 days ago.
I'm not entirely sure if this is a Java issue or an IntelliJ issue, but I had a quick question about my File not being found via a path. My Main class is in the same directory as my input.txt file.
I though I should be able to do File file = new File("./input.txt"), except I get FileNotFoundException. When I do something like File file = new File("src/input.txt"), it does work.
I get that this may be a solution, but if I try to run this code outside of IntelliJ without a src directory, this will lead to an error where the File can't be found.
Is there any reason why I can't just do ./input.txt to specify that the File is in the same directory as the Main file?
That's because in IDEA, the working directory of your program is the root directory of your project, not src by default. Therefore, you can access the file by ./src/input.txt or src/input.txt, not input.txt. By the way, it's bad practice to put your resources in src, because it gets mixed with your source code, and it won't be included in the JAR file, which means you cannot access it when your project is packaged as a JAR file. If you are using maven, you can put input.txt inside the resources folder(src/main/resources), and access it by Main.class.getResource("/input.txt")(gives you a URL) or Main.class.getResourceAsStream("/input.txt")(gives you a InputStream) depending on your needs. So that the resources will always be available, no matter you are running the program in IDEA or from a JAR.
FileNotFoundException occurs when the file doesn't exist at the specified location.
File file = new File("./input.txt"), the current working directory (.) is being used as the file location.
If you are running the code from an IDE, the current working directory is usually the project root directory, not the directory where the code file is located.
So, if the input.txt file is located in the same directory as your code file, then you should use the relative path to it.
File file = new File("src/input.txt"), you are specifying the absolute path to the input.txt file, which is inside the src directory.
This is why it works, as the file is guaranteed to exist at that location.
If the file is located in the same directory as your code file, you can use a relative path, and if it's in a different directory, you should use an absolute path to it.
Related
I have a folder called lib that contains all my Jar files and in one of the Jar files class, I have a main method which is called by a batch file. In the same folder location as my lib, I have another folder structure path/to/a/resource/myresource.txt
How can I load this file from a class inside the Jar file? I tried the following and both resulted in null:
getClass().getResource("path/to/a/resource/myresource.txt")
getClass().getClassLoader().getResource("path/to/a/resource/myresource.txt")
Any ideas? Even with an absolute path, it failed! Any suggestions?
You can use:
getClass().getResourceAsStream("path/to/a/resource/myresource.txt")
However, for this to work, you need to add the path '.' to the Class-Path entry of the JAR's MANIFEST.MF file.
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Two things you tried are used to read files from class-path since this folder is not on your classpath you can read it directly with any of the java File IO classes.
File file = new File("C:/folder/myFile.txt");
or if you know the relative path:
File file = new File("../../path/myFile.txt");
Your path seems not to be precise enough. Further, this question has been worked before.
Have a look here:
How to Load File Outside of, but Relative to, the JAR?
How to get the path of a running JAR file?
You can either load the file from file system
new FileReader(relativeOrAbsoluteFilesystemLocation)
or you can add the directory in question to your classpath:
java -cp "lib/*;lib" ...
and then use your original method.
(Unix uses : rather than ; as classpath separator)
This question already has answers here:
Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)
(2 answers)
Closed 8 years ago.
I'm developing a custom plugin for Eclipse and I have a powerpoint file named "extract.ppt" that I want to open through my source code.
When I place the file on my projects root diercotry and use:
File file = new File("/extract.ppt");
the file opens just fine.
But since im gonna be using a few more files on my application, I thought it would be a good idea to keep them all organised under a folder. So I created a folder named "files" under my main project folder and tried to use:
File file = new File("/files/extract.ppt");
but I get an error saying the file does not exist. I checked my Eclipse project's folder and the folder "files" as long as the "extract.ppt" are there.
Any ideas?
Use this instead:
File file = new File(".\\files\\extract.ppt");
The ".\\files\\..." is like saying LOOK on current directory IN folder files IN ...
You should use relative path:
File file = new File("./files/extract.ppt");
the files folder should be under the project.
Just like .setting or bin folder
reader = new CSVReader(new FileReader("file.txt"));
The file is placed in the same directory as the java class. Anyhow I get FileNotFoundException.
What is wrong?
Relative paths are relative to the current working directory. In your code sample, if file.txt isn't in your current directory, it won't be found.
Be wary of using relative paths in your code. That's because it's impossible to tell at compile time what the current working directory will be when your code is run.
If the file is part of your deployment, store it on the classpath and access it via ClassLoader.getResourceAsStream(), if it's truly external data that the user can change, put the file name in a configuration of some sort.
File needs to be in the root folder of the project.
Your application doesn't run in that directory. You would either have to use an absolute (or classpath-relative) path, or move the file to the directory where the application runs. In Eclipse, for example, this would be the root folder of your project.
Suppose I have a Java class that needs to access a file with absolute path
/home/gem/projects/bar/resources/test.csv:
package com.example
class Foo {
String filePath = ????? // path to test.csv
String lines = FileInputStream(new File(filePath).readAllLines();
}
Where the path to Foo.java is /home/gem/projects/bar/src/com/example.
Of course I cannot specify absolute path to the resource file. This is because jar file will be distributed as library for any clients to use in their own environments.
Assume the resource file like test.csv is always in the same path relative to project root. When a jar is created containing Foo.class, this jar also contains test.csv in the same relative path ( relative to project root).
What is the way to specify relative path that would work no matter where the project bar is moved to? Also how can I create a jar file (which can be in any location) so that the path to the resource file test.csv would still be correct.
To keep things simple, I have used invalid Java API ( readAllLines() which reads all the lines and return a string containing entire file content. Also not using try/catch).
Assume csv file can be read as well as written to.
I hope this makes it clear now.
Put the test.csv file into the src folder and use this:
Foo.class.getResourceAsStream("/test.csv")
To get an InputStream for the file. This will work wherever the project is moved, including packaged as a JAR file.
Example:
ProjectX\src\Test.java
ProjectX\resources\config.properties
If you have the above structure and you want to use your config.properties file, this is how you do it:
InputStream input = new FileInputStream("./resources/config.projects");
In this example you don't have to worry about packaging your source into jar file. You can still modify your resources folder anytime.
Use getResource(), as shown here.
I have a scanner that's trying to read a file named info.data in the src folder.I get Exception in thread "main" java.io.FileNotFoundException: info.data (The system cannot find the file specified). What's the address I should put in the scanner?
If the input file is always part of your application (i.e. you also put this into the .jar file later) you should use getResourceAsStream() in order to read its contents.
InputStream in = getClass().getResourceAsStream(filename);
Scanner scanner = new Scanner(in);
In netbeans, the src folder isn't the destination of the compiled classes, so if you are using a relative path, the location your program launches is not going to be the src folder.
That means you typically should "extend" your build to copy a non-source file into the build path if you want it to operate in the manner you imply. Many files already copy over to the build path (like properties files), but if you are including a data file that doesn't have a rule for being place in the build path, you need to add the rule yourself.
Try putting the path to it.
File f = new File("C:\\path\\src\\info.data");