字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD
| MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
字符串 CSV解析 表格 逗號分隔值 通訊錄 電話簿 MD
目錄
目錄CSV文件簡介
解析工具類
數據格式:
工具類
數據模型
CSV文件簡介
逗號分隔值:Comma-Separated Values,CSV,有時也稱為字符分隔值,因為分隔字符也可以不是逗號。
逗號分隔值文件以純文本形式存儲表格數據。純文本意味著該文件是一個字符序列,不含必須像二進制數字那樣被解讀的數據。
CSV文件由任意數目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號或制表符。
通常,所有記錄都有完全相同的字段序列。通常都是純文本文件。建議使用記事本來開啟,再則先另存新檔后用EXCEL開啟,也是方法之一。
CSV是一種通用的、相對簡單的文件格式,被用戶、商業和科學廣泛應用。最廣泛的應用是在程序之間轉移表格數據,而這些程序本身是在不兼容的格式上進行操作的(往往是私有的和/或無規范的格式)。因為大量程序都支持某種CSV變體,至少是作為一種可選擇的輸入/輸出格式。
CSV文件格式的通用標準并不存在,但是在RFC 4180中有基礎性的描述。使用的字符編碼同樣沒有被指定,但是7-bitASCII是最基本的通用編碼。
CSV并不是一種單一的、定義明確的格式,在實踐中,術語 CSV 泛指具有以下特征的任何文件:
- 純文本,使用某個字符集,比如ASCII、Unicode、EBCDIC或GB2312;
- 由記錄組成(典型的是每行一條記錄);
- 每條記錄被分隔符分隔為字段(典型分隔符有逗號、分號或制表符;有時分隔符可以包括可選的空格);
- 每條記錄都有同樣的字段序列。
在這些常規的約束條件下,存在著許多CSV變體,故CSV文件并不完全互通。然而,這些變異非常小,并且有許多應用程序允許用戶預覽文件(這是可行的,因為它是純文本),然后指定分隔符、轉義規則等。如果一個特定CSV文件的變異過大,超出了特定接收程序的支持范圍,那么可行的做法往往是人工檢查并編輯文件,或通過簡單的程序來修復問題。因此在實踐中,CSV文件還是非常方便的。
解析工具類
數據格式:
微信號,bqt20094,這里是密碼,郵箱#909120849@qq.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 CsvBean {public String name;//必選項public String account;//必選項public String password;//可選項public HashMap<String, String> other;//可選項@Overridepublic String toString() {return "CvsBean [name=" + name + ", account=" + account + ", password=" + password + ", other=" + other + "]";} }2018-9-8
轉載于:https://www.cnblogs.com/baiqiantao/p/9610165.html
總結
以上是生活随笔為你收集整理的字符串 CSV解析 表格 逗号分隔值 通讯录 电话簿 MD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql显示创建表的sql语句
- 下一篇: 分布式锁防止订单重复提交_防止表单重复提