代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
public class Test {
public static void main(String[] args) {
System.out.println(getStr1("C:\\test\\20220301\\hello.json"));
System.out.println("\n\n—————————————————————☆☆☆分界线☆☆☆—————————————————————————————\n\n");
System.out.println(getStr2("C:\\test\\20220301\\hello.json"));
}
public static String getStr1(String filePath) {
File file = new File(filePath);
Long length = file.length();
byte[] content = new byte[length.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(content);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return new String(content);
}
public static String getStr2(String filePath) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String s = null;
while ((s = reader.readLine()) != null) {
sb.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
结果
{
"code": 200,
"message": "成功",
"success": true
}
—————————————————————☆☆☆分界线☆☆☆—————————————————————————————
{ "code": 200, "message": "成功", "success": true}
原文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5824554b99873467958430f18dafa2a9.png)