标签:idt case 条件 length operator enum min 文件格式 检查
微信号,bqt20094,这里是密码,邮箱#909120849@qq.com,QQ#909120849,
x
微信号,bqt20094,这里是密码,邮箱#909120849 .com,QQ#909120849,
//解析方式之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvUtils {
private static final String COMMA = ",";
private static final String SEPARATOR = "#";
private static final String LINE_SEPARATOR = File.separator;
private static final String ENCODING = "GBK";
public static void obj2CsvFils(List<CsvBean> dataList, File file) {
String content = obj2String(dataList);
writeFile(content, file);
}
public static List<CsvBean> csvFils2Obj(String filePath) {
String content = readFile(filePath);
return string2Obj(content);
}
private static String obj2String(List<CsvBean> dataList) {
StringBuilder sb = new StringBuilder();
for (CsvBean cvsBean : dataList) {
sb.append(cvsBean.name).append(COMMA).append(cvsBean.account).append(COMMA);
if (isNotEmpty(cvsBean.password)) sb.append(cvsBean.password);//密码有可能为空
sb.append(COMMA);
if (cvsBean.other != null && !cvsBean.other.keySet().isEmpty()) {
for (String key : cvsBean.other.keySet()) {
String value = cvsBean.other.get(key);
if (isNotEmpty(value)) sb.append(key).append(SEPARATOR).append(value).append(COMMA);
}
}
sb.append(LINE_SEPARATOR);
}
return sb.toString();
}
private static void writeFile(String content, File file) {
try {
FileOutputStream writer = new FileOutputStream(file);
writer.write(content.getBytes(ENCODING));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<CsvBean> string2Obj(String content) {
if (content == null) return null;
String[] array = content.split(LINE_SEPARATOR);
List<CsvBean> list = new ArrayList<CsvBean>();
for (String string : array) {
int index = string.indexOf(COMMA);
if (index >= 0) {
CsvBean bean = new CsvBean();
bean.name = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.account = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.password = string.substring(0, index);
string = string.substring(index + 1);
for (int i = 0; i <= string.length(); i++) {
if (string.endsWith(",")) string = string.substring(0, string.length() - 1);
else break;
}
if (string.length() > 0) {
String[] otherStrings = string.split(COMMA);
bean.other = new HashMap<>();
for (String other : otherStrings) {
String[] keyValue = other.split(SEPARATOR);
if (keyValue.length >= 2) {
bean.other.put(keyValue[0], keyValue[1]);
}
}
}
} else {
bean.password = string;
}
} else {
bean.account = string;
}
list.add(bean);
}
}
return list;
}
private static String readFile(String filePath) {
File file = new File(filePath);
byte[] temp = new byte[(int) file.length()];
try {
FileInputStream in = new FileInputStream(file);
in.read(temp);
in.close();
return new String(temp, ENCODING);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static boolean isNotEmpty(String string) {
return string != null && string.trim().length() > 0 && !string.equalsIgnoreCase("null");
}
}
//解析方式之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvUtils {
private static final String COMMA = ",";
private static final String SEPARATOR = "#";
private static final String LINE_SEPARATOR = File.separator;
private static final String ENCODING = "GBK";
public static void obj2CsvFils(List<CsvBean> dataList, File file) {
String content = obj2String(dataList);
writeFile(content, file);
}
public static List<CsvBean> csvFils2Obj(String filePath) {
String content = readFile(filePath);
return string2Obj(content);
}
private static String obj2String(List<CsvBean> dataList) {
StringBuilder sb = new StringBuilder();
for (CsvBean cvsBean : dataList) {
sb.append(cvsBean.name).append(COMMA).append(cvsBean.account).append(COMMA);
if (isNotEmpty(cvsBean.password)) sb.append(cvsBean.password);//密码有可能为空
sb.append(COMMA);
if (cvsBean.other != null && !cvsBean.other.keySet().isEmpty()) {
for (String key : cvsBean.other.keySet()) {
String value = cvsBean.other.get(key);
if (isNotEmpty(value)) sb.append(key).append(SEPARATOR).append(value).append(COMMA);
}
}
sb.append(LINE_SEPARATOR);
}
return sb.toString();
}
private static void writeFile(String content, File file) {
try {
FileOutputStream writer = new FileOutputStream(file);
writer.write(content.getBytes(ENCODING));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<CsvBean> string2Obj(String content) {
if (content == null) return null;
String[] array = content.split(LINE_SEPARATOR);
List<CsvBean> list = new ArrayList<CsvBean>();
for (String string : array) {
int index = string.indexOf(COMMA);
if (index >= 0) {
CsvBean bean = new CsvBean();
bean.name = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.account = string.substring(0, index);
string = string.substring(index + 1);
index = string.indexOf(COMMA);
if (index >= 0) {
bean.password = string.substring(0, index);
string = string.substring(index + 1);
for (int i = 0; i <= string.length(); i++) {
if (string.endsWith(",")) string = string.substring(0, string.length() - 1);
else break;
}
if (string.length() > 0) {
String[] otherStrings = string.split(COMMA);
bean.other = new HashMap<>();
for (String other : otherStrings) {
String[] keyValue = other.split(SEPARATOR);
if (keyValue.length >= 2) {
bean.other.put(keyValue[0], keyValue[1]);
}
}
}
} else {
bean.password = string;
}
} else {
bean.account = string;
}
list.add(bean);
}
}
return list;
}
private static String readFile(String filePath) {
File file = new File(filePath);
byte[] temp = new byte[(int) file.length()];
try {
FileInputStream in = new FileInputStream(file);
in.read(temp);
in.close();
return new String(temp, ENCODING);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static boolean isNotEmpty(String string) {
return string != null && string.trim().length() > 0 && !string.equalsIgnoreCase("null");
}
}
//数据结构之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvBean {
public String name;//必选项
public String account;//必选项
public String password;//可选项
public HashMap<String, String> other;//可选项
@Override
public String toString() {
return "CvsBean [name=" + name + ", account=" + account + ", password=" + password + ", other=" + other + "]";
}
}
//数据结构之所以定义成这样,是为了兼容我所使用的一款叫"密码本子"的APP
public class CsvBean {
public String name;//必选项
public String account;//必选项
public String password;//可选项
public HashMap<String, String> other;//可选项
public String toString() {
return "CvsBean [name=" + name + ", account=" + account + ", password=" + password + ", other=" + other + "]";
}
}
标签:idt case 条件 length operator enum min 文件格式 检查
原文地址:https://www.cnblogs.com/baiqiantao/p/9610165.html