Advertisement
Guest User

herethisthing

a guest
Aug 29th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package csvtestloader;
  2. //Bases off tutorial code by mkyong at http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10.  
  11. public class test1 {
  12. protected static ArrayList<String> fileThatIsRead;
  13. protected static ArrayList<String> data;
  14.  
  15. public static void main(String[] args) {
  16. String fileName ="GeoLiteCity-Blocks.csv";
  17. String csvFile = "/Users/Will/Downloads/GeoLiteCity-latest/GeoLiteCity_20140805/"+fileName;
  18. test1 obj = new test1();
  19. fileThatIsRead= new ArrayList<String>();
  20. obj.load(csvFile);
  21. obj.printRead(fileThatIsRead);
  22. System.out.println(" Done");
  23. }
  24.  
  25. public ArrayList<String> load(String csvFile) {
  26.  
  27.  
  28. BufferedReader br = null;
  29. String line = "";
  30. String csvSplitBy = ",";
  31. data = new ArrayList<String>();
  32. String[] tempArray=null;
  33. try
  34. {
  35. br = new BufferedReader(new FileReader(csvFile));
  36. while ((line = br.readLine()) != null)
  37. {
  38. tempArray=line.split(csvSplitBy);
  39. }
  40. for (int i=0; i<tempArray.length;++i){
  41. data.add(tempArray[i]);
  42. System.out.print(i);
  43. }
  44.  
  45. }
  46. catch (FileNotFoundException e)
  47. {
  48. e.printStackTrace();
  49. }
  50. catch (IOException e)
  51. {
  52. e.printStackTrace();
  53. }
  54. finally
  55. {
  56. if (br != null)
  57. {
  58. try
  59. {
  60. br.close();
  61. }
  62. catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. return data;
  68. }
  69. public void printRead(ArrayList<String> csvFile)
  70. {
  71. for (int i=0;i<csvFile.size();++i)
  72. {
  73. System.out.print(csvFile.get(i)+",");
  74. System.out.print(i);
  75. }
  76.  
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement