I have to convert pipe delimited data into either tab delimited or comma delimited format. I wrote the following method in java:
public ArrayList<String> loadData(String path, String fileName){
File tempfile;
try {//Read File Line By Line
tempfile = new File(path+fileName);
FileInputStream fis = new FileInputStream(tempfile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int i = 0;
while ((strLine = br.readLine()) != null) {
strLine.replaceAll("\\|", ",");
cleanedData.add(strLine);
i++;
}
}
catch (IOException e) {
System.out.println("e for exception is:"+e);
e.printStackTrace();
}
return cleanedData;
}
The problem is that the resulting data is still pipe delimited. Can anyone show me how to fix the code above, so that it returns either tab delimited or comma delimited data?
Since Strings in Java are immutable. The replaceAll
method doesn't do in-place replacement. It returns a new string, which you have to re-assign back:
strLine = strLine.replaceAll("\\|", ",");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With