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