I would like to know how to add files to my NetBeans project and then access them via getResource or getResourceAsStream. But apparently I can't figure out where my problem lies.
I added some xml file into the root folder of the project and when I try to access it via
InputStream is = this.getClass().getClassLoader().getResourceAsStream("some_file.xsd");
I get null as a result.
Make sure the some_file.xsd is present in the classpath, then also verify if the path to the file is proper.
If not you can instead just read it using FileInputStream. Something like this:-
InputStream fileStream = new FileInputStream(filePath);
and make sure that filePath is a proper relative URL.
Related
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 writing a simple JSF application and am trying to get a resource (database.properties file) using Class, ClassLoader, and URL that is not working. url is null and I don't know why. I have done a lot of research but without success.
Code:
Class cls = Class.forName("<packagename>.SimpleDataSource");
ClassLoader cLoader = cls.getClassLoader();
URL url = cLoader.getResource(fileName); // fileName = "database.properties" w/o the double quote
FileInputStream in = new FileInputStream(url.getFile());
Thanks for your comment Sandeep, it was helpful. I found out that my properties file was in the wrong place. I then moved it inside my src folder under the java resources folder and now my properties file gets loaded in. I have a new problem now but will start a new thread if I can't figure it out.
Once you have a URL you can get an InputStream from it using url.openStream(), or you can simply use cLoader.getResourceAsStream(...) in the first place. Your current approach of
FileInputStream in = new FileInputStream(url.getFile());
will work on some platforms but not all, and only when your application is running from a directory on disk. It will fail if your classes and resources are packed up into a JAR, but if you use getResourceAsStream it will work from a JAR as well as an unpacked directory.
I need to read the configuration details from a properties file in eclipse. I have put the config.properties at the same level as plugin.xml and in the class file I call:
Properties properties = new Properties();
FileInputStream file;
String path = "./config.properties";
file = new FileInputStream(path);
properties.load(file);
I get a file not found exception. Is there a better way of doing this?
Did you remember to include it in the build?
Secondly, using the classloader resource is probably better anyway
InputStream fileStream = myClass.getResourceAsStream( "/config.properties" );
Also, there is another way of opening a resource URL in eclipse using
url = new URL("platform:/plugin/com.example.plugin/config.properties");
InputStream inputStream = url.openConnection().getInputStream();
Put the properties file in the root of your project. This should be where the user.dir system property is pointing. The FileInputStream constructor looks in this directory for the file.
You can confirm it is in the correct directory by outputting the System Property.
System.getProperty("user.dir");
If it is a plugin, you should tell it that your file must be included into the target. It is set in the build.properties file. Open it and look - you haven't your file mentioned there.
Open plugin.xml, go to build tab and include your file in the binary.build by checking it. Save. That will change the build.properties file correctly. Go to build.properties tab and look at the file again - now you see your file included correctly.
Of course, you could do it by hand, but plugin UI is convenient and you can be sure you haven't any problem ends of line or spaces.
For absolute addressing the files in the plugins look here
InputStream inp = new FileInputStream("src/main/resources/ExportHour.xls");
I have a file in the src/main/resources folder of my Java Spring project.
I am attempting to create an inputstream in one of my Controllers, however I always get a file not found exception. When I change the path location to point specifically to the file on my machine, it works fine.
Any way I can make it so the file can be found within the java project?
Try with spring ClassPathResource.
InputStream inp = new ClassPathResource("ExportHour.xls").getInputStream();
That is because the resources folder in maven is put in your jar file directly i.e. the ExportHours.xls file is put inside your jar in the root directory.
It sounds like you could just change the working directory of your process - it's not where you think it is, I suspect. For example, I suggest you write
File file = new File("src/main/resources/ExportHour.xls");
and then log file.getAbsolutePath(), to see what exact file it's using.
However, you should almost certainly not be using a FileInputStream anyway. It would be better to use something like:
InputStream inp = Foo.class.getResourceAsStream("/ExportHour.xls");
... for some class Foo which has a classloader which includes the resources you need.
(Or possibly /resources/ExportHour.xls", depending on your build structure.)
That way even when you've built all of this into a jar file, you'll still be able to open the resource.
In my Maven project, I have the following code in the main method:
FileInputStream in = new FileInputStream("database.properties");
but always get a file not found error.
I have put the file in src/main/resources and it is properly copied to the target/classes directory (I believe that is the expected behavior for Maven resources) but when actually running the program it seems it can never find the file. I've tried various other paths:
FileInputStream in = new FileInputStream("./database.properties");
FileInputStream in = new FileInputStream("resources/database.properties");
etc. but nothing seems to work.
So what is the proper path to use?
Based on "disown's" answer below, here was what I needed:
InputStream in = TestDB.class.getResourceAsStream("/database.properties")
where TestDB is the name of the class.
Thanks for your help, disown!
You cannot load the file directly like that, you need to use the resource abstraction (a resource could not only be in the file system, but on any place on the classpath - in a jar file or otherwise). This abstraction is what you need to use when loading resources. Resource paths are relative to the location of your class file, so you need to prepend a slash to get to the 'root':
InputStream in = getClass().getResourceAsStream("/database.properties");