BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
BufferedReader fr = new BufferedReader (new FileReader (br.readLine ()));
BufferedWriter fw = new BufferedWriter (new FileWriter ((br.readLine ())));
br.close ();
Pattern patten = Pattern.compile("^\\d+$");
String result = "";
while(fr.ready ()){
String str = fr.readLine ();
String[] pipei = str.split (" ");
for (String s : pipei){
Matcher matcher = patten.matcher (s);
if (matcher.find ( )) {
result.concat (matcher.group ( ) + " ") ;
}
}
}
fw.write (result.substring (0, result.length ()-1));
fr.close ();
fw.close ();
Is there any danger in concat? if i replace"concat" with "+=", it is ok, but if not, i will get some problems about closing BufferedReader and BufferedWriter. What's the reason?
已解决
评论 (2)
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
Guadalupe Gagnon
10 七月 2020, 13:56有用的
The concat() method returns a String that is the String that the method is called on plus the String that is passed in as the argument. It does not add the second String to the first. You would need to save the returned String to variable to be able to use is:
+1
Khalil Chen
11 七月 2020, 00:45
it works!thx a lot!
i viewed the original code, so i can use this method:
0