1

I am trying to read a file which resides in eclipse src folder but I keep getting file not found.

public static void main(String[] args) {
    try {
        File file = new File("/PMIS/src/config/PMIS_Request_Template.xml");
        FileReader fr = new FileReader(file);
        
        BufferedReader br = new BufferedReader(fr);
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        
        while(line != null) {
            sb.append(line);
            sb.append("\n");
        }
        fr.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }   
} 

enter image description here

Joshua Wade
  • 4,755
  • 2
  • 24
  • 44
Sara Noor
  • 35
  • 8
  • Use resources. src folder is for code only and doesn't exist when your code is compiled. https://mkyong.com/java/java-read-a-file-from-resources-folder/ – OneCricketeer Sep 15 '20 at 15:59
  • Method [getAbsolutePath](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#getAbsolutePath--) in class `java.io.File` will show you the actual path that java is using in order to locate the file. Also note that the Eclipse build automatically copies resource files from the _src_ folder to the _bin_ folder. – Abra Sep 15 '20 at 17:58

3 Answers3

0

A path starting with a slash indicates an absolute path, so under windows that would be something like a drive called PMIS, under Linux a base folder with that name in your mounted filesystem. In fact, you want to open a file relative to your working directory, so you've got to start with ./PMIS/src/config/PMIS_Request_Template.xml (with the initial dot indicating the current working directory) or with PMIS/src/config/PMIS_Request_Template.xml (without the leading slash). Apart from that, you want to verify that your current working directory is even what you expect it to be (i.e. the eclipse folder). You can find it out this way, for example. Adjust your path accordingly, or better, make the path flexible and allow it to be handed over from the args, so the person starting this program can specify the relative or absolute location.

Side node as you use maven: If you want to have this file statically around, you should put it into a resources folder at the same level as the src folder because it doesn't contain Java source code. Use the maven resource loading logic then - check this thread out, for example.

And, well, the standard folder structure for Maven is a bit different. So if you haven't modified these settings manually, you should fix that: move src to main/src and resource files into main/resources, or you'll get into trouble later.

NotX
  • 1,516
  • 1
  • 14
  • 28
0

/PMIS/src/config/PMIS_Request_Template.xml Replace this line with: //PMIS//src//config//PMIS_Request_Template.xml

0

I first list some issues:

  • File file = new File("/PMIS/src/config/PMIS_Request_Template.xml"); The file could reside with the class, as resource on the class path. Then it is not a File (disk system file) but could be in the application jar. Now it might only work with the IDE, as src is for the sources.
  • FileReader fr = new FileReader(file); FileReader is an old utility class that uses the default platform encoding, so that could vary from user to user, which is dead wrong for an XML you pass.
  • while(line != null) { ... } Missing a br.readLine();

Place the XML as part (resource) of the application. If the application is delivered as jar, open it with 7zip or such and check the path.

try {
    URL xmlURL = YourClass.class.getResource("/config/PMIS_Request_Template.xml");
    // xmlURL will be null when the path is incorrect.
    Path path = Paths.get(xmlURL.toURI());
    byte[] xmlBytes = Files.readAllBytes(path);
    String xml = new String(xmlBytes, StandardCharsets.UTF_8);
} catch (Exception ex) {
    ex.printStackTrace();
}   
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138