psd網(wǎng)站首頁圖片如何查網(wǎng)站的空間
鶴壁市浩天電氣有限公司
2026/01/24 14:18:48
psd網(wǎng)站首頁圖片,如何查網(wǎng)站的空間,手機(jī)營銷型網(wǎng)站制作,seo查詢51181.1 什么是IO當(dāng)你編輯一個文本文件#xff0c;忘記了 ctrls 可能文件就白白編輯了。當(dāng)你電腦上插入一個U盤#xff0c;可以把一個視頻#xff0c;拷貝到你的電腦硬盤里。那么數(shù)據(jù)都是在哪些設(shè)備上的呢#xff1f;鍵盤、內(nèi)存、硬盤、外接設(shè)備等等。我們把這種數(shù)據(jù)的傳輸忘記了 ctrls 可能文件就白白編輯了。當(dāng)你電腦上插入一個U盤可以把一個視頻拷貝到你的電腦硬盤里。那么數(shù)據(jù)都是在哪些設(shè)備上的呢鍵盤、內(nèi)存、硬盤、外接設(shè)備等等。我們把這種數(shù)據(jù)的傳輸可以看做是一種數(shù)據(jù)的流動按照流動的方向以內(nèi)存為基準(zhǔn)分為 輸入input和 輸出output即流向內(nèi)存是輸入流流出內(nèi)存的輸出流。Java中I/O操作主要是指使用 java.io 包下的內(nèi)容進(jìn)行輸入、輸出操作。輸入也叫做讀取數(shù)據(jù)輸出也叫做作寫出數(shù)據(jù)。1.2 IO的分類根據(jù)數(shù)據(jù)的流向分為輸入流和輸出流。輸入流把數(shù)據(jù)從其他設(shè)備上讀取到內(nèi)存中的流輸出流把數(shù)據(jù)從內(nèi)存中寫出到其他設(shè)備上的流格局?jǐn)?shù)據(jù)的類型分為字節(jié)流和字符流字節(jié)流以字節(jié)為單位讀寫數(shù)據(jù)的流。字符流以字符為單位讀寫數(shù)據(jù)的流。1.3 頂級父類們輸入流輸出流字節(jié)流字節(jié)輸入流InputStream字節(jié)輸出流OutputStream字符流字符輸入流Reader字符輸出流Writer2.1 字節(jié)流一切文件數(shù)據(jù)(文本、圖片、視頻等)在存儲時都是以二進(jìn)制數(shù)字的形式保存都一個一個的字節(jié)那么傳輸時一樣如此。所以字節(jié)流可以傳輸任意文件數(shù)據(jù)。在操作流的時候我們要時刻明確無論使用什么樣的流對象底層傳輸?shù)氖冀K為二進(jìn)制數(shù)據(jù)。2.2 字節(jié)輸出流【OutputStream】java.io.OutputStream抽象類是表示字節(jié)輸出流的所有類的超類將指定的字節(jié)信息寫出到目的地。它定義了字節(jié)輸出流的基本共性功能方法。public void close()關(guān)閉此輸出流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源public void flush()刷新此輸出流并強(qiáng)制任何緩沖的輸出字節(jié)被寫出public void write(byte[] b)將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流public void write(byte[] b, int off, int len)從指定的字節(jié)數(shù)組寫入 len字節(jié)從偏移量 off開始輸出到此輸出流public abstract void write(int b)將指定的字節(jié)輸出流2.3 FileOutputStream類OutputStream有很多子類我們從最簡單的一個子類開始java.io.FileOutputStream類是文件輸出流用于將數(shù)據(jù)寫出到文件構(gòu)造方法public FileOutputStream(File file)創(chuàng)建文件輸出流以寫入由指定的 File對象表示的文件public FileOutputStream(String name)創(chuàng)建文件輸出流以指定的名稱寫入文件。當(dāng)你創(chuàng)建一個流對象時必須傳入一個文件路徑。該路徑下如果沒有這個文件會創(chuàng)建該文件。如果有這個文件會清空這個文件的數(shù)據(jù)。構(gòu)造舉例示例代碼public class FileOutputStreamConstructor throws IOException { public static void main(String[] args) { // 使用File對象創(chuàng)建流對象 File file new File(a.txt); FileOutputStream fos new FileOutputStream(file); // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(b.txt); } }寫出字節(jié)數(shù)據(jù)1.寫出字節(jié)write(int b)方法每次可以寫出一個字節(jié)數(shù)據(jù)示例代碼public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(fos.txt); // 寫出數(shù)據(jù) fos.write(97); // 寫出第1個字節(jié) fos.write(98); // 寫出第2個字節(jié) fos.write(99); // 寫出第3個字節(jié) // 關(guān)閉資源 fos.close(); } } //輸出結(jié)果 abc2.寫出字節(jié)數(shù)組write(byte[] b)每次可以寫出數(shù)組中的數(shù)據(jù)示例代碼public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(fos.txt); // 字符串轉(zhuǎn)換為字節(jié)數(shù)組 byte[] b JAVA.getBytes(); // 寫出字節(jié)數(shù)組數(shù)據(jù) fos.write(b); // 關(guān)閉資源 fos.close(); } } //輸出結(jié)果 JAVA3.寫出指定長度字節(jié)數(shù)組write(byte[] b, int off, int len),每次寫出從off索引開始len個字節(jié)示例代碼public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(fos.txt); // 字符串轉(zhuǎn)換為字節(jié)數(shù)組 byte[] b abcde.getBytes(); // 寫出從索引2開始2個字節(jié)。索引2是c兩個字節(jié)也就是cd。 fos.write(b,2,2); // 關(guān)閉資源 fos.close(); } } //輸出結(jié)果 cd數(shù)據(jù)追加續(xù)寫經(jīng)過以上的演示每次程序運(yùn)行創(chuàng)建輸出流對象都會清空目標(biāo)文件中的數(shù)據(jù)。如何保留目標(biāo)文件中數(shù)據(jù)還能繼續(xù)添加新數(shù)據(jù)呢public FileOutputStream(File file, boolean append)建文件輸出流以寫入由指定的 File對象表示的文件public FileOutputStream(String name, boolean append) 創(chuàng)建文件輸出流以指定的名稱寫入文件。這兩個構(gòu)造方法參數(shù)中都需要傳入一個boolean類型的值,true表示追加數(shù)據(jù) ,false表示清空原有數(shù)據(jù)。這樣創(chuàng)建的輸出流對象就可以指定是否追加續(xù)寫了示例代碼public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(fos.txttrue); // 字符串轉(zhuǎn)換為字節(jié)數(shù)組 byte[] b abcde.getBytes(); // 寫出從索引2開始2個字節(jié)。索引2是c兩個字節(jié)也就是cd。 fos.write(b); // 關(guān)閉資源 fos.close(); } } //文件操作前cd //文件操作后cdabcde寫出換行Windows系統(tǒng)里換行符號是
以指定是否追加續(xù)寫了示例代碼public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名稱創(chuàng)建流對象 FileOutputStream fos new FileOutputStream(fos.txt); // 定義字節(jié)數(shù)組 byte[] words {97,98,99,100,101}; // 遍歷數(shù)組 for (int i 0; i words.length; i) { // 寫出一個字節(jié) fos.write(words[i]); // 寫出一個換行, 換行符號轉(zhuǎn)成數(shù)組寫出 fos.write(
.getBytes()); } // 關(guān)閉資源 fos.close(); } } 輸出結(jié)果 a b c d e2.4 字節(jié)輸入流【InputStream】java.io.InputStream抽象類是表示字節(jié)輸入流的所有類的超類可以讀取字節(jié)信息到內(nèi)存中。它定義了字節(jié)輸入流的基本共性功能方法public void close()關(guān)閉此輸入流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源public abstract int read()從輸入流讀取數(shù)據(jù)的下一個字節(jié)public int read(byte[] b)從輸入流中讀取一些字節(jié)數(shù)并將它們存儲到字節(jié)數(shù)組 b中2.5 FileInputStream類java.io.FileInputStream類是文件輸入流從文件中讀取字節(jié)構(gòu)造方法FileInputStream(File file)通過打開與實際文件的連接來創(chuàng)建一個 FileInputStream 該文件由文件系統(tǒng)中的 File對象 file命名FileInputStream(String name)通過打開與實際文件的連接來創(chuàng)建一個 FileInputStream 該文件由文件系統(tǒng)中的路徑名 name命名當(dāng)創(chuàng)建一個流對象時必須傳入一個文件路徑。該路徑下如果沒有該文件,會拋出FileNotFoundException示例代碼public class FileInputStreamConstructor throws IOException{ public static void main(String[] args) { // 使用File對象創(chuàng)建流對象 File file new File(a.txt); FileInputStream fos new FileInputStream(file); // 使用文件名稱創(chuàng)建流對象 FileInputStream fos new FileInputStream(b.txt); } }讀取字節(jié)數(shù)據(jù)讀取字節(jié)read方法每次可以讀取一個字節(jié)的數(shù)據(jù)提升為 int 類型讀取到文件末尾返回-1示例代碼public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名稱創(chuàng)建流對象 FileInputStream fis new FileInputStream(read.txt); // 讀取數(shù)據(jù)返回一個字節(jié) 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); read fis.read(); System.out.println((char) read); read fis.read(); System.out.println((char) read); // 讀取到末尾,返回-1 read fis.read(); System.out.println( read); // 關(guān)閉資源 fis.close(); } } 輸出結(jié)果 a b c d e -1循環(huán)改進(jìn)讀取方式示例代碼2.使用字節(jié)數(shù)組讀取readbyte[ ] b每次讀取b的長度個字節(jié)到數(shù)組中返回讀取到的有效字節(jié)個數(shù)讀取到末尾時返回 -1示例代碼public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名稱創(chuàng)建流對象. FileInputStream fis new FileInputStream(read.txt); // 文件中為abcde // 定義變量作為有效個數(shù) int len // 定義字節(jié)數(shù)組作為裝字節(jié)數(shù)據(jù)的容器 byte[] b new byte[2]; // 循環(huán)讀取 while (( len fis.read(b))!-1) { // 每次讀取后,把數(shù)組變成字符串打印 System.out.println(new String(b)); } // 關(guān)閉資源 fis.close(); } } 輸出結(jié)果 ab cd ed錯誤數(shù)據(jù) d是由于最后一次讀取時只讀取一個字節(jié)e數(shù)組中上次讀取的數(shù)據(jù)沒有被完全替換所以要通過 len獲取有效的字節(jié)示例代碼public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名稱創(chuàng)建流對象. FileInputStream fis new FileInputStream(read.txt); // 文件中為abcde // 定義變量作為有效個數(shù) int len // 定義字節(jié)數(shù)組作為裝字節(jié)數(shù)據(jù)的容器 byte[] b new byte[2]; // 循環(huán)讀取 while (( len fis.read(b))!-1) { // 每次讀取后,把數(shù)組的有效字節(jié)部分變成字符串打印 System.out.println(new String(b0len));// len 每次讀取的有效字節(jié)個數(shù) } // 關(guān)閉資源 fis.close(); } } 輸出結(jié)果 ab cd e