Advertisement
Guest User

ReadFile

a guest
May 18th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. package productorder;
  2.  
  3. import java.io.IOException;
  4. import java.io.FileReader;
  5. import java.io.BufferedReader;
  6.  
  7. public class ReadFile {
  8.     private String path;
  9.     public ReadFile(String text_path)
  10.     {
  11.         path = text_path;
  12.     }
  13.    
  14.     public Products[] openFileProducts() throws IOException {
  15.         FileReader new_reader = new FileReader(path);
  16.         BufferedReader bfBufferedReader = new BufferedReader(new_reader);
  17.  
  18.         int size = countLines();
  19.         String aLine;
  20.         Products[] arrStrings = new Products[size];
  21.        
  22.         for (int i = 0; i < size; i++) {
  23.             String temp = bfBufferedReader.readLine();
  24.             String[] splitter = temp.split("\\s+");
  25.             arrStrings[i] = new Products(splitter[0], Double.parseDouble(splitter[1]));
  26.         }
  27.         bfBufferedReader.close();
  28.         return arrStrings;
  29.     }
  30.     public Orders[] OpenFileOrders() throws IOException {
  31.         FileReader new_reader = new FileReader(path);
  32.         BufferedReader bfBufferedReader = new BufferedReader(new_reader);
  33.  
  34.         int size = countLines();
  35.         String aLine;
  36.         Orders[] arrStrings = new Orders[size];
  37.        
  38.         for (int i = 0; i < size; i++) {
  39.             String temp = bfBufferedReader.readLine();
  40.             String[] splitter = temp.split("\\s+");
  41.             arrStrings[i] = new Orders(Double.parseDouble(splitter[0]), splitter[1]);
  42.         }
  43.         bfBufferedReader.close();
  44.         return arrStrings;
  45.     }
  46.    
  47.     int countLines() throws IOException{
  48.         FileReader new_read = new FileReader(path);
  49.         BufferedReader new_bf = new BufferedReader(new_read);
  50.        
  51.         String aNewLineString;
  52.         int counter = 0;
  53.         while ((aNewLineString = new_bf.readLine()) != null) {
  54.             counter++;
  55.         }
  56.         new_bf.close();
  57.         return counter;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement