I have config.properties file in src/main/resources folder having a property as "driverFilePath=C:\\VSP\\workspace\\Drivers\\IEDriverServer_Win32_3.5.0\\IEDriverServer.exe".
Here I'm currently reading it by mentioning full path, now I want to read only through relative path as "Drivers\\IEDriverServer_Win32_3.5.0\\IEDriverServer.exe" instead, as the drivers are also located in svn repo.
I'm using the code as
File file = new File(driverFilePath); to get the path from local.Please suggest a way so that I can mention only relative path like "Drivers/IEDriverServer_Win32_3.5.0/IEDriverServer.exe" which will match both my local and also the svn location.Hence I won't be making any change in my code.
Related
I am using IDEA 14 to follow a simple Java tutorial (JDBC). As part of this, I am storing some configuration properties in a file called jdbcTutorial.properties. When I put this in the root directory of the project, I can read it with the following:
Properties props = new Properties();
props.load(new FileInputStream("jdbcTutorial.properties"));
However, as soon as I move it to any other directory in the project, I get the error "No such file or directory". This happens regardless of whether I specify a relative or absolute path:
Maybe there are more standard ways to use config files, but I would really like to understand the behavior I am observing. Thanks for helping!
By default root directory would be added to your project's build path. Since the directory in which you are putting the file is not added in your project's build path jvm is unable to find the file. You have two options:
Add the folder where you are putting your prop to build path.
Access the file with full path i.e. /home/user/workspace/....
When you build a project, IDEA takes the files in the resources directory and puts them in the executable jar. So to get an input stream from that file, you need to get it directly from inside the jar. Instead of FileInputStream, use
getClass().getResourceAsStream("jdbcTutorial.properties")
When no path is supplied for a file, java assumes that this file is in the project's root folder. So the relative path "." always points to this folder. When the file is somewhere else, put the appropriate relative path before the path name, like "./files/configuration/jdbcTutorial.properties".
It work also when you put an absolute file path before the path name, like "C:/Users/Me/Documents/Java/workspace/thisProject/files/configuration/jdbcTutorial.properties".
I'm working on a project for school that needs to read from text files inside the project directory. I have it working but only because I have the filepath hardcoded to my computer.
i.e.
String path = "C:\\Users\\MyName\\workspace\\ProjectName\\"
If I sent it to my teacher, the filepath would result in an error.
Is there a way I can set the filepath to wherever the project is stored, from inside the project?
Just put the file name.
String path = "XPTO.txt"
This means your file is in the project root.
Resource files can be place relative to the class files in your project (in this manner, they can be packaged together with class files as a jar file). To access these from within your project, you can use Class.getResource() or Class.getResourceAsStream. For instance...
InputStream is = MyClass.class.getResourceAsStream('path/to/file');
Where 'path/to/file' is the path relative to where MyClass resides. Note the lack of a '/' at the beginning of this path - if it began with '/' it would be an absolute path relative to the highest package level of the project. Also note that one can use a relative file path to read a file external to the class package directory structure.
Just do WhateverNeedsAPath("Something") //path will be whereever/ProjectName/Something
This might help you also: How to define a relative path in java
Hey is it possible to get an image from file without using the full file location in java? My code below will only work with the full file path:
Icon icon = new ImageIcon("/Users/MyMac/Documents/Project/Software/Project/src/UI/Images/default_pic.png");
Is it possible to use the file path as such?:
Icon icon = new ImageIcon("/src/UI/Images/default_pic.png");
"/src/UI/Images/default_pic.png" is an absolute path, so it will look for a src directory in the root directory, then a UI subdirectory in it, etc. Not what you want.
You can use a relative path such as "src/UI/Images/default_pic.png" (notice it doesn't start with a "/"), but as its name says, it is relative to the current directory. So it will work if your current directory is /Users/MyMac/Documents/Project/Software/Project (or any directory that contains the file in the same subpath), otherwise it won't.
Finally, another way is to access the file through the classpath. Considering that the project could be packed in a jar file, the image file might not be a separate file on the disk, but you can still get a URL or InputStream to access it. Search for getResource and getResourceAsStream in Class and ClassLoader.
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 want to use a File object to read a local file in the same directory as a groovlet. However, using a relative path to the file (either "example.txt" or "./example.txt") doesn't do the trick. If I give it an absolute path (e.g., "/example.txt"), then it works.
Is there any way to get the working directory or context path of the groovlet programmatically?
new File("${request.getContextPath()}/example.txt")