Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package app;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.text.ParseException;
  7. import java.util.Scanner;
  8. import java.util.Vector;
  9.  
  10. public class MyFileReader {
  11.     private static Scanner sc;
  12.  
  13.     public static Vector<Person> readFromFile() throws ParseException, IOException {
  14.         Vector<Person> data = new Vector<Person>();
  15.        
  16.         File file = new File("persons.txt");
  17.         if (!file.exists()) {
  18.             file.createNewFile();
  19.         }
  20.         file.canRead();
  21.  
  22.         try {
  23.             sc = new Scanner(file);
  24.         } catch (FileNotFoundException e) {
  25.             System.out.println("Błąd otworzenia pliku.");
  26.             return null;
  27.         }
  28.         while (sc.hasNext()) {
  29.             String line = sc.nextLine();
  30.             String[] words = line.split(" ");
  31.             Person person = new Person();
  32.             person.setFirstname(words[0]);
  33.             person.setLastname(words[1]);
  34.             person.setAge(words[2]);
  35.             try {
  36.                 person.setDate(words[3]);
  37.             } catch (ParseException e) {
  38.                 System.out.println("Błąd wczytania daty.");
  39.                 return null;
  40.             }
  41.             data.add(person);
  42.         }
  43.         return data;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement