无法在流的结尾之外进行读取_IO流,字节流,字符流
IO流
輸入(Input)指的是:可以讓程序從外部系統獲得數據(核心含義是“讀”,讀取外部數據)。
輸出(Output)指的是:程序輸出數據給外部系統從而可以操作外部系統(核心含義是“寫”,將數據寫出到外部系
按流的方向分類:
1. 輸入流:數據流向是數據源到程序(以InputStream、Reader結尾的流)。
2. 輸出流:數據流向是程序到目的地(以OutPutStream、Writer結尾的流)。統)。
按處理的數據單元分類:
字節流:以字節為單位獲取數據,命名上以Stream結尾的流一般是字節流,如FileInputStream、FileOutputStream。
字符流:以字符為單位獲取數據,命名上以Reader/Writer結尾的流一般是字符流,如FileReader、FileWriter。
按處理對象不同分類:
1. 節點流:可以直接從數據源或目的地讀寫數據,如FileInputStream、FileReader、DataInputStream等。
2. 處理流:不直接連接到數據源或目的地,是”處理流的流”。通過對其他流的處理提高程序的性能,如BufferedInputStream、BufferedReader等。處理流也叫包裝流。
節點流處于IO操作的第一線,所有操作必須通過它們進行;處理流可以對節點流進行包裝,提高性能或提高程序的靈活性。
FileInputStream構造方法:
FileInputStream(File file) 通過打開與實際文件的連接創建一個 FileInputStream ,該文件由文件系統中的 File對象 file命名。
FileInputStream(String name) 通過打開與實際文件的連接來創建一個 FileInputStream ,該文件由文件系統中的路徑名 name命名。
常用方法:
read() 從該輸入流讀取一個字節的數據。
read(byte[] b) 從該輸入流讀取最多 b.length個字節的數據為字節數組。
close() 關閉此文件輸入流并釋放與流相關聯的任何系統資源。
package com.sxt.io;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /*** 構造方法:* FileInputStream(File file) 通過打開與實際文件的連接創建一個 FileInputStream ,該文件由文件系統中的 File對象 file命名。* FileInputStream(String name) 通過打開與實際文件的連接來創建一個 FileInputStream ,該文件由文件系統中的路徑名 name命名。 * 常用方法:* read() 從該輸入流讀取一個字節的數據。* read(byte[] b) 從該輸入流讀取最多 b.length個字節的數據為字節數組。 * close() 關閉此文件輸入流并釋放與流相關聯的任何系統資源。 */ public class TestFileInputStream {public static void main(String[] args) throws FileNotFoundException {FileInputStream file = new FileInputStream("D:/b.txt");//內容是abcdefgint temp = 0;try {while((temp=file.read()) != -1) {//當等于-1是,說明到結尾了char ch = (char) temp;System.out.print(ch);}} catch (IOException e) {e.printStackTrace();}try {file.close();} catch (IOException e) {e.printStackTrace();}}}常用方法:
read(byte[] b) 從該輸入流讀取最多 b.length個字節的數據為字節數組。
package com.sxt.io;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /*** read(byte[] b) 從該輸入流讀取最多 b.length個字節的數據為字節數組。* @date 2019年7月30日*/ public class TestFileInputStream2 {public static void main(String[] args) {FileInputStream fs = null;try {fs = new FileInputStream("D:/a.txt");//abcdefgbyte[] bs = new byte[1024];int length = fs.read(bs);System.out.println("讀取到的字節數:"+length);//7String str = new String(bs);//String str = new String(bs,0,11);System.out.println(str);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {fs.close();} catch (IOException e) {e.printStackTrace();}}}java.io.OutputStream
--java.io.FileOutputStream : 繼承了OutputStream,文件輸出流是用于將數據寫入到輸出流File
構造方法:
FileOutputStream(File file) 創建文件輸出流以寫入由指定的 File對象表示的文件。
FileOutputStream(String name) 創建文件輸出流以指定的名稱寫入文件。
FileOutputStream(String name, boolean append) 創建文件輸出流以指定的名稱寫入文件。
FileOutputStream(File file, boolean append) 創建文件輸出流以寫入由指定的 File對象表示的文件。
常用方法:
write(int b) 將指定的字節寫入此文件輸出流。
write(byte[] b) 將 b.length個字節從指定的字節數組寫入此文件輸出流。
close() 關閉此文件輸出流并釋放與此流相關聯的任何系統資源。
package com.sxt.io;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;/*** java.io.OutputStream * --java.io.FileOutputStream : 繼承了OutputStream,文件輸出流是用于將數據寫入到輸出流File* 構造方法:*FileOutputStream(File file) 創建文件輸出流以寫入由指定的 File對象表示的文件。 *FileOutputStream(String name) 創建文件輸出流以指定的名稱寫入文件。 *FileOutputStream(String name, boolean append) 創建文件輸出流以指定的名稱寫入文件。*FileOutputStream(File file, boolean append) 創建文件輸出流以寫入由指定的 File對象表示的文件。* 常用方法:* write(int b) 將指定的字節寫入此文件輸出流。 * write(byte[] b) 將 b.length個字節從指定的字節數組寫入此文件輸出流。 * close() 關閉此文件輸出流并釋放與此流相關聯的任何系統資源。 */ public class TestFileOutputStream {public static void main(String[] args) {FileOutputStream fos = null;try {fos = new FileOutputStream("E:/b.txt");fos.write('a');fos.write('b');fos.write('c');fos.write('d');System.out.println("寫入完畢!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}} }將字符串添加到另一個文件中
package com.sxt.io; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;/*** 將字符串添加到另一個文件中*/ public class TestFileOutputStream2 {public static void main(String[] args) {FileOutputStream fos = null;try {fos = new FileOutputStream("D:/b.txt");fos.write('a');fos.write('b');fos.write('c');String str = "hello";fos.write(str.getBytes());System.out.println("輸入完成!");//abchello} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}}}數組拷貝
使用輸入流從源中將文件讀取出來
使用輸出流將文件寫入到目的地
package com.sxt.io;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;/*** 使用輸入流從源中將文件讀取出來* 使用輸出流將文件寫入到目的地* @date 2019年7月30日*/ public class TestCopy {public static void main(String[] args) throws IOException {FileInputStream fis = null;FileOutputStream fos = null;fis = new FileInputStream("D:/b.txt");fos = new FileOutputStream("E:/b.txt");int i = 0;while((i=fis.read()) != -1){fos.write(i);}System.out.println("復制完成!");fis.close();fos.close();} }四大IO抽象類
·InputStream
此抽象類是表示字節輸入流的所有類的父類。InputSteam是一個抽象類,它不可以實例化。 數據的讀取需要由它的子類來實現。根據節點的不同,它派生了不同的節點流子類 。
繼承自InputSteam的流都是用于向程序中輸入數據,且數據的單位為字節(8 bit)。
常用方法:
int read():讀取一個字節的數據,并將字節的值作為int類型返回(0-255之間的一個值)。如果未讀出字節則返回-1(返回值為-1表示讀取結束)。
void close():關閉輸入流對象,釋放相關系統資源。
· OutputStream
此抽象類是表示字節輸出流的所有類的父類。輸出流接收輸出字節并將這些字節發送到某個目的地。
常用方法:
void write(int n):向目的地中寫入一個字節。
void close():關閉輸出流對象,釋放相關系統資源。
· Reader
Reader用于讀取的字符流抽象類,數據單位為字符。
int read(): 讀取一個字符的數據,并將字符的值作為int類型返回(0-65535之間的一個值,即Unicode值)。如果未讀出字符則返回-1(返回值為-1表示讀取結束)。
void close() : 關閉流對象,釋放相關系統資源。
· Writer
Writer用于寫入的字符流抽象類,數據單位為字符。
void write(int n): 向輸出流中寫入一個字符。
void close() : 關閉輸出流對象,釋放相關系統資源。
總結
以上是生活随笔為你收集整理的无法在流的结尾之外进行读取_IO流,字节流,字符流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 报错注入 读文件_SQL注入
- 下一篇: 汕尾二马路成兴街老房子房价是多少?