FileInputStream 和 FileOutputStream
-
InputStream:位元組輸入流
InputStream抽象類是所有類輸入流的超類
InputStream 常用的子類
- FileInputStream: 檔案輸入流
- BufferedInputStream:緩沖位元組輸入流
- ObjectInputStream:物件位元組輸入流
一、FileInputStream
-
構造方法摘要
Constructor and Description FileInputStream(File file)
通過打開與實際檔案的連接創建一個FileInputStream
,該檔案由檔案系統中的File
物件file
命名,FileInputStream(FileDescriptor fdObj)
創建FileInputStream
通過使用檔案描述符fdObj
,其表示在檔案系統中的現有連接到一個實際的檔案,FileInputStream(String name)
通過打開與實際檔案的連接來創建一個FileInputStream
,該檔案由檔案系統中的路徑名name
命名, -
方法摘要
Modifier and Type Method and Description int
available()
回傳從此輸入流中可以讀取(或跳過)的剩余位元組數的估計值,而不會被下一次呼叫此輸入流的方法阻塞,void
close()
關閉此檔案輸入流并釋放與流相關聯的任何系統資源,protected void
finalize()
確保當這個檔案輸入流的close
方法沒有更多的參考時被呼叫,FileChannel
getChannel()
回傳與此檔案輸入流相關聯的唯一的FileChannel
物件,FileDescriptor
getFD()
回傳表示與此FileInputStream
正在使用的檔案系統中實際檔案的連接的FileDescriptor
物件,int
read()
從該輸入流讀取一個位元組的資料,int
read(byte[] b)
從該輸入流讀取最多b.length
個位元組的資料為位元組陣列,int
read(byte[] b, int off, int len)
從該輸入流讀取最多len
位元組的資料為位元組陣列,long
skip(long n)
跳過并從輸入流中丟棄n
位元組的資料,
代碼演示:
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author
* @version 1.0
*/
public class FileInputStream_ {
public static void main(String[] args) {
}
/**
* 演示讀取檔案
* read(byte b)單個位元組的讀取,效率較低
* 使用 read(byte[] b) 來讀取
*/
@Test
public void readFile01(){
String filePath = "e:\\hello.txt";
int readData = https://www.cnblogs.com/zh-Note/archive/2023/06/04/0;
FileInputStream fileInputStream = null;
try {
//創建 FileInputStream 物件,用于讀取檔案
fileInputStream = new FileInputStream(filePath);
//從該輸入流讀取一個位元組的資料,如果沒有輸入可用,此方法將阻止
//如果回傳-1.表示讀取完畢
while((readData = fileInputStream.read()) != -1){
System.out.print((char)readData);//轉成char顯示
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02(){
String filePath ="e:\\hello.txt";
byte[] buf = new byte[8];//一次讀8個位元組
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//創建 FileInputStream 物件,用于讀取檔案
fileInputStream = new FileInputStream(filePath);
// 讀取的位元組數最多等于b的長度,
// 令k為實際讀取的位元組數; 這些位元組將存盤在元素b[0]至b[ k -1] ,使元素b[ k ]至b[b.length-1]不受影響,
// 回傳讀取到緩沖區的總位元組數,或者如果沒有更多的資料,因為已經到達流的末尾,則是 -1 ,
while((readLen = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readLen));//顯示
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*運行結果
hello world
hello world
*/
二、FileOutputStream
-
構造方法摘要
Constructor and Description FileOutputStream(File file)
創建檔案輸出流以寫入由指定的File
物件表示的檔案,FileOutputStream(File file, boolean append)
創建檔案輸出流以寫入由指定的File
物件表示的檔案,FileOutputStream(FileDescriptor fdObj)
創建檔案輸出流以寫入指定的檔案描述符,表示與檔案系統中實際檔案的現有連接,FileOutputStream(String name)
創建檔案輸出流以指定的名稱寫入檔案,FileOutputStream(String name, boolean append)
創建檔案輸出流以指定的名稱寫入檔案, -
方法摘要
Modifier and Type Method and Description void
close()
關閉此檔案輸出流并釋放與此流相關聯的任何系統資源,protected void
finalize()
清理與檔案的連接,并確保當沒有更多的參考此流時,將呼叫此檔案輸出流的close
方法,FileChannel
getChannel()
回傳與此檔案輸出流相關聯的唯一的FileChannel
物件,FileDescriptor
getFD()
回傳與此流相關聯的檔案描述符,void
write(byte[] b)
將b.length
個位元組從指定的位元組陣列寫入此檔案輸出流,void
write(byte[] b, int off, int len)
將len
位元組從位于偏移量off
的指定位元組陣列寫入此檔案輸出流,void
write(int b)
將指定的位元組寫入此檔案輸出流,
代碼演示:
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author
* @version 1.0
*/
public class FileOutputStream_ {
public static void main(String[] args) {
}
@Test
public void writeFile(){
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
String str = "Hello World!";
//得到FileOutputStream物件
//1. new FileOutputStream(filePath) 創建方式,寫入內容會默認覆寫原來的內容
//2. new FileOutputStream(filePath, append) 創建方式,append 為ture,寫入內容會默認追加至原來的內容后,否則就覆寫
fileOutputStream = new FileOutputStream(filePath, true);
//寫入一個位元組
fileOutputStream.write('H');
//寫入字串,str.getBytes() 可以把 字串 -》字符陣列
fileOutputStream.write(str.getBytes());
/*
write(byte[] b, int off, int len) 將 len 位元組從位于偏移量 off的指定位元組陣列寫入檔案輸出流
*/
// fileOutputStream.write(str.getBytes(), 0, str.length());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/554257.html
標籤:其他
上一篇:p2 IO流原理及流的分類
下一篇:返回列表