1、位元組流
位元組流是什么:
? 一切檔案資料在存盤時,都是以二進制數字的形式保存,都一個一個的位元組,那么傳輸時一樣如此,所以,位元組流可以傳輸任意檔案資料,在操作流的時候,我們要時刻明確,無論使用什么樣的流物件,底層傳輸的始終為二進制資料,
流的關閉原則:
? 先開后關,后開先關,
2、位元組輸出流 OutputStream:
* public void close():關閉此輸出流并釋放與此流相關聯的任何系統資源,
* public void flush() :重繪此輸出流并強制任何緩沖的輸出位元組被寫出,
* public void write(byte[] b):將 b.length位元組從指定的位元組陣列寫入此輸出流,
* public void write(byte[] b, int off, int len) :從指定的位元組陣列寫入 len位元組,從偏移量 off開始輸出到此輸出流,
*public abstract void write(int b) :將指定的位元組輸出流,
close方法,當完成流的操作時,必須呼叫此方法,釋放系統資源,
FileOutputStream類
OutputStream有很多子類,我們從最簡單的一個子類開始,
java.io.FileOutputStream 類是檔案輸出流,用于將資料寫出到檔案,
//創建流物件
public class FileOutputStreamConstructor throws IOException {
public static void main(String[] args) {
// 使用File物件創建流物件
File file = new File("a.xt");
FileOutputStream fos = new FileOutputStream(file);
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("v.txt")
}
}
//向檔案寫入int型別的資料
1 創建流物件
2 寫資料
3 關閉資源
public class Fwrite{
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt");
// 寫出資料
fos.write(97); // 寫出第1個位元組
fos.write(98); // 寫出第2個位元組
// 關閉資源
fos.close();
}
}
//換行書寫
public class Fwrite{
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt");
// 定義位元組陣列
byte[] words = {97,98,99,100,101};
// 遍歷陣列
for (int i = 0; i < words.length; i++) {
// 寫出一個位元組
fos.write(words[i]);
// 寫出一個換行, 換行符號轉成陣列寫出
fos.write("\r\n".getBytes());
}
// 關閉資源
fos.close();
}
3、位元組輸入流InputStream
public void close() :關閉此輸入流并釋放與此流相關聯的任何系統資源,
public abstract int read(): 從輸入流讀取資料的下一個位元組,
public int read(byte[] b): 從輸入流中讀取一些位元組數,并將它們存盤到位元組陣列 b中 ,
//當創建一個流物件時,必須傳入一個檔案路徑,該路徑下,如果沒有該檔案,會拋出FileNotFoundException
public class FileInput throws IOException{
public static void main(String[] args) {
// 使用File物件創建流物件
File file = new File("adc.txt");
FileInputStream fos = new FileInputStream(file);
// 使用檔案名稱創建流物件
FileInputStream fos = new FileInputStream("b.txt");
}
}
讀取位元組資料:
public class FRead {
public static void main(String[] args) throws IOException{
/*
//一次讀取一位元組
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("read.txt");
// 讀取資料,回傳一個位元組
int read = fis.read();
System.out.println((char) read);
read = fis.read()
System.out.println((char) read);
read = fis.read()
System.out.println( read);
// 關閉資源
fis.close();
*/
//回圈讀取資料
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("read.txt");
// 定義變數,保存資料
int b ;
// 回圈讀取
while ((b = fis.read())!=-1) {
System.out.println((char)b);
}
// 關閉資源
fis.close();
}
}
//使用位元組陣列讀取
public class FRead {
public static void main(String[] args) throws IOException{
// 使用檔案名稱創建流物件.
FileInputStream fis = new FileInputStream("adc.txt"); // 檔案中為abcde
// 定義變數,作為有效個數
int len ;
// 定義位元組陣列,作為裝位元組資料的容器
byte[] b = new byte[2];
// 回圈讀取
while (( len= fis.read(b))!=-1) {
// 每次讀取后,把陣列的有效位元組部分,變成字串列印
System.out.println(new String(b,0,len));// len 每次讀取的有效位元組個數
}
// 關閉資源
fis.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/229447.html
標籤:java
上一篇:Tcl技巧與bug匯總
下一篇:多執行緒java、python
