10

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

6
  • You have to provide the full path, I think. Where (on your file system) is that file stored? Commented Jul 24, 2019 at 7: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. Commented Jul 24, 2019 at 7:53
  • you could also try giving reative path from root of project like src/.../controllers/CustomerOrganization.json Commented Jul 24, 2019 at 7:54
  • If you want to find out the path to a file relative to your class, you can use CustomerControllerIT.class.getResource("CustomerOrganization.json") Commented Jul 24, 2019 at 7:54
  • If you expect it in the same package, you should be using resources, not files. Commented Jul 24, 2019 at 7:55

3 Answers 3

15

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
Sign up to request clarification or add additional context in comments.

6 Comments

True, but this was not requested in the question.
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.
True, added JAR solution as well.
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.
What about the encoding?
|
4

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.