java file 字符串_Java读取一个文本文件拼接成一个字符串(readFileToString)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.Test;
public class Demo {// 使用示例
@Test
public void testName1() throws Exception {
String filePath = "D:\\測(cè)試數(shù)據(jù)\\測(cè)試數(shù)據(jù).json";
String jsonString = readFileToString(filePath);
System.out.println(jsonString);
System.out.println("done.....");
}
public static String readFileToString(String path) {
// 定義返回結(jié)果
String jsonString = "";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), "UTF-8"));// 讀取文件
String thisLine = null;
while ((thisLine = in.readLine()) != null) {
jsonString += thisLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException el) {
}
}
}
// 返回拼接好的JSON String
return jsonString;
}
}
總結(jié)
以上是生活随笔為你收集整理的java file 字符串_Java读取一个文本文件拼接成一个字符串(readFileToString)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: perl python ruby_per
- 下一篇: python3数据科学入门与实战_Pyt