按指定编码读写文件

来源:互联网 发布:机械甩棍淘宝 编辑:程序博客网 时间:2024/06/05 15:10
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


public class EncodeTest {
public static void main(String[] args) throws IOException {
//writeText();
readText();
}


public static void readText() throws IOException{
InputStreamReader  isr=new InputStreamReader(new FileInputStream("G:\\gbk.txt"),"gbk");
char[] buf=new char[1024];
int len=0;
while((len=isr.read(buf))!=-1){
String str=new String(buf,0,len);
System.out.println(str);
}
isr.close();


}
public static void writeText() throws IOException{
OutputStreamWriter  osw=new OutputStreamWriter(new FileOutputStream("G:\\utf.txt"),"utf-8");//使用utf-8编码


                    osw.write("你好");
                    osw.close();


}
}
0 0
原创粉丝点击