如何在程序中正确设置JVM(1.5.x)所使用的默认字符编码?
我了解到在旧版本的JVM中,使用-Dfile.encoding=whatever
是一种可行的方式。但出于某些原因,我并没有这种奢侈。
我已经尝试过:
System.setProperty("file.encoding", "UTF-8");
属性被设置了,但似乎不会导致下面的最终getBytes
调用使用UTF8:
System.setProperty("file.encoding", "UTF-8");
byte inbytes[] = new byte[1024];
FileInputStream fis = new FileInputStream("response.txt");
fis.read(inbytes);
FileOutputStream fos = new FileOutputStream("response-2.txt");
String in = new String(inbytes, "UTF8");
fos.write(in.getBytes());
file.encoding
属性是不受支持的。 - McDowellclass Reader
和class Writer
的所有子类)时才相关吗?因为class FileInputStream
是基于字节的I/O流,所以为什么要关心字节流中的字符集呢? - overexchange