1.目标:输出b.txt下的记事本字符
2.代码:
package IOChar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* 纯文本读取
* @author john
*
*/
public class Demo1 {
public static void main(String[] args){
//创建源
File src=new File("G:/iotest/IOChar/b.txt");
//选择流
Reader reader=null;
try {
reader=new FileReader(src);
//读取操作
char[] flush=new char[10];
int len=0;
try {
while(-1!=(len=reader.read(flush))){
//字符数组转字符串
String str=new String(flush,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件不存在");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.结果
4.问题出在哪里?
答案:两者没有相同的编码格式.
5.解决方案
两者改为相同的编码格式--->问题解决