DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • JQueue: A Library to Implement the Outbox Pattern
  • How to Convert CSV to XML in Java

Trending

  • The Role of Functional Programming in Modern Software Development
  • How Clojure Shapes Teams and Products
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  1. DZone
  2. Coding
  3. Languages
  4. OpenCSV: Properly Handling Backslashes

OpenCSV: Properly Handling Backslashes

OpenCSV is a popular library for handing CSV data in Java projects, but there's a slight problem when dealing with backslashes. Let's see what it is and how to fix it.

By 
Vatsal Mevada user avatar
Vatsal Mevada
·
Nov. 16, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
24.4K Views

Join the DZone community and get the full member experience.

Join For Free

OpenCSV is one of the popular Java libraries out there used for handling CSV data. In this post, I will discuss one specific issue that I recently faced with this library.

The Problem

Here is a minimal code snippet for writing and reading CSV data using OpenCSV:

String dataValue = "test";

//writing  
StringWriter writer = new StringWriter();

try (CSVWriter csvwriter = new CSVWriter(writer)) {
    String[] originalData = new String[2];
    originalData[0] = dataValue;
    originalData[1] = dataValue;
    System.out.println("Original data: " + originalData[0] + "," + originalData[1]);
    csvwriter.writeNext(originalData);
} catch (IOException e) {
    throw new RuntimeException(e);
}
System.out.println("Written data: " + writer.toString());

//reading
try (CSVReader csvReader = new CSVReader(new StringReader(writer.toString()))) {
    String[] readData = csvReader.readNext();
    System.out.println("Read data: " + readData[0] + "," + readData[1]);
} catch (IOException e) {
    throw new RuntimeException(e);
}


The output of the above snippet gives us:

Original data: test,test
Written data: "test","test"

Read data: test,test


Which is as expected. Well, life is good with OpenCSV until you encounter a backslash character ('\\') in your CSV data.

So let's try running the same snippet with dataValue having a backslash character:

String dataValue = "t\\est";


Output:

Original data: t\est,t\est
Written data: "t\est","t\est"

Read data: test,test


Note that the backslash character is gone in the read CSV data.

The Root Cause

By default, CSVReader uses the backslash ('\\') as its escape character. Meanwhile,
CSVWriter uses a double quote('"') as the escape character.

Because of this, at the time of writing, the backslash characters lead to improper escaping. At the time of reading, a single backslash character will be ignored by the CSVParser, as it is the escape character.

The Solution

By default, CSVReader uses CSVParser for parsing CSV data. OpenCSV provides another parser (RFC4180Parser) that strictly follows RFC4180 standards.

Using RFC4180Parser, the CSVReader will use a double quote ('"') as the escape character, making it consistent with CSVWriter.

We need to replace the reading part of the above-mentioned snippet with the following code:

RFC4180Parser rfc4180Parser = new RFC4180ParserBuilder().build();
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(new StringReader(writer.toString()))
                .withCSVParser(rfc4180Parser);
try (CSVReader csvReader = csvReaderBuilder.build()) {
    String[] readData = csvReader.readNext();
    System.out.println("Read data: " + readData[0] + "," + readData[1]);
} catch (IOException e) {
    throw new RuntimeException(e);
}


Output:

Original data: t\est,t\est
Written data: "t\est","t\est"

Read data: t\est,t\est


P.S.: Apache Commons CSV is a good alternative to OpenCSV.

The library version used for the code snippets was OpenCSV 4.0.

CSV Snippet (programming) Data (computing) Library Java (programming language) Parser (programming language) POST (HTTP)

Published at DZone with permission of Vatsal Mevada. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • JQueue: A Library to Implement the Outbox Pattern
  • How to Convert CSV to XML in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!