ΠΠΎΠΎΠ±ΡΠ΅ΠΌ Π½ΠΈ ΠΌΠ΅ΡΠΎΠ΄ save Π½ΠΈ Π½Π° ΠΌΠ΅ΡΠΎΠ΄ load Π²Π°Π»ΠΈΠ΄Π°ΡΠΎΡ ΠΏΡΠΈΠ½ΠΈΠΌΠ°ΡΡ Π½Π΅ Ρ
ΠΎΡΠ΅Ρ, Π½Π΅ ΡΠΎΠ²ΡΠ΅ΠΌ ΠΏΠΎΠ½ΠΈΠΌΠ°Ρ ΠΏΠΎΡΠ΅ΠΌΡ, Π½Π° ΠΏΡΠ°ΠΊΡΠΈΠΊΠ΅ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠ΅ ΡΠ΅ΡΡΡ ΠΊΠΎΠ΄ ΠΏΡΠΎΡ
ΠΎΠ΄ΠΈΡ.
ΠΡΠ»ΠΈ Π³ΠΎΡΠΏΠΎΠ΄Π° ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Ρ
ΠΎΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΊΠ»Π°ΡΡΠ° Properties ΡΠΎ ΡΠ°ΠΊ ΡΠΆ ΠΈ Π½Π°ΠΏΠΈΡΠΈΡΠ΅. ΠΠ»ΠΈ ΠΆΠ΅ Π΅ΡΡΡ ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ Π±Π°Π³ Π² ΠΌΠΎΠ΅ΠΌ ΡΠΏΠ°Π³Π΅ΡΡΠΈ?
package com.javarush.task.task20.task2003;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.*;
/*
ΠΠ½Π°ΠΊΠΎΠΌΡΡΠ²ΠΎ Ρ properties
*/
public class Solution {
public static Map<String, String> properties = new HashMap<>();
public void fillInPropertiesMap() {
try {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
FileInputStream fileInputStream = new FileInputStream(consoleReader.readLine());
// consoleReader.close();
load(fileInputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void save(OutputStream outputStream) throws Exception {
PrintStream writer = new PrintStream(outputStream);
for (Map.Entry<String, String> pair : properties.entrySet()){
writer.print(pair.getKey());
writer.print("=");
writer.print(pair.getValue());
writer.println();
}
writer.flush();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
Pattern equalSignPattern = Pattern.compile("[^\\\\]=");
Pattern colonPattern = Pattern.compile("[^\\\\]:");
Pattern backSlashPattern = Pattern.compile("\\\\$");
Pattern frontSpacePattern = Pattern.compile("^\\s+");
while (fileReader.ready()){
String readLine = fileReader.readLine();
Matcher equalSignMatcher = equalSignPattern.matcher(readLine);
Matcher colonMatcher = colonPattern.matcher(readLine);
Matcher backSlashMatcher = backSlashPattern.matcher(readLine.trim());
boolean equalSignFind = equalSignMatcher.find();
boolean colonFind = colonMatcher.find();
// System.out.println("Now read: " + readLine); //zakomenti menya
if (readLine.startsWith("#") || readLine.startsWith("!")){
// System.out.println("comment"); //Π·Π°ΠΊΠΎΠΌΠ΅Π½ΡΠΈ ΠΌΠ΅Π½Ρ
} else if (equalSignFind || colonFind){
int index;
if (equalSignFind && colonFind) {
int index1 = equalSignMatcher.start();
int index2 = colonMatcher.start();
index = index1 < index2 ? index1 : index2;
} else if (equalSignFind){
index = equalSignMatcher.start();
} else {
index = colonMatcher.start();
}
String key = readLine.substring(0, index+1);
String value = readLine.substring(index+2, readLine.length());
if (key.trim().endsWith("\\") && key.startsWith(" ")){
Matcher frontSpaces = frontSpacePattern.matcher(key);
frontSpaces.find();
key = frontSpaces.replaceAll("");
} else {
key = key.trim();
}
Matcher frontSpacesVal = frontSpacePattern.matcher(value);
if (frontSpacesVal.find()){
value = frontSpacesVal.replaceAll("");
}
// System.out.println("targetString");
if (backSlashMatcher.find()){
value = value.trim();
StringBuilder builder = new StringBuilder(value);
String nextString = "";
while (true) {
nextString = fileReader.readLine();
// System.out.println(nextString);
Matcher frontSpaces = frontSpacePattern.matcher(nextString);
frontSpaces.find();
nextString = frontSpaces.replaceAll("");
builder.append(nextString);
value = builder.toString();
if (!nextString.trim().endsWith("\\")){
break;
}
}
// System.out.println("backslashMatcher Find");
}
// System.out.println(key);
// System.out.println(value);
properties.put(key, value);
} else {
// System.out.println("not properties string");
}
}
}
public static void main(String[] args) throws Exception {
Solution s = new Solution();
s.fillInPropertiesMap();
// System.out.println(properties);
s.save(new FileOutputStream("C:\\test\\testprop.txt"));
s.fillInPropertiesMap();
System.out.println(properties);
}
}