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