码迷,mamicode.com
首页 > 其他好文 > 详细

从磁盘读取一个文件到内存中,再打印到控制台

时间:2016-12-05 22:14:48      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:out   bsp   txt   main   偏移量   建立   ring   class   目标   

public class work3 {

public static void main(String[] args) throws IOException {

getFile();

}


//通过建立缓冲区和循环的方式来读取
public static void getFile() throws IOException{


//1.找目标文件
File file = new File("C:\\abcd\\12.5.txt");


//2.建立通道
FileInputStream fileInputStream = new FileInputStream(file);


//3.创建缓冲区
byte[] b = new byte[1024];


//4.读取数据
int count = 0;
while((count = fileInputStream.read(b))!=-1){
System.out.println(new String(b,0,count));
//是说把数组b中的元素付给String,0是指偏移量,从数组第0个开始
}
fileInputStream.close();

}

从磁盘读取一个文件到内存中,再打印到控制台

标签:out   bsp   txt   main   偏移量   建立   ring   class   目标   

原文地址:http://www.cnblogs.com/future-zmy/p/6135482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!