When I am executing this program it is working absolutely fine
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "0X10001,0X10002,0X610001,0X610002";
String pattern = "0X(?=\\d{6})|(0)X(?=\\d{5})";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,
"$1");
}
m.appendTail(builder);
// Print the replaced matcher
System.out.println("After Replacement: "
+ builder.toString());
}
}
But when I am incorporating the same piece of code in the code below, it throws an error. (This code writes the rawData to CSV file, but before writing it is doing hexadecimal cleanup.)
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern r = Pattern.compile("0x(?=\\d{6})|(0)x(?=\\d{5})");
Matcher m = r.matcher(rawData);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,"$1");
}
m.appendTail(builder);
LOGGER.info("Cleaned hexadecimal digits are {0}",builder.toString());
try {
bw.write(builder.toString());
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
But without using string builder it works fine:
//code 3
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern myPattern = Pattern.compile("0x");
Matcher myMatcher = myPattern.matcher(rawData);
try {
bw.write(myMatcher.repalaceAll("0");
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
Can You please tell me why the second piece of code does not compile?
From the Javadoc for Matcher
, we can see that the appendReplacement
and appendTail
methods use only StringBuffer
:
public Matcher appendReplacement(StringBuffer sb, String replacement)
public StringBuffer appendTail(StringBuffer sb)
So, you need to use the StringBuffer
version if you want it to compile.