import java.io.FileInputStream;标签:编码,java,读写,指定,IOException,io,new,import,public From: https://blog.51cto.com/u_10028442/5824121
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();
}
}