1.文件(格式可以是UTF-8,GBK,GB2312或者其他)
2......----->读入到InputStream
3............----->把InputStream转好为String
4..................----->String写入文本文件(UTF-8编码)问题:
第一步读取文件的时候,没有提供编码类型(因为我根本不知道某一个文件是什么类型)
到第三步的时候就发现字符串中的汉字是乱码了
第四步输出的文件理所当然也就乱码了
如何解决这个问题啊。谢谢!
2......----->读入到InputStream
3............----->把InputStream转好为String
4..................----->String写入文本文件(UTF-8编码)问题:
第一步读取文件的时候,没有提供编码类型(因为我根本不知道某一个文件是什么类型)
到第三步的时候就发现字符串中的汉字是乱码了
第四步输出的文件理所当然也就乱码了
如何解决这个问题啊。谢谢!
public class FileTest
{
public static void main(String[] args) throws IOException
{
File sourceFile = new File("e:\\source.txt");
File targetFile = new File("d:\\" + sourceFile.getName());
FileWriter fw = new FileWriter(targetFile);
FileReader fr = new FileReader(sourceFile);
BufferedReader br = new BufferedReader(fr);
String temp = null;
while ((temp = br.readLine()) != null)
{
fw.write(temp);
}
fw.flush();
fw.close();
//测试方便资源待释放 ...可以try{}catch{}finally{在这里释放资源}
}
}
即:读取文件的时候没有指定编码类型,读取到InputStream内的字符串已经是乱码的二进制了。