is it possible to get file encoding?

I am working withwindows netsh util

/**filename = path to file*/

privatestatic BufferedReader readFileWithBufferedReader(String filename){

FileInputStream fis =null;

InputStreamReader sr=null;

BufferedReader r=null;

try{

fis =new FileInputStream(filename);

}catch (FileNotFoundException ex){

ex.printStackTrace();

}//fis catch

try{

/*creating InputStreamReader with encoding param*/

sr =new InputStreamReader(fis,"Cp866"/*"Cp1251"*/);

System.out.println("encoding ::: " +sr.getEncoding());

}catch (UnsupportedEncodingException ex){

ex.printStackTrace();

}//sr catch

/*wrapping to BufferedReader*/

br =new BufferedReader( sr);

System.out.println("YES!!!");

return br;

}

I getInputStream from console. I set encoding as "cp866" (it's O.K.)

Then, I save stream to file.

Then user can open this file and get config from file.

The problem is in open file procedure.

I do not know file encoding: it can be "cp1251" (windows cyrillic) or it can be "cp866" (cmd output).

So if user tries to open "cp1251" file with "cp866" encoding, he gets unreadable characters.

Is it possible to get file encoding and then re-create InputStreamReader with proper encoding?

[2313 byte] By [Holoda] at [2007-11-27 7:30:20]
# 1

Well, you can't get the file encoding. It's not saved with the file. Unless it's your file format, in which case you can save whatever you want.

There are apps that can guess a file's encoding.

Of course, if you have control over the files entirely, it shouldn't be a problem, and generally it'd make life a lot easier to just use UTF-8.

> So if user tries to open "cp1251" file with "cp866" encoding,

> he gets unreadable characters.

what if you read a "cp866" with "cp1251" encoding?

bsampieria at 2007-7-12 19:10:32 > top of Java-index,Desktop,Core GUI APIs...
# 2
If I try to read cp866 with cp1251 - I get unreadable symbolsIf I try to read cp1251 with cp866 - again, I get unreadable symbols.
Holoda at 2007-7-12 19:10:32 > top of Java-index,Desktop,Core GUI APIs...
# 3
do you not have complete control of the file creation?I'd search for some code that can detect the encoding... however they are not 100% accurate for various reasons.
bsampieria at 2007-7-12 19:10:32 > top of Java-index,Desktop,Core GUI APIs...