Advertisement
ostyleo

ParseFile

Mar 27th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.Objects;
  6. import java.util.Scanner;
  7.  
  8. public class FileManager {
  9.     private String fileName;
  10.  
  11.     public FileManager(String fileName) {
  12.         this.fileName = fileName;
  13.     }
  14.  
  15.     public ArrayList<Double> getDataSetOneDimension() {
  16.         ArrayList<Double> out = new ArrayList<>();
  17.         for (File file : Objects.requireNonNull(new File("C:\\Users\\Marian\\IdeaProjects\\AILab2\\src\\datas").listFiles())) {
  18.             if (file.getName().equals(fileName)) {
  19.                 try {
  20.                     FileInputStream fis = new FileInputStream(file);
  21.                     Scanner scan = new Scanner(fis);
  22.                     int length = scan.nextInt();
  23.                     for (int i = 0; i < length; i++) {
  24.                         out.add(scan.nextDouble());
  25.                     }
  26.                 } catch (FileNotFoundException e) {
  27.                     System.out.println("File" + file + "not found!");
  28.                 }
  29.             }
  30.         }
  31.         return out;
  32.     }
  33.  
  34.     public ArrayList<Double> getDataSetTwoDimensions() {
  35.         ArrayList<Double> out = new ArrayList<>();
  36.         for (File file : Objects.requireNonNull(new File("C:\\Users\\Marian\\IdeaProjects\\AILab2\\src\\datas").listFiles())) {
  37.             if (file.getName().equals(fileName)) {
  38.                 try {
  39.                     FileInputStream fis = new FileInputStream(file);
  40.                     Scanner scan = new Scanner(fis);
  41.                     int length1 = scan.nextInt();
  42.                     int length2 = scan.nextInt();
  43.                     for (int i = 0; i < length1; i++){
  44.                         for (int j = 0; j < length2; j++){
  45.                             out.add(scan.nextDouble());
  46.                         }
  47.                     }
  48.                 } catch (FileNotFoundException e) {
  49.                     System.out.println("File" + file + "not found!");
  50.                 }
  51.             }
  52.         }
  53.         return out;
  54.     }
  55.  
  56.     public ArrayList<Double> getDataSetThreeDimensions() {
  57.         ArrayList<Double> out = new ArrayList<>();
  58.         for (File file : Objects.requireNonNull(new File("C:\\Users\\Marian\\IdeaProjects\\AILab2\\src\\datas").listFiles())) {
  59.             if (file.getName().equals(fileName)) {
  60.                 try {
  61.                     FileInputStream fis = new FileInputStream(file);
  62.                     Scanner scan = new Scanner(fis);
  63.                     int length1 = scan.nextInt();
  64.                     int length2 = scan.nextInt();
  65.                     int length3 = scan.nextInt();
  66.                     for (int i = 0; i < length1; i++){
  67.                         for (int j = 0; j < length2; j++){
  68.                             for (int x = 0; x < length3; x++){
  69.                                 out.add(scan.nextDouble());
  70.                             }
  71.                         }
  72.                     }
  73.                 } catch (FileNotFoundException e) {
  74.                     System.out.println("File" + file + "not found!");
  75.                 }
  76.             }
  77.         }
  78.         return out;
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement