0

my problem continues from here utf-8 as output.Currently i am using:

PrintWriter bout = new java.io.PrintWriter(filename,"UTF-8");

and it still is not working .as @dacwe suggested,i used command line with

-Dfile.encoding=UTF-8

to execute my .jar file and it worked,where the desired utf-8 file output was created(no alphabets prob).So i am thinking this workaround like how eclipse.exe works with .ini file in eclipse directory.can i wrap my java prog into exe and have the program to include the .ini file?or can someone suggest any workable idea?my program read csv file (from opencsv) and output as a text in UTF-8.

Community
  • 1
  • 1
reukEN11
  • 121
  • 1
  • 2
  • 9
  • Setting encoding in PrintWriter is perfectly enough. Are you reading the file from java or using some text file editor? – bezmax Jul 19 '11 at 08:18
  • @Max in a class of my prog,this was declared : CSVReader reader1 = new CSVReader(new FileReader(inputFilePath),';').so file is read by java-by opencsv of course. – reukEN11 Jul 19 '11 at 08:26
  • @Max : yes.it is OpenCsv – reukEN11 Jul 19 '11 at 08:31

2 Answers2

1

Use a shell script or batch file to pass the JVM parameters.

Example shell script

#!/bin/sh
java -jar yourJar.jar -Dfile.encoding=UTF-8 "$@"

For windows batch files have a look at http://www.computerhope.com/batch.htm

bstick12
  • 1,699
  • 12
  • 18
1

From this question: Parse CSV file containing a Unicode character using OpenCSV

You should use your CSVReader like this:

CSVReader reader=new CSVReader(
    new InputStreamReader(new FileInputStream("d:\\a.csv"), "UTF-8"), 
    ',', '\'', 1);

And you do not need to set any environment properties like file.encoding or anything else.

Community
  • 1
  • 1
bezmax
  • 25,562
  • 10
  • 53
  • 84
  • i am confused here.as the documentation stated,there is no such constructor. – reukEN11 Jul 19 '11 at 08:40
  • The constructor is described here: http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVReader.html . It is just that instead of a `FileReader` (which does not support encoding) a `InputStreamReader` is created which does support encoding. InputStreamReader reads from any streams, so you need to give it a stream to read from (in constructor). In this case it uses `FileInputStream` which opens the file and makes it a stream. So the full flow becomes File -> FileInputStream -> InputStreamReader -> CSVReader – bezmax Jul 19 '11 at 08:51
  • thx for the solution and explanation.it solved my prob with the encoding. – reukEN11 Jul 19 '11 at 08:58
  • Using this method will hard code the encoding used by the application. It might be more desirable to pass the encoding as a parameter to the application. Using UTF-8 as the default if no encoding specified. – bstick12 Jul 19 '11 at 09:18
  • Maybe. Anyway I would rather recommend to pass it as some configuration file rather than a JVM option. – bezmax Jul 19 '11 at 10:11