Writing special character to a text file

Hi,

I have a text will with encrypted data which show as special character in the file and plain text. If I read and write to another file I get only question marks for the special characters. But this only occurs when I run it from a jar file from the command prompt.

If I run the code from a IDE it works correctly. So I am really hosed on this one. I run the JAR file on Solaris with does not works. I also run the file in Eclipse IDE on Solaris and it works. This also happens if I try to append to the file. The data in the file looks like

w383 ~I~Y~D~A~U~G

w384 ~D| ~X~U~\~Y

cm89 ~Q~\~A

After I run from the jar file it looks like:

w383 ~I~Y~D~A~U~G

w384 ~D| ~X~U~\~Y

cm89 ~Q~\~A

w385 ?

Here is the code I uses.

String nameFile ="/usr/files/passfile";

String addTxt = userId +" " + pCode +"\n";

boolean status =false;

try{

BufferedWriter out =new BufferedWriter(new FileWriter(nameFile,

true));

out.write(addTxt);

out.close();

status =true;

logger.info("\n\nAdded user: " + userId +" to password file: "

+ nameFile +"\n\n");

}catch (IOException e){

// e.printStackTrace();

logger.info("\n\n"+e.getMessage());

}

Thanks

[1877 byte] By [thenew05a] at [2007-11-27 10:26:29]
# 1

> I have a text with encrypted data...

Then don't use Reader and Writer to work with that data. Use InputStream and OutputStream.

DrClapa at 2007-7-28 17:39:47 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks,

I tried the

BufferedWriter out = new BufferedWriter(

new OutputStreamWriter(

new FileOutputStream(nameFile,true)));

out.write(addTxt);

out.close();

And the same results. So should I try only using Stream classes to write to this file.

thenew05a at 2007-7-28 17:39:47 > top of Java-index,Java Essentials,Java Programming...
# 3

If whatever you are using to view the file is incapable of displaying the special char then all you will get is a ? or black square or whatever.

floundera at 2007-7-28 17:39:47 > top of Java-index,Java Essentials,Java Programming...
# 4

I found the issue!!!!

It was that the OS encoding is different from Eclipses encoding. To get that figure that out I ran this program:

public static void main(String[] args) {

System.out.println("letra: ");

System.out.println(System.getProperty("file.encoding"));

}

in eclipse it shows :

letra:

Cp1252

on Solaris OS it shows:

letra: ?

ISO646-US

so I added the encoding on the command line like:

java -Dfile.encoding=ISO-8859-1 -jar eco.jar

letra:

ISO-8859-1

AND I tried:

java -Dfile.encoding=Cp1252 -jar eco.jar

letra:

Cp1252

and it works now!

So by adding the

-Dfile.encoding=

to the command line I always get the encoding I want.

Thanks everyone

thenew05a at 2007-7-28 17:39:47 > top of Java-index,Java Essentials,Java Programming...