You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
5.9 KiB
Java
196 lines
5.9 KiB
Java
package com.docus.server.util;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.io.*;
|
|
|
|
public class TableJsonRead {
|
|
|
|
/**
|
|
* 读取数据结构
|
|
* @param path 路径后缀
|
|
* @param fileName 文件名称
|
|
* @return
|
|
*/
|
|
public <T> T Read(String path,String fileName,Class<T> clazz){
|
|
String currentPath=CurrentPath();
|
|
path = currentPath+"\\"+path;
|
|
StringBuilder sb = new StringBuilder();
|
|
T dto =null;
|
|
File file = new File(path+"\\"+fileName);
|
|
try {
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
if (!file.exists()) {
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
} else {
|
|
BufferedReader bufferedReader = null;
|
|
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
String line;
|
|
while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
sb.append(line);
|
|
}
|
|
if (sb.length() > 0) {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
//dto = (T)JSONObject.parse(sb.toString());
|
|
dto= objectMapper.readValue(sb.toString(), clazz);
|
|
//dto = objectMapper.convertValue(o, new TypeReference<T>() { });
|
|
}
|
|
bufferedReader.close();
|
|
}
|
|
|
|
return dto;
|
|
}
|
|
catch (Exception ex){
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public <T> T Read(File file,Class<T> clazz) {
|
|
StringBuilder sb = new StringBuilder();
|
|
T dto =null;
|
|
try {
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
if (!file.exists()) {
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
} else {
|
|
BufferedReader bufferedReader = null;
|
|
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
String line;
|
|
while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
sb.append(line);
|
|
}
|
|
if (sb.length() > 0) {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
//dto = (T)JSONObject.parse(sb.toString());
|
|
dto= objectMapper.readValue(sb.toString(), clazz);
|
|
//dto = objectMapper.convertValue(o, new TypeReference<T>() { });
|
|
}
|
|
bufferedReader.close();
|
|
}
|
|
|
|
return dto;
|
|
}
|
|
catch (Exception ex){
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
/**
|
|
* 取得当前jar路径
|
|
* @return
|
|
*/
|
|
public static String CurrentPath(){
|
|
File dir = new File(".");
|
|
String currentpath ="";
|
|
try {
|
|
currentpath = dir.getCanonicalPath();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return currentpath;
|
|
}
|
|
|
|
/**
|
|
* 取得当前jar路径
|
|
* @return
|
|
*/
|
|
public static String currentPath(String dir){
|
|
String path = CurrentPath() + File.separator + dir;
|
|
File file = new File(path);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
return path;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 读取文件内容
|
|
* @param path
|
|
* @param fileName
|
|
* @return
|
|
*/
|
|
public String ReadContent(String path,String fileName){
|
|
String currentPath=CurrentPath();
|
|
path = currentPath+"\\"+path;
|
|
StringBuilder sb = new StringBuilder();
|
|
File file = new File(path+"\\"+fileName);
|
|
try {
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
if (!file.exists()) {
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
} else {
|
|
BufferedReader bufferedReader = null;
|
|
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
String line;
|
|
while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
sb.append(line);
|
|
}
|
|
|
|
bufferedReader.close();
|
|
}
|
|
|
|
return sb.toString();
|
|
}
|
|
catch (Exception ex){
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存json至文件
|
|
* @param path 路径后缀
|
|
* @param fileName 文件名称
|
|
* @param data json信息
|
|
* @return
|
|
*/
|
|
public void Save(String path,String fileName,String data){
|
|
String currentPath=CurrentPath();
|
|
path = currentPath+"\\"+path;
|
|
FileWriter fwriter = null;
|
|
try {
|
|
File file = new File(path);
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
fwriter = new FileWriter(path+"\\"+fileName);
|
|
fwriter.write(data);
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
} finally {
|
|
try {
|
|
fwriter.flush();
|
|
fwriter.close();
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|