package dwzx.com.get; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; //解析指定目錄下的所有csv文件,返回三維數組數據 public class GetCsvData { public static final int COLUMN_NUM = 9;//csv文件列數 //獲取指定csv文件數據,存入二維數組並返回 public static String[][] getCsvDataNew(String filePath) throws IOException{ BufferedReader br = new BufferedReader(new FileReader(new File(filePath))); String line = ""; ArrayList<String[]> lineList = new ArrayList<String[]>(); // Read a single line from the file until there are no more lines to read while((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); // 以逗號作為分隔符 String[] currCol = new String[COLUMN_NUM]; // Each currCol has 9 fields, so we need room for the 8 tokens. for(int i = 0; i < COLUMN_NUM; i++) { // For each token in the line that we've read: //先判斷是否還有待讀取數據,防止溢出 if(st.hasMoreTokens()){ currCol[i] = st.nextToken(); } } lineList.add(currCol); } String[][] str = new String[lineList.size()][9]; for(int i = 0; i < lineList.size(); i++) { for(int j = 0; j < 9; j++) { str[i][j] = lineList.get(i)[j]; //System.out.println(str[i][x]); } } br.close(); return str; } //獲取目錄當前路徑下所有csv文件的絕對路徑 public static ArrayList<String> getFileList(String dirPath){ File dir = new File(dirPath); File[] fileList = dir.listFiles(); ArrayList<String> strList = new ArrayList<String>(); for(File f:fileList){ if((f.isFile()) && (".csv".equals( f.getName(). substring( f.getName().lastIndexOf("."), f.getName().length())))){ strList.add(f.getAbsolutePath()); } } return strList; } //獲取指定目錄下所有csv文件數據,存入三維數組並返回 public static String[][][] getAllCsvData(String dir) throws IOException{ //ArrayList<String> list = new ArrayList<String>(); ArrayList<String> csvList = getFileList(dir); String[][][] s3 = new String[csvList.size()][][]; for(int i = 0; i < csvList.size(); i ++){ s3[i] = (getCsvDataNew(csvList.get(i))); } return s3; } public static void main(String[] args) throws IOException { String[][][] s = getAllCsvData("testdata\\"); for(int i = 0; i < s.length; i ++) for(int j = 0; j < s[i].length; j ++) for(int k = 0; k < s[i][j].length; k ++){ System.out.println("s["+i+"]"+"["+j+"]"+"["+k+"]: "+s[i][j][k]); } } }