准备上传oss做云备份

This commit is contained in:
yutou
2021-06-09 18:31:48 +08:00
parent e9ff6a0207
commit 44e0a866a3
11 changed files with 194 additions and 54 deletions

View File

@@ -3,62 +3,72 @@ package com.yutou.nas.utils;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
/**
* 配置和参数
*/
public class ConfigTools {
public static final String CONFIG="config.json";
public static final String DATA="data.json";
public static final String SQLITE="sqlite.json";
public static final String CONFIG = "config.json";
public static final String DATA = "data.json";
public static final String SQLITE = "sqlite.json";
static {
try {
File file=new File(CONFIG);
if(!file.exists()){
File file = new File(CONFIG);
if (!file.exists()) {
file.createNewFile();
}
file=new File(DATA);
if(!file.exists()){
file = new File(DATA);
if (!file.exists()) {
file.createNewFile();
}
file=null;
}catch (Exception e){
file = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object load(String type,String key){
File file=new File(type);
public static Object load(String type, String key) {
return load(type, key, Object.class, null);
}
public static <T> T load(String type, String key, Class<T> t) {
return load(type, key, t, null);
}
public static <T> T load(String type, String key, Class<T> t, T def) {
File file = new File(type);
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
String src=readFile(file);
if(src!=null){
String src = readFile(file);
if (src != null) {
try {
JSONObject json=JSONObject.parseObject(src);
if(json==null){
json=new JSONObject();
saveFile(file,json.toJSONString());
}
return json.getOrDefault(key, "");
}catch (Exception e){
return "";
JSONObject json = JSONObject.parseObject(src);
return json.getObject(key, t);
} catch (Exception e) {
}
}
return "";
return def;
}
public static boolean save(String type,String key,Object data){
File file=new File(type);
String src=readFile(file);
if(src==null){
src="{}";
public static boolean save(String type, String key, Object data) {
File file = new File(type);
String src = readFile(file);
if (src == null) {
src = "{}";
}
JSONObject json=JSONObject.parseObject(src);
json.put(key,data);
saveFile(file,json.toJSONString());
JSONObject json = JSONObject.parseObject(src);
json.put(key, data);
saveFile(file, json.toJSONString());
return false;
}
public static boolean saveFile(File file,String data){
public static boolean saveFile(File file, String data) {
try {
FileWriter writer=new FileWriter(file);
FileWriter writer = new FileWriter(file);
writer.write(data);
writer.flush();
writer.close();
@@ -68,12 +78,13 @@ public class ConfigTools {
return false;
}
}
public static String readFile(File file){
public static String readFile(File file) {
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
BufferedReader reader = new BufferedReader(new FileReader(file));
String tmp;
StringBuilder str= new StringBuilder();
while ((tmp=reader.readLine())!=null){
StringBuilder str = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
}
reader.close();