利用OutputStreamWriter用特定的编码写字符


class hello {

	public static void main(String[] args) throws IOException {
		BufferedReader isp  = new BufferedReader(new FileReader("xxx.txt"));
		//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("yyy.txt"), "utf-8");//利用UTF-8编码写字符

		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("yyy.txt"), "gbk");//利用GBK编码写字符
		int a;
		while((a = isp.read()) != -1) {
			osw.write(a);
		}
		isp.close();
		osw.close();
	}

先将yyy.txt的编码改成GBK

如果使用UTF-8编码写字符

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("yyy.txt"), "utf-8");

运行结果:

利用OutputStreamWriter用特定的编码写字符_第1张图片

改成GBK编码写字符:

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("yyy.txt"), "gbk");

利用OutputStreamWriter用特定的编码写字符_第2张图片

你可能感兴趣的:(利用OutputStreamWriter用特定的编码写字符)