Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing pipe delimiter with tab or comma in java

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?

like image 451
CodeMed Avatar asked Sep 17 '13 17:09

CodeMed


1 Answers

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("\\|", ",");   
like image 86
Rohit Jain Avatar answered Sep 23 '22 10:09

Rohit Jain