通常情况下我们考虑系统默认的编码格式(gbk),notepad上默认的是ansi编码格式。当一个txt文件的编码格式为utf-8时,直接用系统默认的编码格式肯定是会乱码的。这时候我们在输入输出的时候可以给定编码方式,使其不乱码或得到的目标文件为我们想要的编码格式。
1.选择FileInputStream和FileOutputStream输入输出
这种方法不能指定编码格式,当原文件为gbk时,生成的文件的编码是gbk。当原文件为utf-8时,生成的文件就为utf-8。不会产生乱码。
import java.io.*;
public class FileInputStream1{
public static void main(String[] args){
FileInputStream fis=null;
FileOutputStream fos=null;
try{
File fin=new File("file.txt");
File fout=new File("fileCopy.txt");
fis=new FileInputStream(fin);
fos=new FileOutputStream(fout);
byte[] b=new byte[(int)fin.length()];
fis.read(b);
fos.write(b);
//文件为gbk时生成的文件为gbk编码,为utf-8时生成的就为utf-8
}catch(IOException e){
System.out.println("IO异常!");
}finally{
try{
if(fis!=null){fis.close();};
if(fos!=null){fos.close();};
}catch(IOException e){
System.out.println("IO异常!");
}
}
}
}
2.选择FileInputStream和FileOutputStream,通过byte[]数组输入输出
import java.io.*;
public class FileInputStream2{
public static void main(String[] args){
long startTime = System.nanoTime(); //获取开始时间
FileInputStream fis=null;
FileOutputStream fos=null;
try{
File fin=new File("file.txt");
File fout=new File("fileCopy.txt");
fis=new FileInputStream(fin);
fos=new FileOutputStream(fout);
byte[] b=new byte[(int)fin.length()];
fis.read(b);//读取的位置一定要正确
String a=new String(b,"utf-8");//这一步至关重要,把txt文件的编码转为utf-8,再转为unicode
b=a.getBytes("gbk");//unicode转为gbk,即生成文件的编码格式,当文件中没有显示ansi(gbk),默认为ansi
//记得关闭之后再用notepad++打开就能看到编码发生了改变
fos.write(b);
}catch(IOException e){
System.out.println("IO异常!");
}finally{
try{
if(fis!=null){fis.close();};
if(fos!=null){fos.close();};
}catch(IOException e){
System.out.println("IO异常!");
}
}
}
}
3.FileReader和FileWriter,char[]数组
import java.io.*;
public class FileReader1{
public static void main(String[] args){
FileReader fr=null;
FileWriter fw=null;
File f=new File("file.txt");
try{
fr=new FileReader("file.txt");
fw=new FileWriter("fileCopy.txt");
char[] b=new char[(int)f.length()];
fr.read(b);
fw.write(b);
//文件为gbk时不会乱码,为utf-8时会乱码
}catch(Exception e){
e.printStackTrace();
} finally{
try{
//if(rf!=null){rf.close();}
if(fw!=null){fw.close();}
}catch(IOException e){
System.out.println("IO异常!");
}
}
}
}
4.InputStreamReader和OutputStreamWriter,char[]数组
import java.io.*;
public class InputStreamReader1{
public static void main(String[] args){
InputStreamReader rf =null;
OutputStreamWriter rw =null;
File f=new File("file.txt");
try{
//原文件是utf-8
//读取时格式要和源文件一致,否则乱码。写出去时可以指定编码格式,生成的文件就是什么样的编码方式了
rf=new InputStreamReader(new FileInputStream("file.txt"),"gbk");
rw =new OutputStreamWriter(new FileOutputStream("filecopy.txt"),"gbk");//文字有乱码
/*
rf=new InputStreamReader(new FileInputStream("file.txt"),"gbk");
rw =new OutputStreamWriter(new FileOutputStream("filecopy.txt"),"utf-8");//有文字乱码 */
/*
rf=new InputStreamReader(new FileInputStream("file.txt"),"utf-8");
rw =new OutputStreamWriter(new FileOutputStream("filecopy.txt"),"utf-8");//文字无乱码*/
/*
rf=new InputStreamReader(new FileInputStream("file.txt"),"utf-8");
rw =new OutputStreamWriter(new FileOutputStream("filecopy.txt"),"gbk");//文字无乱码 */
char[] b=new char[(int)f.length()];
int i=rf.read(b);
rw.write(b,0,i);
}catch(Exception e){
e.printStackTrace();
} finally{
try{
if(rf!=null){rf.close();}
if(rw!=null){rw.close();}
}catch(IOException e){
System.out.println("IO异常!");
}
}
}
}
5.InputStreamReader和FileWriter,char[]数组
import java.io.*;
public class InputStreamReader2{
public static void main(String[] args){
InputStreamReader rf =null;
FileWriter rw =null;
File f=new File("file.txt");
try{
//文件为utf-8编码格式,读取时是否是文件txt的格式,是无乱码,否有乱码
//rf=new InputStreamReader(new FileInputStream("file.txt"),"gbk");//文字有乱码
rf=new InputStreamReader(new FileInputStream("file.txt"),"utf-8");//文字无乱码,后面又一大串nul
rw =new FileWriter("filecopy.txt");
char[] b=new char[(int)f.length()];
int i=rf.read(b);
rw.write(b,0,i);
/*
//原解法文字无乱码,后面又一大串nul
rf=new InputStreamReader(new FileInputStream("file.txt"),"utf-8");//文字无乱码,后面又一大串nul
rw =new FileWriter("filecopy.txt");
char[] b=new char[(int)f.length()];
rf.read(b);
rw.write(b); */
}catch(Exception e){
e.printStackTrace();
} finally{
try{
if(rf!=null){rf.close();}
if(rw!=null){rw.close();}
}catch(IOException e){
System.out.println("IO异常!");
}
}
}
}