|
| 1 | + |
| 2 | +//And God shall wipe away all tears from their eyes; and there shall be no more death, |
| 3 | +//neither sorrow, nor crying, neither shall there be any more pain: for the former things are passed away. (Revelation 21:4) |
| 4 | + |
| 5 | +package com.javarush.task.task31.task3109; |
| 6 | + |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.util.Properties; |
| 10 | + |
| 11 | +/* |
| 12 | +Читаем конфиги |
| 13 | +*/ |
| 14 | +public class Solution { |
| 15 | + public static void main(String[] args) { |
| 16 | + Solution solution = new Solution(); |
| 17 | + Properties properties = solution.getProperties("4.JavaCollections/src/com/javarush/task/task31/task3109/properties.xml"); |
| 18 | + properties.list(System.out); |
| 19 | + |
| 20 | + properties = solution.getProperties("4.JavaCollections/src/com/javarush/task/task31/task3109/properties.txt"); |
| 21 | + properties.list(System.out); |
| 22 | + |
| 23 | + properties = solution.getProperties("4.JavaCollections/src/com/javarush/task/task31/task3109/notExists"); |
| 24 | + properties.list(System.out); |
| 25 | + } |
| 26 | + |
| 27 | + public Properties getProperties(String fileName) { |
| 28 | + Properties properties = new Properties(); |
| 29 | + int pos = fileName.lastIndexOf("."); |
| 30 | + String ext = pos >= 0 ? fileName.substring(pos) : ""; |
| 31 | + |
| 32 | + try { |
| 33 | + switch (ext) { |
| 34 | + case ".xml": { |
| 35 | + FileInputStream fileInputStream = new FileInputStream(fileName); |
| 36 | + properties.loadFromXML(fileInputStream); |
| 37 | + fileInputStream.close(); |
| 38 | + break; |
| 39 | + } |
| 40 | + case ".txt": { |
| 41 | + FileReader fileReader = new FileReader(fileName); |
| 42 | + properties.load(fileReader); |
| 43 | + fileReader.close(); |
| 44 | + break; |
| 45 | + } |
| 46 | + default: { |
| 47 | + FileInputStream fileInputStream = new FileInputStream(fileName); |
| 48 | + properties.load(fileInputStream); |
| 49 | + fileInputStream.close(); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } catch (Exception e) { |
| 54 | + System.out.println(e.getMessage()); |
| 55 | + return properties; |
| 56 | + } |
| 57 | + |
| 58 | + return properties; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/* |
| 63 | +Читаем конфиги |
| 64 | +
|
| 65 | +Реализовать метод getProperties, который должен считывать свойства из переданного файла fileName. |
| 66 | +
|
| 67 | +fileName может иметь любое расширение - как xml, так и любое другое, или вообще не иметь. |
| 68 | +
|
| 69 | +Нужно обеспечить корректное чтение свойств. |
| 70 | +
|
| 71 | +При возникновении ошибок должен возвращаться пустой объект. |
| 72 | +
|
| 73 | +Метод main не участвует в тестировании. |
| 74 | +
|
| 75 | +
|
| 76 | +
|
| 77 | +Подсказка: возможно тебе понадобится File.separator. |
| 78 | +
|
| 79 | +
|
| 80 | +
|
| 81 | +
|
| 82 | +
|
| 83 | +Требования: |
| 84 | +
|
| 85 | +1. Класс Solution должен содержать метод Properties getProperties(String fileName). |
| 86 | +
|
| 87 | +2. Метод getProperties должен корректно считывать свойства из xml-файла. |
| 88 | +
|
| 89 | +3. Метод getProperties должен корректно считывать свойства из любого другого файла с любым расширением. |
| 90 | +
|
| 91 | +4. Метод getProperties должен возвращать пустой объект, если во время чтения свойств возникла ошибка. |
| 92 | +*/ |
0 commit comments