IO(五、FileReader第二种读数据)char[] buf=new char[1024];
2015-10-12 16:48
309 查看
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//通过字符数组读取
public class FileReader2 {
public static void main(String[] args) {
FileReader fr=null;
try {
fr = new FileReader("G:\\demo.txt");
//定义一个字符数组,用于存储读到字符
char[] buf=new char[1024];
/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数
int num=fr.read(buf);
System.out.println(num+" "+new String(buf));*/
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));//从0开始取,取num个
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.FileReader;
import java.io.IOException;
//通过字符数组读取
public class FileReader2 {
public static void main(String[] args) {
FileReader fr=null;
try {
fr = new FileReader("G:\\demo.txt");
//定义一个字符数组,用于存储读到字符
char[] buf=new char[1024];
/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数
int num=fr.read(buf);
System.out.println(num+" "+new String(buf));*/
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));//从0开始取,取num个
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章推荐
- RESTClient使用
- window.location.href 和 window.location.replace 的区别
- django通过middleware计算每个页面的详细执行时间
- IOS 添加刷新按钮,刷新当前页面(contentOffset)
- javascript知识整理
- struts2拦截器的三种配置方法
- TableView的Cell高度自适应,UILabel自动换行适应
- 第7周项目2——建立链队算法库
- python pip源配置
- 虚拟机pvm
- zoj3905
- 模拟退火算法(MATLAB实现)
- 实现android4.4新特性:沉浸式状态栏
- httpclient超时设置
- java 复制,删除文件
- poj1860 & poj2240(Bellman-Ford)
- 第6周、项目4—数制转换
- 重定向和转发
- 第七周项目2链队算法库
- superoj921 编译优化