- import java.io.*;
- public class FileTest {
- public static void main(String[] args) throws IOException {
- BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("e:\\1.txt"),"gbk"));
- BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e:\\2.txt"),"utf-8"));
- String line=null;
- while((line=br.readLine())!=null){
- bw.write(line);
- bw.newLine();
- }
- br.close();
- bw.close();
- }
- }
复制代码 今天在写博客的时候发现为什么我用的gbk和gb2312读取数据,用UTF-8写出数据的时候没出现乱码(按我想的应该会出现乱码啊)?如果反过来就出现乱码呢?即如下程序- import java.io.*;
- public class FileTest {
- public static void main(String[] args) throws IOException {
- BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("e:\\1.txt"),"utf-8"));
- BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e:\\2.txt"),"gbk"));
- String line=null;
- while((line=br.readLine())!=null){
- bw.write(line);
- bw.newLine();
- }
- br.close();
- bw.close();
- }
- }
复制代码 |