一種讀取類csv格式字串/列表字典巢狀字串的方式
阿新 • • 發佈:2018-12-13
原始碼:
import java.io.*; import java.util.*; //給定一個含有csv格式的字串,讀取其中的資訊 public class readcsv { //格式字串 private static char c0=','; //字典字串 private static String s0=":"; //呼叫函式,讀取檔案,並返回列表 public static ArrayList<Object> read(File file) { ArrayList<Object> list; String s=readfile(file); s=check(s); list=new ArrayList<Object>(Arrays.asList(split(s))); readcsv1(list); return list; } //呼叫函式,讀取字串,並返回列表 public static ArrayList<Object> read(String s){ ArrayList<Object> list; s=check(s); list=new ArrayList<Object>(Arrays.asList(split(s))); readcsv1(list); return list; } //讀取檔案,生成待處理字串 private static String readfile(File file) { String s=null; try{ FileInputStream fis=new FileInputStream(file); s=new String(fis.readAllBytes()); s=s.replace("\"", ""); fis.close(); }catch(Exception e) { e.printStackTrace(); } return s; } //檢查字串中滿足條件的子串並返回 private static String check(String s) { int count1=0,count2=0,count3=0,count4=0; int start=0,end=0; for(int i=0;i<s.length();i++) { char c=s.charAt(i); if(c=='[') { count1++; if(count1==1) start=i; } else if(c==']') count2++; else if(c=='{') count3++; else if(c=='}') count4++; if(count1<count2||count3<count4) { System.out.println("格式不正確"); break; } else if(count1==count2&&count1!=0) { end=i; break; } } s=s.substring(start+1,end); return s; } //如果字串某一位置是'['或者'{',計算與其對應的']'或者'}'的位置。 private static int count(String s,int k) { char c1,c2; int count1=0,count2=0; int mark=0; if(s.charAt(k)=='[') { c1='[';c2=']'; } else if(s.charAt(k)=='{') { c1='{';c2='}'; } else { c1=0;c2=0; System.out.println("charException"); } for(int i=k;i<s.length();i++) { char cc=s.charAt(i); if(cc==c1) count1++; else if(cc==c2) count2++; if(count1==count2) { mark=i;break; } } return mark; } //利用字元變數c0分割字串,並且跳過被"{}"或者"[]"包圍的字串。 private static String[] split(String s) { ArrayList<Integer> list=new ArrayList<>(); list.add(-1); for(int i=0;i<s.length();i++) { char c=s.charAt(i); if(c=='['||c=='{') i=count(s,i)-1; else if(c==c0) list.add(i); } list.add(s.length()); //System.out.println(list); String[] strs=new String[list.size()-1]; for(int i=0;i<strs.length;i++) strs[i]=s.substring(list.get(i)+1,list.get(i+1)); return strs; } //把被'[]'包圍的字串變成List private static ArrayList<Object> genelist(String s){ ArrayList<Object> list=null; if(s.charAt(0)=='['&&s.charAt(s.length()-1)==']') { String s1=s.substring(1,s.length()-1); list=new ArrayList<Object>(Arrays.asList(split(s1))); } else { System.out.println("格式不正確"); } return list; } //把被'{}'包圍的字串變成Map private static TreeMap<String,Object> genemap(String s){ TreeMap<String,Object> map=null; if(s.charAt(0)=='{'&&s.charAt(s.length()-1)=='}') { map=new TreeMap<String,Object>(); String s1=s.substring(1,s.length()-1); String[] ss=split(s1); for(String s2:ss) { String[] ss1=s2.split(s0,2); map.put(ss1[0], ss1[1]); } } else { System.out.println("格式不正確"); } return map; } //判斷一個字串是不是被'[]'或者'{}'包圍 private static int jud(String s) { int i=0; char c1=s.charAt(0); char c2=s.charAt(s.length()-1); if(c1=='['&&c2==']') i=1; else if(c1=='{'&&c2=='}') i=2; return i; } //讀取類csv檔案 private static void readcsv1(ArrayList<Object> list) { for(int i=0;i<list.size();i++) { String s=(String)list.get(i); if(s.length()>0) { int k=jud(s); if(k==1) { ArrayList<Object> list1=genelist(s); readcsv1(list1); list.set(i, list1); } else if(k==2) { TreeMap<String,Object> map1=genemap(s); readcsv2(map1); list.set(i, map1); } } } } //讀取類csv檔案 private static void readcsv2(TreeMap<String,Object> map) { Set<String> set=map.keySet(); for(String s1:set) { String s=(String)map.get(s1); if(s.length()>0) { int k=jud(s); if(k==1) { ArrayList<Object> list1=genelist(s); readcsv1(list1); map.put(s1, list1); } else if(k==2) { TreeMap<String,Object> map1=genemap(s); readcsv2(map1); map.put(s1, map1); } } } } }
兩個例子:
String s="[456,[wo],[ai],[ni],[[zhong],guo,[haha],gege],123]"; String s1="[4,5,6,[7,[{9:10,11:12,13:[14,15]},{16:{17:18}}],8],[],[],1,2,3]"; ArrayList<Object> list=readcsv.read(s); ArrayList<Object> list1=readcsv.read(s1); for(int i=0;i<list.size();i++) System.out.println(list.get(i)); System.out.println(); for(int i=0;i<list1.size();i++) System.out.println(list1.get(i));
執行結果:
456
[wo]
[ai]
[ni]
[[zhong], guo, [haha], gege]
123
4
5
6
[7, [{11=12, 13=[14, 15], 9=10}, {16={17=18}}], 8]
[]
[]
1
2
3