Created
July 18, 2016 15:15
-
-
Save josinSbazin/33afbaff54f326a58944b36e3a38d535 to your computer and use it in GitHub Desktop.
level19.lesson10.home03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level19.lesson10.home03; | |
import java.io.*; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
/* Хуан Хуанович | |
В метод main первым параметром приходит имя файла. | |
В этом файле каждая строка имеет следующий вид: | |
имя день месяц год | |
где [имя] - может состоять из нескольких слов, разделенных пробелами, и имеет тип String | |
[день] - int, [месяц] - int, [год] - int | |
данные разделены пробелами | |
Заполнить список PEOPLE импользуя данные из файла | |
Закрыть потоки. Не использовать try-with-resources | |
Пример входного файла: | |
Иванов Иван Иванович 31 12 1987 | |
Вася 15 5 2013 | |
*/ | |
public class Solution { | |
public static final List<Person> PEOPLE = new ArrayList<Person>(); | |
public static void main(String[] args) throws IOException, ParseException { | |
if (args.length < 1) return; | |
File file = new File(args[0]); | |
if (!file.exists()) return; | |
SimpleDateFormat sdf = new SimpleDateFormat("d MM yyyy"); | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
// BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "CP1251")); | |
while (reader.ready()) | |
{ | |
String t = reader.readLine(); | |
if (!t.equals("")) | |
{ | |
String[] lines = t.split(" "); | |
int year = Integer.parseInt(lines[lines.length - 1]); | |
int month = Integer.parseInt(lines[lines.length - 2]); | |
int day = Integer.parseInt(lines[lines.length - 3]); | |
StringBuilder nameSB = new StringBuilder(lines[0]+" "); | |
if (lines.length > 4) for (int i = 1; i < lines.length - 3; i++) nameSB.append(lines[i]).append(" "); | |
String name = nameSB.toString().trim(); | |
String date = day + " " + month + " " + year; | |
PEOPLE.add(new Person(name, sdf.parse(date))); | |
} | |
} | |
reader.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment