1

I am trying to load a file which is present in the resources folder, but getting an exception. I am using java 9, and the java file that has the code to read the file is present in some other module and the calling code is in some other module. Can some one please suggest how to proceed here?

enter image description here

Exception stacktrace

java.nio.file.NoSuchFileException: genome-tags.csv
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:364)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:410)
    at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
    at java.base/java.nio.file.Files.newInputStream(Files.java:154)
    at java.base/java.nio.file.Files.newBufferedReader(Files.java:2809)
    at java.base/java.nio.file.Files.readAllLines(Files.java:3239)
    at java.base/java.nio.file.Files.readAllLines(Files.java:3279)
    at com.bhargo.filesystem.reader/com.bhargo.filesystem.reader.FileSystemReader.read(FileSystemReader.java:15)
    at com.bhargo/com.bhargo.Main.main(Main.java:20)

The code is:-

    public class Main {

        public static void main(String[] args) {
            ServiceLoader<IReader> serviceLoader = ServiceLoader.load(IReader.class);
            try {
                System.out.println(serviceLoader.iterator().next().read("genome-tags.csv", Main.class).size());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public interface IReader {
    List<String> read(String fileLocation, Class clazz) throws IOException;

    default File getFile(String fileLocation, Class clazz) {
        URL url = clazz.getResource(fileLocation);
        return new File(getClass().getClassLoader().getResource(fileLocation).getFile());
    }
}


    public class FileSystemReader implements IReader{
        @Override
        public List<String> read(String fileLocation, Class clazz) throws IOException {
            return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getName()));
        }
    }

Thanks,

Amar

Amar Dev
  • 1,360
  • 2
  • 18
  • 38
  • 1
    This line `return new File(getClass().getClassLoader().getResource(fileLocation).getFile());` try uing absoluatePath() – Minh Kieu Jun 04 '17 at 17:59
  • I tried /resouces/, but I get NPE – Amar Dev Jun 04 '17 at 18:05
  • I tried absoluatePath() but get the same exception – Amar Dev Jun 04 '17 at 18:08
  • 1
    base on the exception, getName() only return the file name...try calling the .getAbsolutePath() which should print the full path including the name. Add a system.out.println() and see what value this is? BTW, if your file is BIG, reading all lines into a List may not be a good idea. You should consider using stream. – Minh Kieu Jun 04 '17 at 18:09
  • 2
    changed Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getName())) to Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getAbsolutePath())) and now it works – Amar Dev Jun 04 '17 at 18:23
  • 1
    Good one. Please accept the answer to complete your question. Thanks. – Minh Kieu Jun 04 '17 at 18:39
  • 1
    But what’s the point of the `new File` detour? That’s wrong semantic, in *all* Java versions. `clazz.getResource(…)` already returned a readable `URL`, not necessarily a `file:` URL. If you want to turn to NIO from there, you may simply use `Paths.get(url.toURI())` to convert it to a path, though it’s more reliable to use `url.openStream()` (or `getResourceAsStream()` in the first place) and read that instead of relying on a `FileSystem` implementation… – Holger Jun 06 '17 at 15:12

1 Answers1

3

Change

public class FileSystemReader implements IReader{
        @Override
        public List<String> read(String fileLocation, Class clazz) throws IOException {
            return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getName()));
        }
    }

to

public class FileSystemReader implements IReader{
        @Override
        public List<String> read(String fileLocation, Class clazz) throws IOException {
            return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getAbsolutePath()));
        }
    }
Minh Kieu
  • 475
  • 3
  • 9