Opening a local file in Groovy - java

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")

Related

How to read driverpath using relative path mentioned in config.properties file

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.

Java can't read file except when in project root directory

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".

Java - getting image from file path

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.

File object not working in Java Web App

I have a simple java class in my web application in which i have written the below code but its not working
File test= new File("/templates/xmdForModel.xsd");
templates folder is inside the root folder of the application.
the location of the file is ----> application-root/package/test.java
location of the file is --------> application-root/testRoot/template/xmdForModel.xsd
Error
Failed to read schema document 'file:/templates/xmdForModel.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
If you want to look up the file name for files inside of your web application, you can use ServletContext#getRealPath.
However, I would recommend loading your resources using the classloader with Class#getResourceAsStream. This way, it even works if the file does not really exist as a file (for example only inside of a jar).
If this is a file that a user is supposed to edit (or that you write to), I would place it outside of the web application, and then specify an absolute path (for example "/etc/myapp/conf/xmd.xsd") with a configurable prefix.
Path in the File constructor can be absolute or relative. When you start the path with '/' (in a linux based os), it is going to consider that path as an absolute path and create a file/folder in the root of your file structure (not in the root of the project). It will be like specifying c:\templates in windows machine.
Try removing the first slash and running your program. Removing first slash will make the part relative from your .java file. So you can use ../ to switch to parent folder.
java: application-root/package/test.java
file: application-root/testRoot/template/xmdForModel.xsd
So from your java file you will need to change directory to application root folder and then select template folder. Like following.
File x = new File("../testRoot/template/xmdForModel.xsd");
src:http://docs.oracle.com/javase/tutorial/essential/io/path.html
/home/sally/statusReport is an absolute path. All of the information
needed to locate the file is contained in the path string.
A relative path needs to be combined with another path in order to
access a file. For example, joe/foo is a relative path. Without more
information, a program cannot reliably locate the joe/foo directory in
the file system.

How to scan a file in a different directory in java?

How do you scan a file with java that isn't in the directory the java file is in?
For example: The java file is located at "C:\Files\JavaFiles\test.java" However, the file I want to scan is located at "C:\Data\DataPacket99\data.txt"
Note: I've already tried putting another java file in the "C:\Data" directory and using the test.java file as a class, but it doesn't work. It still tries to scan from the "C:\Files\JavaFiles" Directory.
By using an absolute path instead of a relative.
File file = new File("C:\\Data\\DataPacket99\\data.txt");
Then you can write code that accesses that file object, using a InputStream or similar.
You need to use absolute paths in java.io stuff. Thus not new File("data.txt"), but new File("C:/Data/DataPacket99/data.txt"). Otherwise it will be relative to the current working directory which may not per-se be the same in all environments or the one you'd expect.
You should be using an absolute path instead of a relative path.
You could use File file = new File("C:/Data/DataPacket99/data.txt"); but it might make your life easier in the future to use a file chooser dialog if at any point the user will have to enter a file path.
I would try this:
File file = new File("../../Data/DataPacket99/data.txt");

Categories

Resources