public class TranStreamDemo2 {
/**
* @param args
* 将一串字符串按指定编码表输入到文本中
* @throws IOException
*/
public static void main(String[] args) throws IOException {
read_1();
read_2();
read_3();
write_3();
write_1();
write_2();
}
public static void read_3() throws IOException, IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("E:\\gbk_2.txt"),"UTF-8");
char[] ch=new char[1024];
int len=isr.read(ch);
System.out.println(new String(ch,0,len));
}
public static void read_2() throws IOException, IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("E:\\utf_3.txt"),"GBK");
char[] ch=new char[1024];
int len=isr.read(ch);
System.out.println(new String(ch,0,len));
}
public static void read_1() throws IOException {
FileReader fr=new FileReader("E:\\utf_3.txt");
char[] ch=new char[1024];
int len=fr.read(ch);
System.out.println(new String(ch,0,len));
}
public static void write_3() throws IOException, IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("E:\\utf_3.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
public static void write_2() throws FileNotFoundException, IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("E:\\gbk_2.txt"),"gbk");
osw.write("你好");
osw.close();
}
public static void write_1() throws IOException {
FileWriter fw=new FileWriter("E:\\gbk_1.txt");
fw.write("你好");
fw.close();
}
}
将一串字符串按指定编码表输入到文本中
Java文件读写与编码转换
最新推荐文章于 2025-08-15 14:43:33 发布
本文演示了如何使用Java进行文件的读取与写入操作,并展示了不同字符编码之间的转换过程。通过具体的代码示例介绍了FileReader、FileWriter、InputStreamReader和OutputStreamWriter等类的应用方法。
822

被折叠的 条评论
为什么被折叠?



