[英]Replacing values from HashMap in a file with Java
i'm stuck on this part.我被困在这部分。 The aim is to take the values from an file.ini with this format
目的是从具有这种格式的 file.ini 中获取值
X = Y
X1 = Y1
X2 = Y2
take the Y values and replace them in a scxml file instead of the corresponding X keys, and save the new file.scxml取 Y 值并将它们替换为 scxml 文件而不是相应的 X 键,然后保存新的 file.scxml
As you can see from my pasted code, i use the HashMap to take the key and values printed correctly, that although it seems right the code to replace the values works only for the first entry of the HashMap.正如您从我粘贴的代码中看到的那样,我使用 HashMap 来获取正确打印的键和值,尽管看起来正确,但替换值的代码仅适用于 HashMap 的第一个条目。
The code is currently as follows:目前代码如下:
public String getPropValues() throws IOException {
try {
Properties prop = new Properties();
String pathconf = this.pathconf;
String pathxml = this.pathxml;
//Read file conf
File inputFile = new File(pathconf);
InputStream is = new FileInputStream(inputFile);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//load the buffered file
prop.load(br);
String name = prop.getProperty("name");
//Read xml file to get the format
FileReader reader = new FileReader(pathxml);
String newString;
StringBuffer str = new StringBuffer();
String lineSeparator = System.getProperty("line.separator");
BufferedReader rb = new BufferedReader(reader);
//read file.ini to HashMap
Map<String, String> mapFromFile = getHashMapFromFile();
//iterate over HashMap entries
for(Map.Entry<String, String> entry : mapFromFile.entrySet()){
System.out.println( entry.getKey() + " -> " + entry.getValue() );
//replace values
while ((newString = rb.readLine()) != null){
str.append(lineSeparator);
str.append(newString.replaceAll(entry.getKey(), entry.getValue()));
}
}
rb.close();
String pathwriter = pathxml + name + ".scxml";
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(pathwriter)));
bw.write(str.toString());
//flush the stream
bw.flush();
//close the stream
bw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return result;
}
so my .ini file is for example所以我的 .ini 文件是例如
Apple = red
Lemon = yellow
it print key and values correctly:它正确打印键和值:
Apple -> red
Lemon -> yellow
but replace in the file only Apple with red and not the others key但在文件中仅用红色替换Apple而不是其他键
The problem lays in your control flow order.问题在于您的控制流顺序。
By the time the first iteration in your for
loop, which corresponds to the first entry Apple -> red
, runs it would caused the BufferedReader rb
to reach the end of stream, hence doing nothing for subsequent iterations.当
for
循环中的第一次迭代(对应于第一个条目Apple -> red
)运行时,它会导致BufferedReader rb
到达流的末尾,因此对后续迭代不做任何事情。
You have then either to reinitialize the BufferedReader
for each iteration, or better, inverse the looping over your Map
entries to be within the BufferedReader
read loop:然后,您必须为每次迭代重新初始化
BufferedReader
,或者更好的是,将Map
条目上的循环反向到BufferedReader
读取循环中:
public String getPropValues() throws IOException {
try {
// ...
BufferedReader rb = new BufferedReader(reader);
//read file.ini to HashMap
Map<String, String> mapFromFile = getHashMapFromFile();
//replace values
while ((newString = rb.readLine()) != null) {
//iterate over HashMap entries
for (Map.Entry<String, String> entry : mapFromFile.entrySet()) {
str.append(lineSeparator);
str.append(newString.replaceAll(entry.getKey(), entry.getValue()));
}
}
rb.close();
// ...
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.