Kofipunches

UNISYSTEM PART2

Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package unisystem;
  2.  
  3. import java.io.FileInputStream;
  4.  
  5. import java.io.LineNumberInputStream;
  6.  
  7.  
  8. @SuppressWarnings("deprecation")
  9. public class DateiEinlesen {
  10.  
  11.     private int bufferSize = 15;
  12.     private String filePath = "/Users/K.T.F/eclipse-workspace/unisystem/src/unisystem/Ergebnis";
  13.     private byte buffer[] = new byte[bufferSize];
  14.     private FileInputStream fis;
  15.     private LineNumberInputStream lnis;
  16.    
  17.     // Constructor calls setInputStreams() to initialize input streams.
  18.     public DateiEinlesen()
  19.     {
  20.         setInputStreams();
  21.         readData();
  22.     }
  23.    
  24.     /**
  25.      * This method Initializes the input streams.
  26.      */
  27.     public void setInputStreams()
  28.     {
  29.         try {
  30.             fis = new FileInputStream(filePath);
  31.             lnis = new LineNumberInputStream(fis);
  32.         } catch(Exception e){
  33.             System.out.println("Error! Problem initializing input streams in method setInputStreams().");
  34.         }      
  35.     }
  36.    
  37.     /**
  38.      * This method calculates the number of lines in the file.
  39.      */
  40.     public int readNumberOfLines()
  41.     {
  42.         int lineNumber = 0;
  43.  
  44.         /**
  45.          * We'll iterate through each byte in the file and determine the current
  46.          * line number at every point and when a new line number is encountered we'll
  47.          * update lineNumber.
  48.          */
  49.         try {
  50.                 while(lnis.read()!= -1)
  51.                 {
  52.                     if(lnis.getLineNumber() != lineNumber)
  53.                     {
  54.                         lineNumber = lnis.getLineNumber();
  55.                     }
  56.                 }                
  57.         } catch (Exception e) {
  58.             System.out.println("Error! Can\'t read data from file in method readNumberOfLines().");
  59.         }
  60.        
  61.         return lineNumber;
  62.     }
  63.    
  64.     /**
  65.      * This method reads the next data stream from the file.
  66.      * Uses ';' as the delimiter.
  67.      */
  68.     public String readData()
  69.     {
  70.         int readByte;
  71.         String data;
  72.        
  73.         try {
  74.                 int iterator = 0;
  75.                
  76.                 while ((readByte = lnis.read()) != -1)
  77.                 {
  78.                     if(lnis.getLineNumber() == 0)
  79.                     {
  80.                         continue;
  81.                     }
  82.                     else if ((char)readByte != ';' && readByte != 10)
  83.                     {
  84.                         buffer[iterator] = (byte)readByte;                      
  85.                     } else {
  86.                         break;
  87.                     }
  88.                    
  89.                     iterator++;
  90.                 }            
  91.         } catch (Exception e) {
  92.             System.out.println("Error! Can\'t read data from file in method readData()."+e);
  93.         }
  94.        
  95.         data = new String(buffer);
  96.        
  97.         clearBuffer();
  98.        
  99.         return data;
  100.     }
  101.    
  102.     public double[] readgesamtPunkte()
  103.     {
  104.         double gesamtPunkte[] = new double[9];
  105.        
  106.         for (int i = 0; i < 9; i++)
  107.         {
  108.             gesamtPunkte[i] = Double.parseDouble(readData());
  109.         }
  110.        
  111.         return gesamtPunkte;
  112.     }
  113.    
  114.     public void clearBuffer()
  115.     {
  116.         buffer = new byte[bufferSize];
  117.     }
  118.    
  119.    
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment