Search code examples
javaarrayscarriage-return

How to deal with carriage return that concatenate strings at the end and beginning of a line?


I have a text file with 2 lines of words

CCCCC,WIKY PODAR,130000,15
DDDDD,XXXXX555,130110,30

Program reads each line word by word spilt and store them into an array.

check myStringArray.length returns : 7

However I expect the output to be : 8

The issue is that two words at the end and the begining of the line are concatenated. How to seperate them and store into the array properly ?

 String fileName = "mac/text.txt";
    byte[] buffer = new byte[1000];
    FileInputStream inputStream = new FileInputStream(fileName);
     while (inputStream.read(buffer) != -1) {
        String testString2 = new String(buffer);
        String delim2 = ",";
        String[] token2 = testString2.split(delim2);
        String[] myStringArray = new String[token2.length];
        for (int i = 0; i < token2.length; i++) {
            myStringArray[i] = token2[i];
             token2[i]=token2[i].replaceAll("\\s+", ", ");
                            }
        System.out.println(myStringArray.length);

Solution

  • Why not load all the content in a String and replace in the String the line separator String by the "," character ? Then you can easily split the String with a single separator ",".

    You can try it :

    String content = new String(Files.readAllBytes(Paths.get("mac/text.txt")));
    content = content.replaceAll(System.lineSeparator(), ",");
    String[] token2 = content.split(",");
    

    Or if you want to avoid a call to replaceAll() and perform directly a split you can indicate in the regex the , character OR the line separator string :

    String content = new String(Files.readAllBytes(Paths.get("mac/text.txt")));
    String[] token2 = content.split(",|"+System.lineSeparator());