InputStreamReader是将字节流转换成字符流,是字节流通向字符流的桥梁,同时可以指定编码格式。
OutputStreamWriter是将字符流转换成字节流,是字符流通向字节流的桥梁,同时也可以指定编码格式。
但他们所指的编码仅仅只是对内容的编码,并不是代码所生成的文件的编码,如:
package IOflow_dome;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class propertiesDemo {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
Properties pw = new Properties();
pw.setProperty("a", "作者最帅");
pw.store(new OutputStreamWriter(new FileOutputStream("abc.properties"), "utf-8"), "jkelj");
}
}
生成的文件仍然是iso-8859-1的编码:
然后咱们再获取一下,内容:
package IOflow_dome;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class propertiesDemo {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
Properties pw = new Properties();
pw.load(new InputStreamReader(new FileInputStream("abc.properties")));
System.out.println(pw);
}
}
发现即便是不指定编码,显示的也是中文。
由此可得出内容还是utf-8的编码格式,因为我的java文件是utf-8的,所以桥梁转变的是内容的编码,并且不会随文件的编码格式所改变。