0

I have a xml file placed at location pkgName/src/main/resources/Config.xml

I am trying to read it from java file as

package com.xstreamparsing.parsers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.URL;

import javax.security.auth.login.Configuration;

import com.thoughtworks.xstream.XStream;

public class XStreamConfigParser {

    public static void main(String[] args) throws FileNotFoundException {

        URL url = XStreamConfigParser.class.getResource("config.xml");
        File file = new File(url.getPath());

        FileReader fileReader = new FileReader(file);  // load our xml 

When i run the program i am unable to read it. i followed the link :

Reading a file from a relative path

Unfortunately i could not succeed. Please suggest best and universal file to read file from java class.

Community
  • 1
  • 1
Scientist
  • 1,458
  • 2
  • 15
  • 31

1 Answers1

2

XStreamConfigParser.class.getResource("config.xml");

This will search relative to XStreamConfigParser, in this case in the same package.

Try instead:

XStreamConfigParser.class.getResource("/config.xml");

This will start looking at the root of your output dir/ jar file.

Alternatively move the file to:

src/main/resources/com/xstreamparsing/parsers/config.xml

Also make sure sure your using the correct case (Config.xml vs config.xml).

Puce
  • 37,247
  • 13
  • 80
  • 152