Javaの8:文字列にファイルを読み込みます

サンドロ・レイ:

私は、コントローラの同じパッケージ内のJSONファイルを持っている、と私は、ファイルを読み込み、文字列に変換してみてください

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

しかし、私はエラーを得ました:

java.nio.file.NoSuchFileException: CustomerOrganization.json

ここでは、画像の説明を入力します。

ジョナスベルリン:

あなたが使用され、ほとんど同じメソッドを使用します:

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

あなたがJAR内から仕事にそれを必要とする場合は、あなたの代わりにこれを実行する必要があります。

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=335071&siteId=1