Matheus Bica Matheus Bica - 11 months ago 51
Java Question

Reading .CSV in Java containing Backslash

I'm having an issue when I read cells with '\' in it and then try to replace it to '\' because of java escape property.

For example: "Error: The provided P\jb credenti"

When I read this directly from a Cell using:

if (type == TStream.Spreadsheet) {
try {
FileInputStream stream = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(stream);
Sheet sheet = workbook.getSheet(0);

int rows = sheet.getRows();

int ignored = 0;
int parsed = 0;

for (int i = 1; i < rows; i++) {
try {
Cell content = sheet.getCell(0, i);

And after that put it in an object that I made to store its information:

ObjectThatIMade object_= new ObjectThatIMade();

object_.setContent(content.getContents().replace("\\", "\\\\"));

It doesn't change the '\' to '\\', I did the same with replaceAll it doesn't work either. It stores in the same way and when I try to send it by JSON to my webservice, it won't work because of the '\' in the string, it gives me error.

Is there any way to guarantee that '\' is replaced by '\\'?

Thanks a lot for your time!

Answer Source

Even when I tried to insert with JSON Object it gave me errors, maybe I'm doing something wrong, but what I did and it worked now is to use the .replace("\\","\\\\") before the assignment to the JSON Object.

And it worked, I'm working with multiple applications and this applications contain multiple parsing files, that generates many problems and add unnecessary complexity.

Thanks a lot for your help guys!