Storing in String [duplicate]
Possible Duplicate:
What's the most elegant way to concatenate a list of values with delimiter in Java?
I have 3 different w开发者_Go百科ords listed in property file in 3 different lines. QWERT POLICE MATTER
I want to read that property file and store those 3 words in String, not in aarylist separated by whitespace. The output should look like this String str = { QWERT POLICE MATTER}
Now I used the follwoing code and getting the output without white space in between the words: String str = {QWERTPOLICEMATTER}. What should I do to the code to get the string result with words separated by whitespace.
FileInputStream in = new FileInputStream("abc.properties");
pro.load(in);
Enumeration em = pro.keys();
StringBuffer stringBuffer = new StringBuffer();
while (em.hasMoreElements()){
String str = (String)em.nextElement();
search = (stringBuffer.append(pro.get(str)).toString());
Try:
search = (stringBuffer.append(" ").append(pro.get(str)).toString());
Just append a blank space in your loop. See the extra append below:
while (em.hasMoreElements()){
String str = (String)em.nextElement();
search = (stringBuffer.append(pro.get(str)).toString());
if(em.hasNext()){
stringBuffer.append(" ")
}
}
精彩评论