I want to open a file in Java Class in an AWS Java Web project via Eclipse.
I have my file in a folder called "res" in
I tried this
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("res\\txtFile.txt"), "UTF-8"));
but not working!
I got
java.io.FileNotFoundException: res\txtFile.txt (The system
cannot find the path specified)
If the file is inside the weapp, you want ServletContext.getResourceAsStream or Class.getResourceAsStream. If it is somewhere else on the filesystem you should probably use an absolute path. A relative path like you used is resolved relative to the directory your appserver started from which might not be what you want.
I solved it !!
used this code to get the absolute path of project anywhere
String AbsolutePath = new File("").getAbsolutePath();
then add the relative path you need.
Related
I have a config.ini file I need to open that is quite far back in directory so I used File.getAbsolutePath() to set the base directory and concatenated the remainder of the path.
Printing the path, I get the correct path that I can paste into file explorer, but the object returned is null.
So I started by initializing my Properties and ClassLoader as such:
Properties prop = new Properties();
ClassLoader classLoader = Test.class.getClassLoader();
Then I create the path. I tried escaping a back slash(1) as well as forward slash(2), both return null but both paths work in file explorer.
String absPath = new File("").getAbsolutePath();
absPath = absPath.concat("\\resources\\config\\config.ini"); // (1)
absPath = absPath.concat("/resources/config/config.ini"); // (2)
then I try to set the URL to open an InputStream
URL res = Objects.requireNonNull(classLoader.getResource(absPath), "Unable to open config.ini");
InputStream is = new FileInputStream(res.getFile());
However, the following returns null.
classLoader.getResource(absPath)
I expected this to properly open the file because the path was correct. I am using Intelij and I read that I needed to add the .ini resource file under settings > compiler, which I did but that did not resolve my issue.
Thank you!
That's not the way to load resources via class-loaders.
If your classpath is something like the following ...
java -cp resources;lib/my.jar ... org.mypack.MyClass
then you load it with this path
getClassLoader().getResource("/config/config.ini");
Your classpath includes the resources folder, and the class-loader loads from there.
The absolute path of the OS is quite certainly not in classpath.
In any case, you must be certain that the resource folder is in classpath.
If your config file is not in classpath then you can't use classloaders to load that file.
Just one more thing, if your configuration is not in classpath, but in a child directory of your working dir, why can't you simply use new FileInputStream("resources/config/config.ini");
I am trying to access a text file, output.txt which is inside a project folder. The image below shows the folder structure.
String file=".\\testFiles\\output.txt";
BufferedReader br=new BufferedReader(new FileReader(file));
When I try to access that file using the code above, I get the following exception.
java.io.FileNotFoundException: .\testFiles\output.txt (No such file or directory)
I have tried different file path formats but none have worked. I think the problem is with the file path format.
Thanks in advance.
If I remember correctly you can get a folder/file in the current directory like so:
File folder = new File("testFiles");
Then you can open the file by getting the absolutePath and creating a new file with it, like so:
File file = new File(folder.getAbsoluteFile() + File.separator + "output.txt");
I'm not sure but I think you can also do:
File file = new File("testFiles/output.txt");
I hope this helps :)
P.S. this is all untested so it might not work.
Judging by the fact that you have a webcontent folder i presume this is a web project, possibly packaged as a war? In this case what you will want to do is package the respective files along with the classes and access it with something like this:
Thread.currentThread().getContextClassLoader().getResourceAsStream("output.txt")
The code above will work if you add the testFiles folder as a source folder (this means it will get packaged with the classes and be available at runtime)
The good thing is that this way the path can stay relative, no need to go absolute
I believe that your problem is due to the fact that you rely on a relative path as your path starts with a dot which means that it will by relative to the user directory (value of the system property user.dir) so I believe that your user directory is not what you expect. What you could do to debug is simply this:
System.out.println(new File(file).getAbsolutePath());
Thanks to this approach you will be able to quickly know if the absolute path is correct.
You must declare your file as a new file:
File yourFile = new File("testFiles/output.txt");
I am pretty new to Maven, and having some trouble reading files. Specifically, my program takes the absolute path of a file as input from the user, and then parses it. Unfortunately I am unclear on how to get my application to read a file as input from an arbitrary location.
Before I started using maven on the project, I used this code successfully:
String absolutePath = "/Users/akhalsa/path/to/file.txt";
inputStream = new BufferedReader(new FileReader(absolutePath));
However, since migrating to maven, this seems to have stopped working. From what I have read in maven I should use
InputStream in = getClass().getResourceAsStream(filePath);
Where filePath seems to be the relative path of the file in question. Does getResourceAsStream require that the file being read be inside the jar? Can this file be an external file's absolute path? When I use an absolute path here it says "Resource not found".
This must be a common problem in terms of letting users input a file from the file system for a maven application to process. What is the best way to this?
Thanks in advance.
getResourceAsStream() finds the resource on a path known to the jvm, so you can't load arbitrary files.
Maven does no magic trickery so if you are using actual absolute paths, the code should keep on working.
The "Users" part of the path reminds me of windows, but the path is not a valid windows path so are you sure you are passing along a valid absolute path?
I am working on a simple web application in NetBeans where I am getting FileNotFoundException. I have stored files in class path, so I need to use relative paths. When I tried with absolute path, it worked fine for me.
Below image shows my file system hierarchy.
I need to write content data in file DBList.txt.
My code is:
File file = new File("data/application/DBList.txt");
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
I have searched lot but not getting solution for reading file using relative path.
The path is relative to the working directory of the server, not your project in NetBeans. Given your FNFE I suspect that the directory structure data/application/ doesn't exist under the working directory.
What server are you running and how are you starting it? You can figure out the server's working directory by logging;
File wd = new File(".");
log.debug("working dir: " + wd.getAbsolutePath());
Edit:
The File class and the classpath are totally unrelated concepts. Don't confuse the two. If you are looking to use classpath resources have a look at the getResource() method in ClassLoader.
try to use "\\" instead of "/"
I am trying to read a file like :
FileInputStream fileInputStream = new FileInputStream("/com/test/Test.xml");
I am always getting the file not found exception. How can i make it work? Will the inputstream takes the relative path or not?
Yes, this can take relative path.
Why your expression does not work? Very simple: your path /com/test/Test.xml is absolute because it starts with /, so you are actually looking for file located in /com/test/ directory starting from root.
How to solve the problem?
I believe that you are trying to find file located under your project. So, you can use relative path like ./com/test/Test.xml or com/test/Test.xml. This will probably help. Probably because I do not know what is your current working directory and your file structure. Your current directory is where you are located when running java. If you are running from IDE typically the working directory is your project directory.
In this case I believe that path ./com/test/Test.xml is invalid because file Test.xml is not located directly under project root but somewhere under ./src/resources/com/test or so.
In this case probably you do not want to read the file as a file but as a resource (located into your classpath). In this case use
getClass().getResourceAsStream("/com/test/Test.xml")
Try using
class.getResourceAsStream(path). In that case, path will have to be relative to the folder containing the class invoking this statement.
InputStream in = getClass().getResourceAsStream("/com/test/Test.xml");
Try this,
String str = "Test.xml";
File file = new File(str);
String absolutePathOfFirstFile = file.getAbsolutePath();
FileInputStream fileInputStream = new FileInputStream(absolutePathOfFirstFile);
Your path must be incorrect. You can check the current directory by using System.getProperty("user.dir"); or printing the path of new File(".")