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


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();

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM