Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.text.ParseException;
- import java.util.Scanner;
- import java.util.Vector;
- public class MyFileReader {
- private static Scanner sc;
- public static Vector<Person> readFromFile() throws ParseException, IOException {
- Vector<Person> data = new Vector<Person>();
- File file = new File("persons.txt");
- if (!file.exists()) {
- file.createNewFile();
- }
- file.canRead();
- try {
- sc = new Scanner(file);
- } catch (FileNotFoundException e) {
- System.out.println("Błąd otworzenia pliku.");
- return null;
- }
- while (sc.hasNext()) {
- String line = sc.nextLine();
- String[] words = line.split(" ");
- Person person = new Person();
- person.setFirstname(words[0]);
- person.setLastname(words[1]);
- person.setAge(words[2]);
- try {
- person.setDate(words[3]);
- } catch (ParseException e) {
- System.out.println("Błąd wczytania daty.");
- return null;
- }
- data.add(person);
- }
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement