You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
}
}
}