java学习之文件的读取


public class Main1 {


/**
* @param args
*/
public static void main(String[] args) {
// 1,设置路径
String path = "E://0.txt";
// 2,获得File对象
File file = new File(path);
try {
// 3,获得FileInputStream
FileInputStream fileInputStream = new FileInputStream(file);
// 4,获得BufferInputSream对象
BufferedInputStream m_BufferedInputStream = new BufferedInputStream(
fileInputStream);
// 用于保存每次读取的信息
byte[] buffer = new byte[24];


// 6,每次读取的信息放到buffer,如果后面还有数据,read返回的值不等于-1,那么就继续读
while (m_BufferedInputStream.read(buffer) != -1) {

String infor = new String(buffer);

System.out.println(infor);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}


}

本文写了文件的读取,BufferedInputStream会预先读取一段原始数据进入缓冲区,第一次为8192字节,以后会成倍的增加,在读取文件的时候不需要关闭,但是在写文件的时候文件的写入和关闭必须对应,也就是说,如果是写是缓存,那么关闭也必须是缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值