Created
January 19, 2020 17:06
-
-
Save codinko/6f7a113ad961b505e342f241144714e5 to your computer and use it in GitHub Desktop.
readFileFromClassPathAndConvert2String , Convert InputStream to String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinko.json.util; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
public class Util { | |
public static String readFileFromClassPathAndConvert2String(String filenameInCP) { | |
// InputStream java.lang.Class.getResourceAsStream(String name) | |
InputStream inputStream = Util.class.getResourceAsStream(filenameInCP); | |
String jsonString = convertInputStream2String(inputStream); | |
return jsonString; | |
} | |
public static String convertInputStream2String(InputStream inputStream) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
stringBuilder.append(line).append("\n"); | |
} | |
} catch (Exception e) { | |
// handle exception | |
} | |
return stringBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment