-1

When I create a csv file through java then for name "Men's Commemorative ® ELITE Bib Short", it is storing "Men's Commemorative ® ELITE Bib Short" in csv. So I have to remove "Â" from csv file through java file.

public boolean writeProductsToFile()
{
    final List<ProductModel> products = getListrakDao().getProducts();

    final String filePath = getFilePath() + getProductFileName();
    final File file = new File(filePath);
    FileWriter writer = null;
    writer = new FileWriter(file);
    for (final ProductModel productModel : products)
    {
            productData.append(StringEscapeUtils.unescapeHtml("\"" + productModel.getName() + "\""));
            productData.append(getFieldSeparator());
            writer.write(productData.toString());
    }
}

This is my code...where "baseProduct.getName()" is fetching name of product. In database product name is "Men's Commemorative ® ELITE Bib Short". But in csv it is getting written as "Men's Commemorative ® ELITE Bib Short". So how can I remove characters like "Â". So tha#t name in csv should be like exactly in database.

Bart
  • 9,925
  • 7
  • 47
  • 64
pnegi
  • 11
  • 2

1 Answers1

2

To a degree, this is a shot in the dark, but...

As a general practice, try to be explicit with the character sets you use.

Instead of

FileWriter writer = null;
writer = new FileWriter(file);

write

final Charset utf8 = java.nio.charset.StandardCharsets.UTF_8;
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), utf8);

(imports left out for brevity, StandardCharsets requires Java 7 or later)

This allows actively controlling the used charset when writing. If not set, the system uses the default charset, which may not be appropriate. If UTF-8 is not what you desire, try something else, like ISO_8859_1.

When you read your CSV file, make sure the reader/editor you use supports the used charset and uses it. Otherwise, you'll see strange characters, much like you did.

Hendrik
  • 5,085
  • 24
  • 56
  • When I am doing this. This is giving me error : The constructor FileOutputStream(File, Charset) is undefined. – pnegi Dec 16 '15 at 09:41
  • @pnegi Did my answer solve your problem? If so, please accept the answer. Thank you. – Hendrik Dec 17 '15 at 07:50