public void load(InputStream inputStream) throws Exception { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); ArrayList<String> us = new ArrayList<>(); while (bufferedReader.ready()) { us.add(bufferedReader.readLine()); } for (int i = 0; i < us.size(); i += 5) { User user = new User(); user.setFirstName(us.get(i)); user.setLastName(us.get(i + 1)); user.setBirthDate(new Date(Long.parseLong(us.get(i + 2)))); user.setMale(Boolean.valueOf(us.get(i + 3))); String country = us.get(i + 4); User.Country country1 = null; if (country.equals("UKRAINE")) { country1 = User.Country.UKRAINE; } else if (country.equals("RUSSIA")) { country1 = User.Country.RUSSIA; } else if (country.equals("OTHER")) { country1 = User.Country.OTHER; } user.setCountry(country1); users.add(user); } bufferedReader.close(); }
public void load(InputStream inputStream) throws Exception { BufferedReader bur = new BufferedReader(new InputStreamReader(inputStream)); while (bur.ready()) { User user = new User(); user.setFirstName(bur.readLine()); user.setLastName(bur.readLine()); user.setBirthDate(new Date(Long.parseLong(bur.readLine()))); user.setMale(Boolean.parseBoolean(bur.readLine())); user.setCountry(User.Country.valueOf(bur.readLine())); users.add(user); } bur.close(); }
public static void main(String[] args) { // you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream // according to your real file location // вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в // соответствии с путем к вашему реальному файлу try { File your_file_name = new File("/home/alexey/pro/out"); OutputStream outputStream = new FileOutputStream(your_file_name); InputStream inputStream = new FileInputStream(your_file_name); User u1 = new User(); User u2 = new User(); u1.setCountry(User.Country.UKRAINE); u2.setCountry(User.Country.OTHER); u1.setBirthDate(new GregorianCalendar(1991, 2, 21).getTime()); u2.setBirthDate(new GregorianCalendar(1990, 11, 7).getTime()); u1.setFirstName("Antonio"); u2.setFirstName("Joni"); u1.setLastName("Kokovetzh"); u2.setLastName("Pakolonetzh"); u1.setMale(true); u2.setMale(true); JavaRush javaRush = new JavaRush(); javaRush.users.add(u1); javaRush.users.add(u2); javaRush.save(outputStream); outputStream.flush(); JavaRush loadedObject = new JavaRush(); loadedObject.load(inputStream); Comparator<List<User>> c = new Comparator<List<User>>() { @Override public int compare(List<User> u1, List<User> u2) { int c = 0; if (u1.size() == u2.size()) for (int i = 0; i < u1.size(); i++) { if (u1.get(i).getBirthDate().equals(u2.get(i).getBirthDate())) c++; if (u1.get(i).getCountry().equals(u2.get(i).getCountry())) c++; if (u1.get(i).getFirstName().equals(u2.get(i).getFirstName())) c++; if (u1.get(i).getLastName().equals(u2.get(i).getLastName())) c++; if (u1.get(i).isMale() == u2.get(i).isMale()) c++; } return c; } }; if (c.compare(javaRush.users, loadedObject.users) == javaRush.users.size() * 5) System.out.println("OK"); else System.out.println("NOT"); outputStream.close(); inputStream.close(); } catch (IOException e) { // e.printStackTrace(); System.out.println("Oops, something wrong with my file"); } catch (Exception e) { // e.printStackTrace(); System.out.println("Oops, something wrong with save/load method"); } }