943,840 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1781
  • Java RSS
Aug 13th, 2008
0

Reading CSV files

Expand Post »
Quote ...
Hello everyone

I have the code that allows me to colect values acording if its Fail or Passed
Java Syntax (Toggle Plain Text)
  1. package spimonitoring;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.lang.reflect.Array;
  7.  
  8. public class InspectionResults44{
  9.  
  10. public static final byte HEIGHT_AVG_RESULT = 6,
  11. HEIGHT_RANGE_RESULT = 11,
  12. AREA_AVG_RESULT = 16,
  13. AREA_RANGE_RESULT = 22,
  14. VOLUME_AVG_RESULT = 29,
  15. VOLUME_RANGE_RESULT = 28,
  16. HAV_FAILED_FEATURE_RESULT = 35,
  17. REG_OFF_RESULT = 38,
  18. BRIDGE_LEN_RESULT = 41;
  19.  
  20. private String retrievedData[];
  21. private boolean failed[];
  22.  
  23.  
  24.  
  25. /**
  26. * Constructs this InspectionResult with the data stored in the args.
  27. * This class expects 44 values within the range of the args.
  28. */
  29. public InspectionResults44(String... args){
  30. retrievedData = args;
  31. boolean temp[] ={
  32. ((retrievedData[6].equalsIgnoreCase("F")) ? true: false),//7
  33. ((retrievedData[11].equalsIgnoreCase("F")) ? true: false),//12
  34. ((retrievedData[16].equalsIgnoreCase("F")) ? true: false),//15
  35. ((retrievedData[22].equalsIgnoreCase("F")) ? true: false),//20
  36. ((retrievedData[29].equalsIgnoreCase("F")) ? true: false),//23
  37. // ((retrievedData[28].equalsIgnoreCase("F")) ? true: false),//28
  38. // ((retrievedData[35].equalsIgnoreCase("F")) ? true: false),
  39. // ((retrievedData[38].equalsIgnoreCase("F")) ? true: false),
  40. // ((retrievedData[41].equalsIgnoreCase("F")) ? true: false)
  41. };
  42. failed = temp;
  43. }
  44. static class MyArrays{
  45. public static <T> T[] copyOfRange(T[] array, T[] emptyArray, int from, int size){
  46. ArrayList<T> temp = new ArrayList<T>(0);
  47. for(int i = from; i < size; i++){
  48. temp.add(array[i]);
  49. }
  50. return temp.toArray(emptyArray);
  51. }
  52. }
  53.  
  54. public static void main(String... args){
  55.  
  56. String line = null;
  57. int countline = 0;
  58. int startAtLineNo = 7;
  59.  
  60. FileReader fr = null;
  61. BufferedReader br = null;
  62. try{
  63. fr = new FileReader(new File("K:\\Spi5\\240-25-04-B-02-R1.F.T.#10.489a5f8e.spi.csv"));
  64. br = new BufferedReader(fr);
  65. }catch(Exception e){e.printStackTrace();}
  66.  
  67.  
  68. String dwArray[][] ={ {""}, {""}, {""} };
  69.  
  70. try {
  71. while ((line = br.readLine()) != null) {
  72. if (countline >= startAtLineNo) {
  73.  
  74. for(int i = 0; i < dwArray.length; i++){
  75. String temp[] = null;
  76.  
  77. try{ temp = br.readLine().split(",");
  78. }
  79. catch(Exception f){f.printStackTrace();
  80. System.exit(1);
  81. };
  82. String empty[] = {};
  83. temp = InspectionResults44.MyArrays.<String>copyOfRange(temp, empty, 1, temp.length);
  84.  
  85. dwArray[i] = temp;
  86. }
  87.  
  88.  
  89. InspectionResults44 ir[] =
  90. {
  91. new InspectionResults44(dwArray[0]),
  92. new InspectionResults44(dwArray[1]),
  93. new InspectionResults44(dwArray[2])
  94. };
  95.  
  96. System.out.println(ir[0]); // as an example
  97. spacer(3);
  98.  
  99. try{
  100. System.out.println(ir[0].hasFailed(InspectionResults32.HEIGHT_AVG_RESULT));
  101. System.out.println(ir[0].getAdjacentValue(InspectionResults32.HEIGHT_AVG_RESULT));
  102. System.out.println(ir[0].hasFailed(InspectionResults32.AREA_AVG_RESULT));
  103. System.out.println(ir[0].getAdjacentValue(InspectionResults32.AREA_AVG_RESULT));
  104. System.out.println(ir[0].hasFailed(InspectionResults32.VOLUME_AVG_RESULT));
  105. System.out.println(ir[0].getAdjacentValue(InspectionResults32.VOLUME_AVG_RESULT));
  106. System.out.println(ir[0].hasFailed(InspectionResults32.REG_OFF_RESULT));
  107. System.out.println(ir[0].getAdjacentValue(InspectionResults32.REG_OFF_RESULT));
  108. System.out.println(ir[0].hasFailed(InspectionResults32.BRIDGE_LEN_RESULT));
  109. System.out.println(ir[0].getAdjacentValue(InspectionResults32.BRIDGE_LEN_RESULT));
  110. }catch(Exception e){
  111. System.out.println(e);
  112. }
  113. }countline++;
  114. }
  115. } catch (IOException e) {
  116. // TODO Auto-generated catch block
  117. e.printStackTrace();
  118. }
  119. }
  120.  
  121. private static void spacer(int lines){
  122. for(int i = 0; i < lines; i++)
  123. System.out.println();
  124. }
  125.  
  126.  
  127.  
  128.  
  129. /**
  130. * Returns true if the given value has failed, returns false otherwise.
  131. * It's preferred to use the constants defined within this class to get the
  132. * desired information, and not regular ints.
  133. */
  134. public boolean hasFailed(byte result) throws Exception{
  135. switch(result){
  136. case HEIGHT_AVG_RESULT:
  137. return failed[0];
  138. case HEIGHT_RANGE_RESULT:
  139. return failed[1];
  140. case AREA_AVG_RESULT:
  141. return failed[2];
  142. case AREA_RANGE_RESULT:
  143. return failed[3];
  144. case VOLUME_AVG_RESULT:
  145. return failed[4];
  146. case VOLUME_RANGE_RESULT:
  147. return failed[5];
  148. case HAV_FAILED_FEATURE_RESULT:
  149. return failed[6];
  150. case REG_OFF_RESULT:
  151. return failed[7];
  152. case BRIDGE_LEN_RESULT:
  153. return failed[8];
  154. default :
  155. throw new Exception("Attempt to access invalid result type! Use the Result Constants to avoid this error!");
  156. }
  157. }
  158.  
  159. /**
  160. * Returns the value next to the specified result.
  161. */
  162. public String getAdjacentValue(byte result) throws Exception{
  163. if(result >= 0 && result < retrievedData.length - 1)
  164. return retrievedData[result + 1];
  165. else throw new Exception("Error! Attempt to access column with either no adjacent value or outside of data-range!");
  166. }
  167.  
  168. /**
  169. * Simply returns a String representing the data for each value in this class.
  170. */
  171. @Override
  172. public String toString(){
  173. String temp = "";
  174. for(String element : retrievedData){
  175. if(element.toString() != retrievedData[retrievedData.length - 1])
  176. temp += element + ", ";
  177. else temp += element;
  178. }
  179. return temp;
  180. }
  181.  
  182.  
  183. }
i'm trying to use this code in a recursive method in order to have the quantities of each faillure...

Java Syntax (Toggle Plain Text)
  1. /* .........................Recursive list............................... */
  2. public void listRecursively(File fdir, int depth) throws IOException {
  3.  
  4. /*Transform milliseconds time to gregorian time */
  5. long datefiles = fdir.lastModified();
  6. SimpleDateFormat Date = new SimpleDateFormat (" dd/MM/yyyy , HH:mm:ss aaa");
  7. Date nDate = new Date(datefiles);
  8.  
  9. String F = ",F,";
  10. String Height = null;
  11. String Area = null;
  12. String Volume = null;
  13. String RegOffset = null;
  14. String Bridging = null;
  15.  
  16. String line = null;
  17. int countline = 0;
  18. int startAtLineNo = 7;
  19.  
  20. int count = 0;
  21.  
  22. int countHeight = 0,
  23. countArea = 0,
  24. countVolume = 0,
  25. countRegOffset = 0,
  26. countBridging = 0;
  27.  
  28. /*Line counter*/
  29. try
  30. {
  31. RandomAccessFile File = new RandomAccessFile(fdir,"r");
  32. long lastline=File.length();
  33. File.close();
  34.  
  35. FileReader fileRead = new FileReader(fdir);
  36. BufferedReader bufferReader = new BufferedReader(fileRead);
  37. String dwArray[][] ={ {""}, {""}, {""} };
  38. Scanner scan = new Scanner(fdir);
  39.  
  40. while(scan.hasNextLine())
  41. {
  42. if(scan.nextLine().contains(F))
  43. count++;
  44. }
  45.  
  46. fileRead.close();
  47. bufferReader.close();//___________________________original
  48.  
  49. InspectionResults44 inspectionResults = new InspectionResults44(line.split(","));
  50.  
  51. while ((line = bufferReader.readLine()) != null) {
  52. if (countline >= startAtLineNo) {
  53. if (line.split(",").length == 32){
  54. HEIGHT_AVG_RESULT = 6;
  55. AREA_AVG_RESULT = 11;
  56. VOLUME_AVG_RESULT = 16;
  57. REG_OFF_RESULT = 22;
  58. BRIDGE_LEN_RESULT = 29;
  59. } else{//File with 44
  60. HEIGHT_AVG_RESULT = 7;
  61. HEIGHT_RANGE_RESULT = 12;
  62. AREA_AVG_RESULT = 15;
  63. AREA_RANGE_RESULT = 20;
  64. VOLUME_AVG_RESULT = 23;
  65. VOLUME_RANGE_RESULT = 28;
  66. HAV_FAILED_FEATURE_RESULT = 35;
  67. REG_OFF_RESULT = 38;
  68. BRIDGE_LEN_RESULT = 41;
  69. }
  70.  
  71. try {
  72. if(inspectionResults.hasFailed(InspectionResults44.HEIGHT_AVG_RESULT)){
  73. Height = inspectionResults.getAdjacentValue(InspectionResults44.HEIGHT_AVG_RESULT);
  74. countHeight++;
  75. }
  76. if(inspectionResults.hasFailed(InspectionResults44.AREA_AVG_RESULT)){
  77. Area = inspectionResults.getAdjacentValue(InspectionResults44.AREA_AVG_RESULT);
  78. countArea++;
  79. }
  80. if(inspectionResults.hasFailed(InspectionResults44.VOLUME_AVG_RESULT)){
  81. Area = inspectionResults.getAdjacentValue(InspectionResults44.VOLUME_AVG_RESULT);
  82. countVolume++;
  83. }
  84. if(inspectionResults.hasFailed(InspectionResults44.REG_OFF_RESULT)){
  85. Area = inspectionResults.getAdjacentValue(InspectionResults44.REG_OFF_RESULT);
  86. countRegOffset++;
  87. }
  88. if(inspectionResults.hasFailed(InspectionResults44.BRIDGE_LEN_RESULT)){
  89. Area = inspectionResults.getAdjacentValue(InspectionResults44.BRIDGE_LEN_RESULT);
  90. countBridging++;
  91. }}
  92. catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. fileRead.close();
  96. bufferReader.close();
  97. }}
  98.  
  99. // /*Start Line counter mode2*/
  100. // LineNumberReader lineRead = new LineNumberReader(fileRead);
  101. // lineRead.skip(lastline);
  102. // int countline = lineRead.getLineNumber()-6; //number of default lines = 6
  103. // fileRead.close();
  104. // lineRead.close();
  105. // /*End Line counter mode2*/
  106.  
  107. /* Output1 */
  108. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  109. System.out.println(INDENTS[depth] + x +", "+fdir.getName() +", "+ count +","+Date.format(nDate) + "," + countHeight + "," + countArea + "," + countVolume + "," + countRegOffset + "," + countBridging);
  110. /* Output2 */
  111. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  112. fw.write( INDENTS[depth] + x +", "+ fdir.getName() +", "+ count +","+ Date.format(nDate)+ System.getProperty("line.separator"));
  113. fw.flush();
  114. //if (fdir.getPath().endsWith(".csv") && (fdir.length()/512 )>= 1 && fdir.length()/512 <= 3)
  115. }
  116. catch(IOException e){
  117. }
  118. if (fdir.isDirectory() && !fdir.isHidden() && depth < MAX_DEPTH) {
  119. for (File f : fdir.listFiles()){ // Go over each file/subdirectory.
  120. listRecursively(f, depth+1);
  121. }}}

but nothing happens... can anyone help me?

thanks in advance!
Reputation Points: 7
Solved Threads: 0
Light Poster
jorgeflorencio is offline Offline
45 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Incompatible Types?
Next Thread in Java Forum Timeline: PLz Help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC