-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTest.java
More file actions
executable file
·66 lines (65 loc) · 2.58 KB
/
FileTest.java
File metadata and controls
executable file
·66 lines (65 loc) · 2.58 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
/*
* Read and process .conf
* Usage in main
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
public class FileTest {
public static String cfgRead(String cfgFileName) throws IOException {
// String cfgFileName = "./httpd.conf";
File cfgFileInfo = new File(cfgFileName);
int numOfBytes = (int) cfgFileInfo.length();
System.out.println("Cfg-Length: " + numOfBytes + "\r\n");
FileInputStream fileStream = new FileInputStream (cfgFileName);
byte[] fileInBytes = new byte[numOfBytes];
fileStream.read(fileInBytes);
// System.out.println(new String(fileInBytes));
return new String(fileInBytes);
}
public static HashMap<String, String> generateCfgMap(String cfgFileContent) {
HashMap<String, String> res = new HashMap<String, String>();
String[] temp0 = cfgFileContent.split("\\s");
String[] temp1 = new String[10];
for(int i = 0; i < temp0.length; i++) {
// System.out.println(i + " " + temp0[i]);
if(temp0[i].equals("<VirtualHost")) {
i++;i++;
for(int j = 0;; i++) {
if(temp0[i].equals("<VirtualHost>")) {
break;
}
if(!temp0[i].equals("")) {
temp1[j] = temp0[i];
// System.out.println(temp1[j]);
j++;
}
}
res.put("vb_" + temp1[3], temp1[1]);
// System.out.println("vb_" + temp1[3] + ": " + res.get("vb_" + temp1[3]));
}
else if (!temp0[i].equals("")) {
for(int j = 0;j < 2; i++) {
if(!temp0[i].equals("")) {
temp1[j] = temp0[i];
// System.out.println(temp1[j]);
j++;
}
}
i--;
res.put(temp1[0], temp1[1]);
// System.out.println("!!newCfg!!" + temp1[0] + ": " + res.get(temp1[0]));
}
}
return res;
}
public static void main(String[] args) throws IOException {
String cfgFileName = "./httpd.conf";
String cfgFileContent = cfgRead(cfgFileName);
HashMap<String, String> cfgMap = new HashMap<String, String>();
cfgMap = generateCfgMap(cfgFileContent);
System.out.println(cfgMap.toString());
}
}