1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.StringTokenizer;
  11.  
  12. public class CsvResolve {
  13.  
  14. public List<String[]> readCsv(String csvfilePath,String encodingCharSet)
  15. {
  16. List<String[]> resultList=new ArrayList<String[]>();
  17. try {
  18. FileInputStream csvfileInputStream = new FileInputStream(csvfilePath);
  19. InputStreamReader csvfileInputStreamReader = new InputStreamReader(csvfileInputStream , encodingCharSet);
  20. BufferedReader csvfileBufferedReader = new BufferedReader(csvfileInputStreamReader);
  21. String line = csvfileBufferedReader.readLine();
  22. String[] firstLineElements=line.split("\t");
  23. //輸出第一行的內容
  24. for(int i=;i<firstLineElements.length;i++)
  25. {
  26. System.out.println(i+"\t"+firstLineElements[i]);
  27. }
  28. System.out.println();
  29.  
  30. while ((line = csvfileBufferedReader.readLine()) != null) {
  31. String[] otherLineElements=line.split("\t");
  32. //如果結尾處有一個或多個tab鍵,說明,最後缺失的有元素
  33. //先把字串陣列轉化為list,讓後新增元素,然後再把新增完元素的list
  34. //轉化為字串陣列
  35.  
  36. if(otherLineElements.length<firstLineElements.length)
  37. {
  38. String[] toaddOtherLineElements;
  39. List<String> tempList=new ArrayList<String>();
  40. int i=;
  41. for(;i<otherLineElements.length;i++)
  42. {
  43. if(otherLineElements[i].equals(""))
  44. {
  45. tempList.add(null);
  46. }else{
  47. tempList.add(otherLineElements[i]);
  48. }
  49. }
  50. for(;i<firstLineElements.length;i++)
  51. {
  52. tempList.add(null);
  53. }
  54. toaddOtherLineElements=tempList.toArray(new String[]);;
  55. resultList.add(toaddOtherLineElements);
  56. }
  57. //如果長度相同說明最後的元素存在
  58. else{
  59. String[] toaddOtherLineElements;
  60. List<String> tempList=new ArrayList<String>();
  61. int i=;
  62. for(;i<otherLineElements.length;i++)
  63. {
  64. if(otherLineElements[i].equals(""))
  65. {
  66. tempList.add(null);
  67. }else{
  68. tempList.add(otherLineElements[i]);
  69. }
  70. }
  71. toaddOtherLineElements=tempList.toArray(new String[]);;
  72. resultList.add(toaddOtherLineElements);
  73. }
  74. }
  75.  
  76. csvfileBufferedReader.close();
  77.  
  78. } catch (FileNotFoundException e) {
  79. // 捕獲File物件生成時的異常
  80. e.printStackTrace();
  81. } catch (IOException e) {
  82. // 捕獲BufferedReader物件關閉時的異常
  83. e.printStackTrace();
  84. }
  85. return resultList;
  86.  
  87. }
  88.  
  89. public static void main(String[] args) {
  90. CsvResolve csvResolve=new CsvResolve();
  91.  
  92. List<String[]> csvContent=csvResolve.readCsv("d:/csv/1.csv","utf-8");
  93. for(int i=;i<csvContent.size();i++)
  94. {
  95. String[] temp=csvContent.get(i);
  96. for(int j=;j<temp.length;j++)
  97. {
  98. System.out.println(j+"\t"+temp[j]);
  99. }
  100. System.out.println();
  101. }
  102.  
  103. }
  104.  
  105. }