我是靠谱客的博主 紧张煎蛋,这篇文章主要介绍javaIO(七)可以转码的字符流,现在分享给大家,希望可以做个参考。

一、InputStreamReader可以以指定编码来读取文件内容,可以防止乱码。

例:现eclipse编码为GBK,以GBK编码来读取目标文件

 

package io.byteput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class InputStreamReaderDemo {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
File file=new File("F:"+File.separator+"student.txt");
/*try {
InputStream in=new FileInputStream(file);
byte[] b=new byte[in.available()];
in.read(b);
System.out.println(new String(b));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
InputStreamReader inReader=new InputStreamReader(new FileInputStream(file), "GBK");
int a=0;
try {
while((a=inReader.read())!=-1){
System.out.println((char)a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

 

 

 

 

最后

以上就是紧张煎蛋最近收集整理的关于javaIO(七)可以转码的字符流的全部内容,更多相关javaIO(七)可以转码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部