字节流与字符流
2015-09-20 21:52
288 查看
获取某个文件下的文字:
File file = new File("C:/a.txt"); //获取文件的地址
InputStream in = null;
BufferedReader br = null;
InputStreamReader is = null;
try {
in = new FileInputStream(fs);
is = new InputStreamReader(in);
br = new BufferedReader(is);
String len = null;
while((len=br.readLine())!=null){
System.out.println(len);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
br.close();
is.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
使用字节流,比如读取写入图片:
File sf = new File("C:/a.jpg"); //源文件,必须存在,路径可选
//目的文件,因为要向其中写入,指定文件可以不存在,由程序生成
File df = new File("C:/b.jpg");
FileInputStream in = null;
FileOutputStream out = null;
try{
in = new FileInputStream(sf);
out = new FileOutputStream(df,true);
byte[] b = new byte[1024];
int len = 0;
while( (len = in.read(b)) != -1 ){
out.write(b,0,len);
//后台输出
System.out.write(b, 0, len);
}
}catch(Exception e){
}finally{
out.close();
in.close();
}
File file = new File("C:/a.txt"); //获取文件的地址
InputStream in = null;
BufferedReader br = null;
InputStreamReader is = null;
try {
in = new FileInputStream(fs);
is = new InputStreamReader(in);
br = new BufferedReader(is);
String len = null;
while((len=br.readLine())!=null){
System.out.println(len);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
br.close();
is.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
使用字节流,比如读取写入图片:
File sf = new File("C:/a.jpg"); //源文件,必须存在,路径可选
//目的文件,因为要向其中写入,指定文件可以不存在,由程序生成
File df = new File("C:/b.jpg");
FileInputStream in = null;
FileOutputStream out = null;
try{
in = new FileInputStream(sf);
out = new FileOutputStream(df,true);
byte[] b = new byte[1024];
int len = 0;
while( (len = in.read(b)) != -1 ){
out.write(b,0,len);
//后台输出
System.out.write(b, 0, len);
}
}catch(Exception e){
}finally{
out.close();
in.close();
}
相关文章推荐
- Android开发碰到的要点
- 笔记20150920
- ios-新浪微博开发-23-加载微博数据
- Spring Boot 分布式Session状态保存Redis
- 第2章 Java内存区域与内存溢出异常
- oracle 创建表
- PE文件格式解析 LUA版本,兼容32/64位
- 对象与方法
- Spring Boot 分布式Session状态保存Redis
- Spring Boot 分布式Session状态保存Redis
- Spring Boot 分布式Session状态保存Redis
- display & visibility区别
- 软件开发模型
- ios-新浪微博开发-22-封装标题按钮
- 指针数组(主要用于二维的数组)
- SQL函数整理
- 软工实践练习一——个人
- 小数点四舍五入
- 简易版网页计算器css页面
- 322 Sort【归并排序】