4

I have a json file in the same package of the controller, and I try to read the file and convert it into String

new String(Files.readAllBytes(Paths.get("CustomerOrganization.json")));

But I got an error:

java.nio.file.NoSuchFileException: CustomerOrganization.json

enter image description here

snieguu
  • 1,798
  • 14
  • 28
Sandro Rey
  • 573
  • 4
  • 15
  • 42
  • You have to provide the full path, I think. Where (on your file system) is that file stored? – deHaar Jul 24 '19 at 07:52
  • The path is not relative to the location of your class, but to the location where the program is run. Typically this is the root of the project. – Jonas Berlin Jul 24 '19 at 07:53
  • you could also try giving reative path from root of project like `src/.../controllers/CustomerOrganization.json` – michalk Jul 24 '19 at 07:54
  • If you want to find out the path to a file relative to your class, you can use `CustomerControllerIT.class.getResource("CustomerOrganization.json")` – Jonas Berlin Jul 24 '19 at 07:54
  • If you expect it in the same package, you should be using resources, **not** files. – RealSkeptic Jul 24 '19 at 07:55
  • I recommend adding a encoding parameter to the "new String(...)" call to ensure it reads the file with the correct encoding. – Jonas Berlin Jul 24 '19 at 07:57

3 Answers3

10

Using mostly the same methods you used:

new String(Files.readAllBytes(Paths.get(CustomerControllerIT.class.getResource("CustomerOrganization.json").toURI())));

However, if you need it to work from inside a JAR, you will need to do this instead:

InputStream inputStream = CustomerControllerIT.class.getResourceAsStream("CustomerOrganization.json");
// TODO pick one of the solutions from below url
// to read the inputStream into a string:
// https://stackoverflow.com/a/35446009/1356047
Jonas Berlin
  • 3,035
  • 1
  • 21
  • 30
  • True, but this was not requested in the question. – Jonas Berlin Jul 24 '19 at 08:35
  • 2
    Nevertheless, if the OP is going to deploy it, it will end up in a Jar. Any solution involving resources should not mix in files, as a project is almost never going to end up deployed as files. – RealSkeptic Jul 24 '19 at 08:38
  • True, added JAR solution as well. – Jonas Berlin Jul 24 '19 at 08:41
  • 2
    The URI detour would even work from a jar, when you create the filesystem first. But it must be emphasized that the `String(byte[])` constructor will use the system’s default encoding which surely isn’t the right choice for a resource embedded in a Java application. The application should be explicit regarding the resource’s encoding. – Holger Jul 24 '19 at 10:52
  • What about the encoding? – AlikElzin-kilaka Oct 29 '20 at 13:51
  • @AlikElzin-kilaka are you referring to the answer or to Holger's comment? – Jonas Berlin Oct 30 '20 at 07:20
2

You have to give the full path as URI like my below code snippet where the json file is in the same package.

try {
        String s = new String(Files.readAllBytes(Paths.get("D:/Test/NTech/src/com/ntech/CustomerOrganization.json")));
        System.out.println(s);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

For more information you can go through the public static Path get(URI uri) method documentation of the Paths class from: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html

0

Below code snippet should work for you.

Path path = Paths.get(CustomerControllerIT.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
String fileContent = new String(fileBytes);