從磁盤讀取一個文件到內存中,再打印到控制台


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