随手笔记,通用的读取配置文件的工具类
public class Content {
public static String readFile(String filePath, String charSet) throws Exception {
System.out.println("filePath:"+filePath+"charSet:"+charSet);
FileInputStream fileInputStream = new FileInputStream(filePath);
try {
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
fileChannel.read(byteBuffer);
byteBuffer.flip();
return new String(byteBuffer.array(), charSet);
} finally {
fileInputStream.close();
}
}
public static String getContent(String filePath) throws Exception {
String content = readFile(filePath, "UTF-8");
System.out.println("content:"+content);
return content.replaceAll("\\-{5}[\\w\\s]+\\-{5}[\\r\\n|\\n]", "");
}
}