Java的面向?qū)ο缶幊?/a>">Java的面向?qū)ο缶幊?/a>
642
2022-05-28
處理流之一:緩沖流的使用
1.緩沖流:
BufferedInputStream???BufferedOutputStream
BufferedReader? ? ? ? BufferedWriter
2. 作用:提供流的讀取、寫入的速度??提高讀寫速度的原因:內(nèi)部提供了一個緩沖區(qū)
3. 處理流,就是“套接”在已有的流的基礎(chǔ)上
BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //1.造文件 File srcFile = new File(srcPath); File destFile = new File(destPath); //2.造流 //2.1 造節(jié)點流 FileInputStream fis = new FileInputStream((srcFile)); FileOutputStream fos = new FileOutputStream(destFile); //2.2 造緩沖流 bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //3.復(fù)制的細節(jié):讀取、寫入 byte[] buffer = new byte[1024]; int len; while((len = bis.read(buffer)) != -1){ bos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4.資源關(guān)閉 //要求:先關(guān)閉外層的流,再關(guān)閉內(nèi)層的流 if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } //說明:關(guān)閉外層流的同時,內(nèi)層流也會自動的進行關(guān)閉。關(guān)于內(nèi)層流的關(guān)閉,我們可以省略. // fos.close(); // fis.close(); } }
@Test public void testBufferedReaderBufferedWriter(){ BufferedReader br = null; BufferedWriter bw = null; try { //創(chuàng)建文件和相應(yīng)的流 br = new BufferedReader(new FileReader(new File("dbcp.txt"))); bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt"))); //讀寫操作 //方式一:使用char[]數(shù)組 // char[] cbuf = new char[1024]; // int len; // while((len = br.read(cbuf)) != -1){ // bw.write(cbuf,0,len); // // bw.flush(); // } //方式二:使用String String data; while((data = br.readLine()) != null){ //方法一: // bw.write(data + "\n");//data中不包含換行符 //方法二: bw.write(data);//data中不包含換行符 bw.newLine();//提供換行的操作 } } catch (IOException e) { e.printStackTrace(); } finally { //關(guān)閉資源 if(bw != null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
處理流之二:轉(zhuǎn)換流的使用
1.轉(zhuǎn)換流:屬于字符流
InputStreamReader:將一個字節(jié)的輸入流轉(zhuǎn)換為字符的輸入流
OutputStreamWriter:將一個字符的輸出流轉(zhuǎn)換為字節(jié)的輸出流
2.作用:提供字節(jié)流與字符流之間的轉(zhuǎn)換
3. 解碼:字節(jié)、字節(jié)數(shù)組 ?--->? 字符數(shù)組、字符串 ?(看不懂到看得懂 -InputStreamReader)
編碼:字符數(shù)組、字符串 ---> 字節(jié)、字節(jié)數(shù)組 ?(OutputStreamWriter)
4.字符集
ASCII:美國標(biāo)準(zhǔn)信息交換碼。用一個字節(jié)的7位可以表示(合計8位,但是其中一位沒用,2*7 ?128)
ISO8859-1:拉丁碼表。歐洲碼表用一個字節(jié)的8位表示。
GB2312:中國的中文編碼表。最多兩個字節(jié)編碼所有字符
GBK:中國的中文編碼表升級,融合了更多的中文文字符號。最多兩個字節(jié)編碼
Unicode:國際標(biāo)準(zhǔn)碼,融合了目前人類使用的所有字符。為每個字符分配唯一的字符碼。所有的文字都用兩個字節(jié)來表示。
UTF-8:變長的編碼方式,可用1-4個字節(jié)來表示一個字符。
@Test public void test2() throws Exception { //1.造文件、造流 File file1 = new File("dbcp.txt"); File file2 = new File("dbcp_gbk.txt"); FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file2); InputStreamReader isr = new InputStreamReader(fis,"utf-8"); OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); //2.讀寫過程 char[] cbuf = new char[20]; int len; while((len = isr.read(cbuf)) != -1){ osw.write(cbuf,0,len); } //3.關(guān)閉資源 isr.close(); osw.close(); }
三 其他處理流的使用
1.標(biāo)準(zhǔn)的輸入、輸出流
2.打印流
3.數(shù)據(jù)流
其他流的使用
1.標(biāo)準(zhǔn)的輸入、輸出流
2.打印流
3.數(shù)據(jù)流
1.標(biāo)準(zhǔn)的輸入、輸出流
System.in:標(biāo)準(zhǔn)的輸入流,默認(rèn)從鍵盤輸入
System.out:標(biāo)準(zhǔn)的輸出流,默認(rèn)從控制臺輸出
System類的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定輸入和輸出的流
2 打印流:PrintStream 和PrintWriter
3. 數(shù)據(jù)流 :為了方便地操作Java語言的基本數(shù)據(jù)類型和String的數(shù)據(jù),可以使用數(shù)據(jù)流
將文件中存儲的基本數(shù)據(jù)類型變量和字符串讀取到內(nèi)存中,保存在變量中。
注意點:讀取不同類型的數(shù)據(jù)的順序要與當(dāng)初寫入文件時,保存的數(shù)據(jù)的順序一致!
3.1 DataInputStream 和 DataOutputStream
3.2 作用:用于讀取或?qū)懗龌緮?shù)據(jù)類型的變量或字符串
public class other { public static void main(String[] args) { BufferedReader br= null; //public final static InputStream in = null; try { InputStreamReader ins = new InputStreamReader(System.in); br= new BufferedReader(ins); while (true){ System.out.println("請輸入字符串: "); String s = br.readLine(); if ("e".equalsIgnoreCase(s) || "exit".equalsIgnoreCase(s)){ System.out.println("88"); break; } System.out.println(s.toUpperCase()); } } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test1(){ PrintStream ps = null; try { FileOutputStream fos = new FileOutputStream("alex.txt"); ps = new PrintStream(fos, true); //自動刷新 if (ps !=null ){ System.setOut(ps); // 指定打印位置 } for (int i = 0; i <=100 ; i++) { System.out.print((char) i ); if (i % 50 == 0) { // 每50個數(shù)據(jù)一行 System.out.println(); // 換行 } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { ps.close(); } } @Test public void test2() throws IOException{ DataOutputStream dos = new DataOutputStream(new FileOutputStream("alex2")); dos.writeUTF("alex"); dos.writeInt(1); dos.writeBoolean(true); dos.flush(); dos.close(); } /* 將文件中存儲的基本數(shù)據(jù)類型變量和字符串讀取到內(nèi)存中,保存在變量中。 注意點:讀取不同類型的數(shù)據(jù)的順序要與當(dāng)初寫入文件時,保存的數(shù)據(jù)的順序一致! */ @Test public void test3() throws IOException{ DataInputStream diss = new DataInputStream(new FileInputStream("alex2")); String s = diss.readUTF(); int i = diss.readInt(); boolean b = diss.readBoolean(); System.out.println(s); System.out.println(i); System.out.println(b); } }
四? 處理流之 對象流
對象流的使用
1.ObjectInputStream 和 ObjectOutputStream
2.作用:用于存儲和讀取基本數(shù)據(jù)類型數(shù)據(jù)或?qū)ο蟮奶幚砹鳌K膹姶笾幘褪强梢园袹ava中的對象寫入到數(shù)據(jù)源中,也能把對象從數(shù)據(jù)源中還原回來
3.要想一個java對象是可序列化的,需要滿足相應(yīng)的要求。見Person.java
4.序列化機制:
對象序列化機制允許把內(nèi)存中的Java對象轉(zhuǎn)換成平臺無關(guān)的二進制流,
從而允許把這種二進制流持久地保存在磁盤上,或通過網(wǎng)絡(luò)將這種二進制流傳輸?shù)搅硪粋€網(wǎng)絡(luò)節(jié)點
當(dāng)其它程序獲取了這種二進制流,就可以恢復(fù)成原來的Java對象
public class ObjectInputOutputStreamTest { /* 序列化過程:將內(nèi)存中的java對象保存到磁盤中或通過網(wǎng)絡(luò)傳輸出去 使用ObjectOutputStream實現(xiàn) */ @Test public void testObjectOutputStream(){ ObjectOutputStream oos = null; try { //1. oos = new ObjectOutputStream(new FileOutputStream("object.dat")); //2. oos.writeObject(new String("我愛北京天安門")); oos.flush();//刷新操作 oos.writeObject(new Person("王銘",23)); oos.flush(); oos.writeObject(new Person("張學(xué)良",23,1001,new Account(5000))); oos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if(oos != null){ //3. try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 反序列化:將磁盤文件中的對象還原為內(nèi)存中的一個java對象 使用ObjectInputStream來實現(xiàn) */ @Test public void testObjectInputStream(){ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("object.dat")); Object obj = ois.readObject(); String str = (String) obj; Person p = (Person) ois.readObject(); Person p1 = (Person) ois.readObject(); System.out.println(str); System.out.println(p); System.out.println(p1); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if(ois != null){ try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
可序列化
1.需要實現(xiàn)接口:Serializable
2.當(dāng)前類提供一個全局常量:serialVersionUID
3.除了當(dāng)前Person類需要實現(xiàn)Serializable接口之外,還必須保證其內(nèi)部所有屬性
也必須是可序列化的。(默認(rèn)情況下,基本數(shù)據(jù)類型可序列化)
補充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修飾的成員變量
public class Person implements Serializable{ public static final long serialVersionUID = 475463534532L; private String name; private Account acct;} class Account implements Serializable{ public static final long serialVersionUID = 4754534532L; private double balance;}
五 特殊流(任意流) RandomAccessFile DataInputStream ?DataOutputStream
RandomAccessFile的使用(任意)
1.RandomAccessFile直接繼承于java.lang.Object類,實現(xiàn)了DataInput和DataOutput接口
2.RandomAccessFile既可以作為一個輸入流,又可以作為一個輸出流
3.如果RandomAccessFile作為輸出流時,寫出到的文件如果不存在,則在執(zhí)行過程中自動創(chuàng)建。
如果寫出到的文件存在,則會對原有文件內(nèi)容進行覆蓋。(默認(rèn)情況下,從頭覆蓋)
4.可以通過相關(guān)的操作,實現(xiàn)RandomAccessFile“插入”數(shù)據(jù)的效果
public class RandomAccessFileTest { @Test public void test1() throws IOException { RandomAccessFile rfa1 = new RandomAccessFile(new File("xxx"), "r"); RandomAccessFile rfa2 = new RandomAccessFile(new File("xxx"), "rw"); int len; byte[] bytes = new byte[1024]; while ((len= rfa1.read(bytes)) != -1){ rfa2.write(bytes,0,len); } rfa1.close(); rfa2.close(); } @Test public void test2() throws IOException { RandomAccessFile rfarw = new RandomAccessFile(new File("hello.txt"), "rw"); int len; byte[] bytes = new byte[20]; StringBuilder stringBuilder = new StringBuilder((int) new File("hello.txt").length()); rfarw.seek(3); //將指針調(diào)到角標(biāo)為3的位置 //保存指針3后面的所有數(shù)據(jù)到StringBuilder中 while ((len= rfarw.read(bytes)) != -1){ stringBuilder.append(new String(bytes,0,len)); } //調(diào)回指針,寫入"abc" rfarw.seek(3); rfarw.write("abc".getBytes()); //將StringBuilder中的數(shù)據(jù)寫入到文件中 rfarw.write(stringBuilder.toString().getBytes()); // 此處是順序?qū)懭?rfarw.close(); } @Test public void test3() throws IOException { RandomAccessFile rfarw = new RandomAccessFile(new File("hello2.txt"), "rw"); rfarw.write("abc".getBytes()); rfarw.write("ddd".getBytes()); // 順序?qū)懭?} }
Java
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。