replacing pipe delimiter with tab or comma in java
17,451
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("\\|", ",");
Author by
CodeMed
Updated on June 06, 2022Comments
-
CodeMed almost 2 years
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?