FileWriter 写入文件中文乱码

		 File file1 = new File("C:\\1.xml");
		 FileWriter fw = new FileWriter(file1);
		 fw.write("要输入的xml字符串");
		 fw.flush();
		 fw.close();

 

Java FileWriter 默认是用(ISO-8859-1 or US-ASCII)西方编码的,总之不是UTF-8的,而FileWriter类有getEncoding方法,却没有setEncoding的方法,如下的写法可以使正确

			File file1 = new File("C:\\1.xml");
			Writer writer = new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream(file1), "UTF-8"));
			writer.write("要输入的xml字符串");
			writer.flush();
			writer.close();


你可能感兴趣的:(java基础)