How to read the xml file in netbeans - java

I am using net beans IDE for development and keeping all my xml files under the folder XML(created under Web Pages folder) ..
I am using the following code to read the xml file .
File file = new File("XML/TableNamesAndColumnNames.xml");
but it is giving file not found exception ..
Can any one suggest how to read the file
Thanks in Advance
Raj

Probably the path you are specifying is not the correct one. Find out what you application's working directory is. Run this:
System.out.println(new File(".").getAbsolutePath());
And then adjust from there accordingly.

Related

Reading a resource file in Jar

I am using IntelliJ Idea and trying to read a json file from the resources folder in the project structure. I read the json file and return the contents using jackson.
return mapper.readValue(File("src/main/resources/file.json"), Map::class.java)
As soon i build the project and make a jar it throws me an error it cannot find the file. I looked here a bit and found that i should use ClassLoader to read files from the resources folder. So i do this now -
mapper.readValue(File( ClassLoader.getSystemClassLoader().getResource("src/main/resources/file.json").toURI()), Map::class.java)
Now I get a NullPointerException. I am a bit lost now. Any help is deeply appreciated.
Assuming your build follows the default convention, whatever under src/main/resource will be available on the root of the classpath, so you just need to change the code to:
mapper.readValue(ClassLoader.getSystemClassLoader().getResourceAsStream("file.json"), Map::class.java)

Cannot access text file in java project

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

java xml validation with relative path to glasshfish config folder

I searched a lot but couldn't find anything. I want to validate a selected xml file with an schema file. My problem is working relative path is config folder of the Glassfish server.
I tried to get url of the schema file which I copied into same package of the class I run this code but still I can't find the file. I only able to make it work is by copying the file into server config file.
URL url = ClassLoader.getSystemResource("/schema.xsd");
File file = new File(url.getPath());
But url returns null. How can I find this file and even if I find I believe file object can not be created because my relative path is /home/user/glassfish3/glassfish/domains/domain1/config. Any ideas how to solve this problem?
Thanks for any help in advance.
The leading slash makes the path absolute. If you remove it, leave just the file name and have that file in the domain/config dir then it should work.
Note that if you'd like this file somewhere else you could also provide a relative path stepping back your directory tree e.g. ../../prj/xsd/schema.xsd

Input Stream returns null for mentioned file path

I am trying to access the sample_report.jrxml file from ReportUtil.java .
Following is the code to access the jrxml file :
InputStream in = new ReportUtil().getClass().getClassLoader().getResourceAsStream("resource/sample_report.jrxml");
I am getting in as NULL. I tried various combinations to read the jrxml file.
Can any one point correct way to get the file ?
Make an entry of resources folder in your classpath.
Assuming that your project is a maven project and you follow maven conventions, then
your file sample_report.jrxml residing in resources folder will be copied into the root level of the generated jar file.
For loading files you should use:
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream( "sample_report.jrxml" );
BalusC has explained this very well here
And as Brian Roach pointed out, there is a typo in the path to your file.

Path resolution in eclipse package structure

This is a very simple question for many of you reading this, but it's quite new for me.
Here is a screenshot for my eclipse
When i run this program i get java.io.FileNotFoundException: queries.xml (The system cannot find the file specified) i tried ../../../queries.xml but that is also not working. I really don't understand when to use ../ because it means go 1 step back in dir, and in some cases it works, can anyone explain this? Also how can I refer to queries.xml here. Thanks
Note: I might even use this code on a linux box
I assume it is compiling your code into a build or classes folder, and running it from there...
Have you tried the traditional Java way for doing this:
def query = new XmlSlurper().parse( GroovySlurping.class.getResourceAsStream( '/queries.xml' ) )
Assuming the build step is copying the xml into the build folder, I believe that should work
I don't use Eclipse though, so can't be 100% sure...
Try
file = new File("src/org/ars/groovy/queries.xml");
To check the actual working directory of eclipse you can use
File f = new File(".");
System.out.println(f.getAbsolutePath());
You could try using a property file to store the path of the xml files.
This way you can place the xml files in any location, and simply change the property file.
This will not require a change/recompilation of code.
This would mean you will only need to hardcode the path of the property file.
If you prefer not hardcoding the path of the property file, then you could pass it as an argument during startup in your server setup file. (in tomcat web.xml). Every server will have an equivalent setup file where you could specify the path of the property file.
Alternatively you could specify the path of the xml in this same file, if you don't want to use property files.
This link will show you an example of reading from property files.
http://www.zparacha.com/how-to-read-properties-file-in-java/

Categories

Resources