位元組流
位元組輸出流【OutputStream】
java.io.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) :將指定的位元組輸出流,
FileOutputStream類
java.io.FileOutputStream 類是檔案輸出流,用于將資料寫出到檔案,
構造方法
- public FileOutputStream(File file) :創建檔案輸出流以寫入由指定的 File物件表示的檔案,
- public FileOutputStream(String name) : 創建檔案輸出流以指定的名稱寫入檔案,
當你創建一個流物件時,必須傳入一個檔案路徑,該路徑下,如果沒有這個檔案,會創建該檔案,如果有這個檔案,會清空這個檔案的資料,
public class FileOutputStreamConstructor throws IOException {
public static void main(String[] args) {
// 使用File物件創建流物件
File file = new File("a.txt");
FileOutputStream fos = new FileOutputStream(file);
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("b.txt");
}
}
寫出位元組資料
- 寫出位元組: write(int b) 方法,每次可以寫出一個位元組資料,代碼演示:
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt");
// 寫出資料
fos.write(97); // 寫出第1個位元組
fos.write(98); // 寫出第2個位元組
fos.write(99); // 寫出第3個位元組
// 關閉資源
fos.close();
}
}
輸出結果:
abc
- 寫出位元組陣列: write(byte[] b) ,每次可以寫出陣列中的資料,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt");
//字串轉換成位元組陣列
byte[] b="我是位元組陣列".getBytes();
// 寫出位元組陣列資料
fos.write(b);
// 關閉資源
fos.close();
}
}
輸出結果:
我是位元組陣列
- 寫出指定長度位元組陣列: write(byte[] b, int off, int len) ,每次寫出從off索引開始,len個位元組,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt");
//字串轉換成位元組陣列
byte[] b = "abcdefg".getBytes();
// 寫出從索引3開始,2個位元組,索引3是d,兩個位元組,也就是de,
fos.write(b,3,2);
// 關閉資源
fos.close();
}
}
輸出結果:
de
資料續寫
- public FileOutputStream(File file, boolean append) : 創建檔案輸出流以寫入由指定的 File物件表示的檔案,
- public FileOutputStream(String name, boolean append) : 創建檔案輸出流以指定的名稱寫入檔案
這兩個構造方法,引數中都需要傳入一個boolean型別的值, true 表示追加資料, false 表示清空原有資料
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileOutputStream fos = new FileOutputStream("fos.txt",true);
//字串轉換成位元組陣列
byte[] b = "abcdefg".getBytes();
fos.write(b);
// 關閉資源
fos.close();
}
}
檔案操作前:de
檔案操作后:deabcdefg
寫出換行
Windows系統里,換行符號是 \r\n ,代碼演示:
public class Demo {
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();
}
}
輸出結果:
a
b
c
d
e
位元組輸入流【InputStream】
java.io.InputStream 抽象類是表示位元組輸入流的所有類的超類,可以讀取位元組資訊到記憶體中,它定義了位元組輸入流的基本共性功能方法,
- public void close() :關閉此輸入流并釋放與此流相關聯的任何系統資源,
- public abstract int read() : 從輸入流讀取資料的下一個位元組,
- public int read(byte[] b) : 從輸入流中讀取一些位元組數,并將它們存盤到位元組陣列 b中
FileInputStream類
java.io.FileInputStream 類是檔案輸入流,從檔案中讀取位元組,
構造方法
- FileInputStream(File file) : 通過打開與實際檔案的連接來創建一個 FileInputStream ,該檔案由檔案系統中的 File物件 file命名,
- FileInputStream(String name) : 通過打開與實際檔案的連接來創建一個 FileInputStream ,該檔案由檔案系統中的路徑名 name命名,
當你創建一個流物件時,必須傳入一個檔案路徑,該路徑下,如果沒有該檔案,會拋出 FileNotFoundException
public class Demo throws IOException{
public static void main(String[] args) {
// 使用File物件創建流物件
File file = new File("a.txt");
FileInputStream fos = new FileInputStream(file);
// 使用檔案名稱創建流物件
FileInputStream fos = new FileInputStream("b.txt");
}
}
讀取位元組資料
- 讀取位元組: read 方法,每次可以讀取一個位元組的資料,提升為int型別,讀取到檔案末尾,回傳 -1 ,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("a.txt");// 檔案中為abc
// 讀取資料,回傳一個位元組
int read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
// 讀取到末尾,回傳-1
read = fis.read();
System.out.println( read);
// 關閉資源
fis.close();
}
}
輸出結果:
a
b
c
-1
改進代碼:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("a.txt");// 檔案中為abc
// 定義變數,保存資料
int b;
// 回圈讀取
while ((b = fis.read())!=-1) {
System.out.println((char)b);
}
// 關閉資源
fis.close();
}
}
輸出結果:
a
b
c
- 使用位元組陣列讀取: read(byte[] b) ,每次讀取b的長度個位元組到陣列中,回傳讀取到的有效位元組個數,讀取到末尾時,回傳 -1 ,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("a.txt");// 檔案中為abc
// 定義變數,作為有效個數
int len;
// 定義位元組陣列,作為裝位元組資料的容器
byte[] b = new byte[2];
// 回圈讀取
while (( len = fis.read(b))!=-1) {
// 每次讀取后,把陣列變成字串列印
System.out.println(new String(b));
}
// 關閉資源
fis.close();
}
}
輸出結果:
ab
cb
錯誤資料 b,是由于最后一次讀取時,只讀取一個位元組 c ,陣列中,上次讀取的資料沒有被完全替換,所以要通過 len ,獲取有效的位元組,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileInputStream fis = new FileInputStream("a.txt");// 檔案中為abc
// 定義變數,作為有效個數
int len;
// 定義位元組陣列,作為裝位元組資料的容器
byte[] b = new byte[2];
// 回圈讀取
while (( len = fis.read(b))!=-1) {
// 每次讀取后,把陣列變成字串列印
System.out.println(new String(b,0,len));
}
// 關閉資源
fis.close();
}
}
輸出結果:
ab
c
復制圖片檔案,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 1.創建流物件
// 1.1 指定資料源
FileInputStream fis = new FileInputStream("D:\\test.png");
// 1.2 指定目的地
FileOutputStream fos = new FileOutputStream("D:\\test_copy.png");
// 2.讀寫資料
// 2.1 定義陣列
byte[] b = new byte[1024];
// 2.2 定義長度
int len;
// 2.3 回圈讀取
while ((len = fis.read(b))!=-1) {
// 2.4 寫出資料
fos.write(b, 0 , len);
}
// 3.關閉資源
fos.close();
fis.close();
}
}
字符流
Java提供一些字符流類,以字符為單位讀寫資料,專門用于處理文本檔案,
字符輸入流【Reader】
java.io.Reader 抽象類是表示用于讀取字符流的所有類的超類,可以讀取字符資訊到記憶體中,它定義了字符輸入流的基本共性功能方法,
- public void close() :關閉此流并釋放與此流相關聯的任何系統資源,
- public int read() : 從輸入流讀取一個字符,
- public int read(char[] cbuf) : 從輸入流中讀取一些字符,并將它們存盤到字符陣列 cbuf中
FileReader類
構造方法
- FileReader(File file) : 創建一個新的 FileReader ,給定要讀取的File物件,
- FileReader(String fileName) : 創建一個新的 FileReader ,給定要讀取的檔案的名稱,
public class Demo throws IOException{
public static void main(String[] args) {
// 使用File物件創建流物件
File file = new File("a.txt");
FileReader fr = new FileReader(file);
// 使用檔案名稱創建流物件
FileReader fr = new FileReader("b.txt");
}
}
讀取字符資料
- 讀取字符: read 方法,每次可以讀取一個字符的資料,提升為int型別,讀取到檔案末尾,回傳 -1 ,回圈讀取,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileReader fr = new FileReader("a.txt");//內容為:我喜歡編程
// 定義變數,保存資料
int b ;
// 回圈讀取
while ((b = fr.read())!=-1) {
System.out.println((char)b);
}
// 關閉資源
fr.close();
}
}
輸出結果:
我
喜
歡
編
程
- 使用字符陣列讀取: read(char[] cbuf) ,每次讀取b的長度個字符到陣列中,回傳讀取到的有效字符個數,
讀取到末尾時,回傳 -1 ,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileReader fr = new FileReader("a.txt");//內容為:我喜歡編程
// 定義變數,保存資料
int len ;
// 定義字符陣列,作為裝字符資料的容器
char[] b = new char[2];
// 回圈讀取
while ((len = fr.read(b))!=-1) {
System.out.println(new String(b,0,len));
}
// 關閉資源
fr.close();
}
}
輸出結果為:
我喜
歡編
程
字符輸出流【Writer】
java.io.Writer 抽象類是表示用于寫出字符流的所有類的超類,將指定的字符資訊寫出到目的地,它定義了位元組輸出流的基本共性功能方法,
- void write(int c) 寫入單個字符,
- void write(char[] cbuf) 寫入字符陣列,
- abstract void write(char[] cbuf, int off, int len) 寫入字符陣列的某一部分,off陣列的開始索引,len寫的字符個數,
- void write(String str) 寫入字串,
- void write(String str, int off, int len) 寫入字串的某一部分,off字串的開始索引,len寫的字符個數,
- void flush() 重繪該流的緩沖,
- void close() 關閉此流,但要先重繪它,
FileWriter類
構造方法
- FileWriter(File file) : 創建一個新的 FileWriter,給定要讀取的File物件,
- FileWriter(String fileName) : 創建一個新的FileWriter,給定要讀取的檔案的名稱,
public class Demo{
public static void main(String[] args) throws IOException {
// 使用File物件創建流物件
File file = new File("a.txt");
FileWriter fw = new FileWriter(file);
// 使用檔案名稱創建流物件
FileWriter fw = new FileWriter("b.txt");
}
}
基本寫出資料
寫出字符: write(int b) 方法,每次可以寫出一個字符資料,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter fw = new FileWriter("b.txt");
// 寫出資料
fw.write(97); // 寫出第1個字符
fw.write('b'); // 寫出第2個字符
fw.write('C'); // 寫出第3個字符
fw.write(33333); // 寫出第4個字符,中文編碼表中30000對應一個漢字,
//如果不關閉,資料只是保存到緩沖區,并未保存到檔案,
fw.close();
}
}
輸出結果:
abC舵
關閉和重繪
因為內置緩沖區的原因,如果不關閉輸出流,無法寫出字符到檔案中,但是關閉的流物件,是無法繼續寫出資料的,如果我們既想寫出資料,又想繼續使用流,就需要 flush 方法了,
flush :重繪緩沖區,流物件可以繼續使用,
close :先重繪緩沖區,然后通知系統釋放資源,流物件不可以再被使用了,
寫出其他資料
- 寫出字符陣列 : write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter b = new FileWriter("a.txt");
// 字串轉換為位元組陣列
char[] chars = "我喜歡編程".toCharArray();
// 寫出字符陣列
b.write(chars); // 我喜歡編程
// 寫出從索引1開始,1個位元組,索引1是'喜',兩個位元組,也就是'喜歡',
b.write(chars,1,2); // 喜歡
// 關閉資源
b.close();
}
}
輸出結果:
我喜歡編程喜歡
- 寫出字串: write(String str) 和 write(String str, int off, int len) ,每次可以寫出字串中的
資料,更為方便,代碼演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter b = new FileWriter("a.txt");
//字串
String msg = "我喜歡編程";
// 寫出字符陣列
b.write(msg); //我喜歡編程
// 寫出從索引1開始,2個位元組,索引1是'喜',兩個位元組,也就是'喜歡',
b.write(msg,1,2); // 喜歡
// 關閉資源
b.close();
}
}
- 續寫和換行:操作類似于FileOutputStream
public class Demo{
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件,可以續寫資料
FileWriter b= new FileWriter("a.txt",true);
// 寫出字串
b.write("我喜歡");
// 寫出換行
b.write("\r\n");
// 寫出字串
b.write("編程");
// 關閉資源
b.close();
}
}
輸出結果:
我喜歡
編程
字符流,只能操作文本檔案,不能操作圖片,視頻等非文本檔案,要使用位元組流
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/116752.html
標籤:Java
