Java读取一个文本文件拼接成一个字符串(readFileToString)
2018-02-13 15:15
471 查看
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:\\测试数据\\测试数据.json"; String jsonString = readFileToString(filePath); System.out.println(jsonString); System.out.println("done....."); } public static String readFileToString(String path) { // 定义返回结果 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; } }
相关文章推荐
- C#控制台基础 file.readalllines一行一行的读取一个txt文本,把txt中的内容放到一个string数组里
- fileread函数:IDL中将文本文件读入一个字符串数组中,每一行作为一个元素
- FileStorage Read String Start With Number Need Quotation Mark 读取数字开头的字符串需要加引号
- Java从入门到精通 - JavaString 字符串拼接,截取,查找及日期转换
- Java String To Date 字符串转时间
- 用fopen函数创建或打开一个文本文件,并写入字符串“this is a test file.
- 解决linux下java读取串口之权限问题 No permission to create lock file.
- 一个异常:Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.rssReader.st
- java 利用POI读取execel文件时出现 Unable to read entire header; 0 bytes read; expected 512 bytes 的解决方法
- java控制台程序如何判断String字符串中只输入了一个回车
- Java解析DBF文件,错误Failed to parse Number: For input string: "-.---" 或读取不出数据或乱码问题都在这里解决
- JAVA读取中文文件,判断string中含有中文字符,判断某一个字符是否为中文字符
- 对java中的String采用+=连接字符串需要注意的一个问题
- 按字符串读取文件内容NSString stringWithContentsOfFile: (NSString类方法)
- In Java, how do I read/convert an InputStream to a String? - Stack Overflow
- java给一个String 字符串反转
- How to read file in Java – BufferedInputStream Example
- java读取Excel 错误: jxl.read.biff.BiffException: Unable to recognize OLE stream
- java读取Excel 错误: jxl.read.biff.BiffException: Unable to recognize OLE stream
- 用JAVA编写的读取一个文本文件的一行之后读取另一个文本文件的另一行,逐行交叉读取并生成新的文本文件