package com.dsideal.base.Util; import com.alibaba.fastjson.JSONObject; import java.io.*; @SuppressWarnings("unchecked") public class FileUtil { /** * 功能:读取JSON文件,返回一个JSON对象 * 作者:黄海 * 时间:2018-12-12 * @param filePath * @return */ public static JSONObject readJsonFile(String filePath) { String content=txt2String(new File(filePath)); return JSONObject.parseObject(content); } /** * 功能:将文本文件的内容读到字符串里 * 作者:黄海 * 时间:2018-11-22 * * @param file * @return */ public static String txt2String(File file) { String encoding = "UTF-8"; Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { return new String(filecontent, encoding).replaceAll("\r", ""); } catch (UnsupportedEncodingException e) { System.err.println("The OS does not support " + encoding); e.printStackTrace(); return null; } } }