-1

I am trying to use '\034' field separator character as a delimiter in a string. The issue is when I hardcode "\034"+opField and write it to a file it works, but if the "\034" character is read from a file, it writes the output as string "col1\034col2'. I tried using StringBuilder but it escapes the \034 to "\\034". I am using the following code to read the character from the file:

try (BufferedReader br = new BufferedReader(new FileReader(fConfig))){
                int lc = 1;
                for(String line;(line = br.readLine())!=null;){
                    String[] rowList = line.split(delim); 
                    int row_len = rowList.length;
                    if (row_len<2){
                        System.out.println("Incorrect dictionary file row:"+fConfig.getAbsolutePath()+"\nNot enough values found at row:"+line);
                    }else{
                        String key = rowList[0];
                        String value = rowList[1];
                        dictKV.put(key, value);
                    }
                    lc++;
                }
            }catch(Exception e){
                throw e;
            }

Any help is welcome...

[update]: The same thing is happening with '\t' character, if harcoded fine, but if read from a file its getting appended as characters. "col0\tcol1"

if(colAl.toLowerCase().contains(" as ")){
                    String temp = colAl.replaceAll("[ ]+as[ ]+"," | ");
                    ArrayList<String> tempA = this.brittle_delim(temp,'|');
                    colAl = tempA.get(tempA.size()-1);
                    colAl = colAl.trim();
                }else {
                    ArrayList<String> tempA = this.brittle_delim(colAl,' ');
                    colAl = tempA.get(tempA.size()-1);
                    colAl = colAl.trim();
                }
                if(i==0){
                    sb.append(colAl);
                    headerCols+=colAl.trim();
                }else{
                    headerCols+= this.output_field_delim + colAl;
                    sb.append(this.output_field_delim);
                    sb.append(colAl);
                }

            }
        }

        System.out.println("SB Header Cols:"+sb.toString());
        System.out.println("Header Cols:"+headerCols);

Output:

SB Header Cols: SPRN_CO_ID\034FISC_YR_MTH_DSPLY_CD\034CST_OBJ_CD\034PRFT_CTR_CD\034LEGL_CO_CD\034HEAD_CT_TYPE_ID\034FIN_OWN_CD\034FUNC_AREA_CD\034HEAD_CT_NR

Header Cols: SPRN_CO_ID\034FISC_YR_MTH_DSPLY_CD\034CST_OBJ_CD\034PRFT_CTR_CD\034LEGL_CO_CD\034HEAD_CT_TYPE_ID\034FIN_OWN_CD\034FUNC_AREA_CD\034HEAD_CT_NR

In the above code if I do the following I am getting correct results:

headerCols+= "\034"+ colAl;

output: SPRN_CO_IDFISC_YR_MTH_DSPLY_CDCST_OBJ_CDPRFT_CTR_CDLEGL_CO_CDHEAD_CT_TYPE_IDFIN_OWN_CDFUNC_AREA_CDHEAD_CT_NR The FS characters are there even if they are geting removed here

vhora
  • 320
  • 2
  • 16
  • 1
    You haven't posted the relevant code. You say "it writes the output as string "col1\034col2'." but you haven't included the code *that writes the output*! That code is most likely were the problem is (but it's probably not a problem, it's just a result of the way in which you write the output to the screen) – Erwin Bolwidt Feb 24 '17 at 15:07
  • Can you give an example? I'm quite sure where that `col0\tcol1` would happen and what `col0` and `col1` are meant to be. – Thomas Feb 24 '17 at 15:07
  • Updated the output code – vhora Feb 24 '17 at 15:13
  • Where you define `output_field_delim`? – SubOptimal Feb 24 '17 at 15:16
  • output_field_delim is coming from the above function and is being set in the constriuctor of this object. Its just a string assignment. – vhora Feb 24 '17 at 15:17
  • You should replace `line.split(delim)` with `line.split(Pattern.quote(delim))` to avoid any surprises from split actually taking a regex. – David Ehrmann Feb 24 '17 at 15:20
  • That doesnt seem to be the issue here as the delim will always be ";", but noted. Also the output_field_delim is correctly read as \034 which is what is stored in the file. – vhora Feb 24 '17 at 15:23

2 Answers2

0

You should provide an example demonstrating your problem. Not just incomplete code snippets.

Following runable snippet does what you explained.

// create a file one line
byte[] bytes = "foo bar".getBytes(StandardCharsets.ISO_8859_1);
String fileName = "/tmp/foobar";
Files.write(Paths.get(fileName), bytes);

String headerCols = "";
String outputFieldDelim = "\034";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
    // read the line from the file and split by blank character
    String[] cols = br.readLine().split(" ");

    // contcatenate the values with "\034"
    // but ... for your code ...
    // don't concatenate String objects in a loop like below
    // use a StringBuilder or StringJoiner instead
    headerCols += outputFieldDelim + cols[0];
    headerCols += outputFieldDelim + cols[1];
}
// output with the "\034" character
System.out.println(headerCols);
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Sorry if I wasn't clear. In my scenario I am reading the \034 from the file too, and that is where i am encountering the above problem. – vhora Feb 24 '17 at 15:34
  • @vhora Then provide a small example which demonstrates the problem. Otherwise we can only guess what you actually do and what is in the file you read. – SubOptimal Feb 24 '17 at 15:54
  • Thanks @SubOptimal , but the question was asked in a haphazard disarray at the end of a very long day. Thanks for your help anyways.. – vhora Feb 24 '17 at 15:56
0

I guess this is where I found my solution and the actual words for my Question.

How to unescape string literals in java

Community
  • 1
  • 1
vhora
  • 320
  • 2
  • 16