Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need to convert ANSI format .csv file to UTF-8 and little endian .csv file  RSS feed

 
Alok Gangwar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to java and in my project, I have a .csv file,which is in ANSI format.I have to convert this file to UTF-8 and little endian .csv file.

Help is appreciated.

Thanks,
Alok
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UTF-8 doesn't know about little endian. That's a property of UTF-16.

So suppose you want to convert to UTF-16 little endian. It doesn't matter the file is a CSV file. You can do this quite easily with just Readers and Writers.

Create an InputStreamReader wrapped around a FileInputStream, using StandardCharsets.US_ASCII (or simply "ASCII" if you don't use Java 7).
Create an OutputStreamWriter wrapped around a FileOutputStream, using StandardCharsets.UTF_16LE (or simply "UTF-16LE").

Now you can't read from a file and write to the same file at the same time. If you need to store the results in the same file you have two choices:
1) After creating the reader, read the contents into a StringBuilder. Write the contents of the StringBuilder to the writer when all is read.
2) Write to a different file at first. Then when done, delete the original file and rename the other file to the first one.
 
Alok Gangwar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please provide the code as well.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, we are NotACodeMill.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!