简体   繁体   中英

Java CSV Reader - Replace Quotes

I am using CSVReader to read the csv file in Java. In my case, the csv file will have double quotes (") and single quotes ('). Something like this.

SL 12" WIR TREE ASST CD

The below code i am using to read the file.

CsvReader reader = null;
reader = readFile(fileName, delimiter, encoding);

while (reader.readRecord()) {
   // Code Part
}

Whenever it cross the reader.readrecord(), its throwing the exception as 'Maximum column length of 100,000 exceeded in column 0 in record 0. Set the SafetySwitch property to false if you're expecting column lengths greater than 100,000 characters to avoid this error.'

What i am trying to do and what i need is, Since i can't able to do any changes in the file, i am trying to replace the double quotes and single quotes to empty string in java. But it is throwing exception, what ever i mentioned above.

I don't know what CsvReader is (it is not part of standard JDK) but the problem seems to occur in readRecord() and thus way before you have the chance to replace any character . So, CsvReader is not usable here and you should use a less specialised reader such as java.io.BufferedReader , for example.

Given, the delimiter is not a quote or double quote (for obvious reasons) then this code snippet works:

File file = new File(fileName);
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, encoding));

try {
    String line = reader.readLine();
    while (line != null) {
        //replace qoutes
        line = line.replace("\"", "");
        line = line.replace("'", "");
        //split line according to given delimiter
        String[] items = line.split(delimiter);
        //handle items...
        line = reader.readLine();
    }
}
catch (IOException e) {
    //handle exception...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM