国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

IO流知識(shí)小結(jié)(一)

2019-11-14 10:41:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

輸出流 這里寫圖片描述

輸入流 這里寫圖片描述

File類 這里寫圖片描述

File類學(xué)習(xí)代碼package com.imooc.file;import java.io.File;import java.io.IOException;public class FileDemo { public static void main(String[] args) { //創(chuàng)建File對(duì)象的三種構(gòu)造方法,由于反斜杠代表轉(zhuǎn)義字符,因此兩個(gè)反斜杠才能代表一個(gè)真正的反斜杠的作用 /* * File(String pathname) * ——通過(guò)將給定的路徑名??字符串轉(zhuǎn)換為抽象路徑名來(lái)創(chuàng)建新的文件實(shí)例。 * File(String parent, String child) * ——從父抽象路徑名和子路徑名字符串創(chuàng)建新的文件實(shí)例。 * 注意File類對(duì)象指向的路徑不需要一定存在 */ File file1=new File("G://imooc//io//score.txt");//方法一 File file2=new File("G://imooc","io//score.txt");//方法二 //方法3 /* * public File(File parent,String child) */ File file3=new File("G://imooc"); File file4=new File(file3,"io//score.txt"); //判斷是文件還是目錄 /* * public boolean isDirectory() * ——測(cè)試此抽象路徑名所表示的文件是否為目錄。 * public boolean isFile() * ——測(cè)試此抽象路徑名所表示的文件是否為文件。 */ System.out.字節(jié)輸入輸出流 一、字節(jié)流 這里寫圖片描述 1、字節(jié)輸入流類及其子類 之后的代碼重點(diǎn)學(xué)習(xí)文件輸入流和緩沖輸入流,字符輸入流等其他流可以對(duì)比著學(xué)習(xí),基本上是大同小異。 這里寫圖片描述 (1)文件輸入流及其常用方法 這里寫圖片描述 這里寫圖片描述

字節(jié)輸入流之文件輸入流學(xué)習(xí)代碼一package com.imooc.input_output_stream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputStreamDemo1 { public static void main(String[] args) { //創(chuàng)建一個(gè)FileInputStream對(duì)象 /* * 兩種常用構(gòu)造方法: * public FileInputStream(File file) throws FileNotFoundException * public FileInputStream(String name) throws FileNotFoundException */ try { //由于imooc.txt是存放在這個(gè)工程目錄下的,是相對(duì)路徑,因此直接寫文件名即可 FileInputStream fis=new FileInputStream("imooc.txt"); /* * public int read() throws IOException * ——從此文件輸入流讀取"一個(gè)"字節(jié),返回讀取的字節(jié)數(shù)據(jù). * 注意,讀取一個(gè)字節(jié)后就不會(huì)再讀取這個(gè)字節(jié)了,相當(dāng)于水管中取水滴 * 如果第二遍執(zhí)行read方法那么讀取的就是下一個(gè)字節(jié)了 */ int n; while((n=(fis.read()))!=-1)//讀取的字節(jié)為-1時(shí),說(shuō)明讀到了文件的末尾 { System.out.print((char) n);//將讀到的字節(jié)轉(zhuǎn)換為對(duì)應(yīng)的字符 } /* * public void close() throws IOException * ——關(guān)閉此文件輸入流,并釋放與流相關(guān)聯(lián)的任何系統(tǒng)資源。 * 要養(yǎng)成隨手關(guān)閉輸入流的習(xí)慣 */ fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) //由于FileNotFoundException是IOException的子類,因此IOException要放后面 { e.printStackTrace(); } }}字節(jié)輸入流之文件輸入流學(xué)習(xí)代碼二package com.imooc.input_output_stream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputStreamDemo2 { public static void main(String[] args) { try { FileInputStream fis=new FileInputStream("imooc.txt"); /* * public int read(byte[] b) throws IOException * ————從該輸入流中最多讀取b.length個(gè)字節(jié)的數(shù)據(jù)并存放在字節(jié)數(shù)組b中,返回實(shí)際讀取的字節(jié)數(shù) * public int read(byte[] b,int off,int len) throws IOException * ————從輸入流中最多讀取len個(gè)字節(jié)的數(shù)據(jù),并將其存放在字節(jié)數(shù)組b中,但放入數(shù)組b中時(shí),從index為off的位置開始放 * 并返回實(shí)際讀取的字節(jié)數(shù) */ byte[] b=new byte[100]; fis.read(b);//等價(jià)于fis.read(b,0,12) //將字節(jié)數(shù)組轉(zhuǎn)換為字符串輸出,利用之前學(xué)過(guò)的String類構(gòu)造方法public String(byte[] bytes) System.out.println(new String(b)); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}

2、字節(jié)輸出流及其子類 這里寫圖片描述 (1)、文件輸出流及其常用方法 這里寫圖片描述

字節(jié)輸出流之文件輸出流學(xué)習(xí)代碼一package com.imooc.input_output_stream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputStreamDemo1 { public static void main(String[] args) { try { /* * public FileOutputStream(String name) throws FileNotFoundException * ————用這種構(gòu)造方法創(chuàng)建的輸出流輸出字節(jié)時(shí)會(huì)覆蓋要寫入文件原有的數(shù)據(jù) * public FileOutputStream(File file,boolean append) throws FileNotFoundException * ————用這種構(gòu)造方法創(chuàng)建的輸出流輸出輸出字節(jié)時(shí),若append為true則在要寫入文件最后寫入新輸出的數(shù)據(jù),否則覆蓋 */ FileOutputStream fos=new FileOutputStream("imooc.txt"); /* * public void write(int b) throws IOException * ————將指定的字符輸出到輸出流中,其中b既可以代表字節(jié),也可以代表字符 */ fos.write(50); fos.write('a');//字符是可以和整型之間相互轉(zhuǎn)換的 fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}字節(jié)輸出流之文件輸出流學(xué)習(xí)代碼二package com.imooc.input_output_stream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputStreamDemo2 { public static void main(String[] args) { try { /* * public FileOutputStream(String name) throws FileNotFoundException * ————用這種構(gòu)造方法創(chuàng)建的輸出流輸出字節(jié)時(shí)會(huì)覆蓋要寫入文件原有的數(shù)據(jù) * public FileOutputStream(File file,boolean append) throws FileNotFoundException * ————用這種構(gòu)造方法創(chuàng)建的輸出流輸出輸出字節(jié)時(shí),若append為true則在要寫入文件最后寫入新輸出的數(shù)據(jù),否則覆蓋 */ FileOutputStream fos=new FileOutputStream("imooc.txt",true); /* * public void write(int b) throws IOException * ————將指定的字符輸出到輸出流中,其中b既可以代表字節(jié),也可以代表字符 */ fos.write(50); fos.write('a');//字符是可以和整型之間相互轉(zhuǎn)換的 fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}字節(jié)輸出流之文件輸出流學(xué)習(xí)代碼三package com.imooc.input_output_stream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputStreamDemo3 { public static void main(String[] args) { //文件拷貝,實(shí)際上就是字節(jié)數(shù)據(jù)經(jīng)輸入流輸入再經(jīng)輸出流輸出到另一個(gè)文件的過(guò)程 try { //創(chuàng)建讀取圖片數(shù)據(jù)的輸入流 FileInputStream fis=new FileInputStream("happy.png"); //創(chuàng)建輸出圖片數(shù)據(jù)的輸出流 FileOutputStream fos=new FileOutputStream("happycopy.png"); int n; byte[] b=new byte[2048]; /* * public void write(byte[] b,int off,int len) throws IOException * ————向輸出流中輸出字節(jié)數(shù)組b中off位置開始的長(zhǎng)度為len的字節(jié)數(shù)據(jù) */ while((n=fis.read(b))!=-1) { fos.write(b,0,n);//用這種多參數(shù)的write方法可以保證不會(huì)寫入多余的數(shù)據(jù) } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}

3、緩沖流 這里寫圖片描述

緩沖流學(xué)習(xí)代碼package com.imooc.input_output_stream;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class BufferDemo { public static void main(String[] args) { try { FileOutputStream fos=new FileOutputStream("imooc.txt"); //創(chuàng)建緩沖輸出流,將文件輸出流和緩沖輸出流連接 /* * public BufferedOutputStream(OutputStream out) * ————?jiǎng)?chuàng)建新的緩沖輸出流,以將數(shù)據(jù)寫入指定的基礎(chǔ)輸出流。 */ BufferedOutputStream bos=new BufferedOutputStream(fos); FileInputStream fis=new FileInputStream("imooc.txt"); //將文件輸入流和緩沖輸入流連接 /* * public BufferedInputStream(InputStream in) * ————?jiǎng)?chuàng)建緩沖輸入流并保存其參數(shù)————基礎(chǔ)輸入流,供以后使用。 */ BufferedInputStream bis=new BufferedInputStream(fis); /* * public void write(int b) throws IOException * ————將指定的字節(jié)寫入此緩沖輸出流,但實(shí)際是存儲(chǔ)在緩沖輸出流的內(nèi)部緩沖區(qū)buf中,只有緩沖區(qū)滿了 * 才會(huì)自動(dòng)執(zhí)行寫操作,因此需要手動(dòng)強(qiáng)制清空內(nèi)部緩存區(qū)執(zhí)行寫操作 * public void flush() throws IOException * ————刷新此緩沖輸出流,這將強(qiáng)制任何緩沖的輸出字節(jié)被寫出到基礎(chǔ)輸出流。只有具有緩沖區(qū)的輸出流才需要使用此方法強(qiáng)制執(zhí)行寫數(shù)據(jù)的操作,但建議在每一個(gè)write語(yǔ)句后都加一句flush以防萬(wàn)一 */ bos.write(50); bos.write('a'); bos.flush(); bos.close();//釋放資源 /* * 緩沖輸入流的read方法與文件輸入流的read方法基本一致 */ System.out.println(bis.read()); System.out.println((char) bis.read()); bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}

二、字符流

這里寫圖片描述 字符輸入流和輸出流 這里寫圖片描述 這里寫圖片描述 由于字符流和字節(jié)流方法基本一致,只有處理數(shù)據(jù)的不同,因此不單獨(dú)寫學(xué)習(xí)代碼了,直接拿出字節(jié)字符轉(zhuǎn)換流的代碼。字節(jié)流轉(zhuǎn)換成字符流。

package com.imooc.reader_writer;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class ChangeStreamDemo { public static void main(String[] args) { try { FileInputStream fis=new FileInputStream("imooc.txt"); /* * 將字節(jié)輸入流轉(zhuǎn)換為字符輸入流 * public InputStreamReader(InputStream in) */ InputStreamReader isr=new InputStreamReader(fis); int n; char[] cbuf=new char[10]; /* * public int read() throws IOException * ————從輸入流讀取單個(gè)字符,返回讀取的字符數(shù)據(jù)(轉(zhuǎn)換為int類型) * public int read(char[] cbuf) throws IOException * ————從輸入流讀取最多cbuf.length個(gè)字符的數(shù)據(jù),存放在字符數(shù)組cbuf中,返回實(shí)際讀取的字符數(shù) * public String(char[] value,int offset,int count) * ————從字符數(shù)組value的offset位置開始,創(chuàng)建一個(gè)長(zhǎng)度為count的字符串 */ /* * 以下注釋代碼為讀取的步驟,由于這個(gè)步驟會(huì)導(dǎo)致下面的輸出流的例子出錯(cuò),因此注釋掉 while((n=isr.read(cbuf))!=-1) { System.out.print(new String(cbuf,0,n));//保證最后一次在未將數(shù)組cbuf存滿的時(shí)候也能正確輸出 } */ //字節(jié)輸出流轉(zhuǎn)字符輸出流 FileOutputStream fos=new FileOutputStream("imooc1.txt"); /* * public OutputStreamWriter(OutputStream out) * ————將基礎(chǔ)字節(jié)輸出流轉(zhuǎn)換為字符輸出流 * public void write(char[] cbuf,int off,int len) throws IOException * ————將cbuf字符數(shù)組中第off個(gè)位置開始長(zhǎng)度為len的字符串輸出到輸出流中 */ OutputStreamWriter osw=new OutputStreamWriter(fos); while((n=isr.read(cbuf))!=-1) { osw.write(cbuf,0,n); osw.flush();/*所有含有緩沖區(qū)的輸出流的write方法都是在輸出流的未滿時(shí)不自動(dòng)執(zhí)行write操作的, 因此需要手動(dòng)清空強(qiáng)制執(zhí)行write操作才能將輸出流中的數(shù)據(jù)寫入文件,之前學(xué)習(xí)的字節(jié)輸出流中沒(méi)有flush是因?yàn)? 后面加了close();但是實(shí)際中最好兩個(gè)都加上 */ } fis.close(); fos.close(); isr.close(); osw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}

三、對(duì)象序列化及反序列化 步驟 這里寫圖片描述 用到的類 這里寫圖片描述

對(duì)象序列化學(xué)習(xí)代碼之Goods商品類package com.imooc.serial;import java.io.Serializable;/*創(chuàng)建一個(gè)實(shí)現(xiàn)序列化接口的類*/public class Goods implements Serializable{ private String goodsId; private String goodsName; private double price; public Goods() { } public Goods(String goodsId, String goodsName, double price) { this.goodsId = goodsId; this.goodsName = goodsName; this.price = price; } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName + ", price=" + price + "]"; }}對(duì)象序列化學(xué)習(xí)代碼之商品測(cè)試類package com.imooc.serial;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class GoodsTest { public static void main(String[] args) { //定義Goods類對(duì)象,將對(duì)象寫入文件中,再讀出來(lái)判斷是否寫入成功 Goods goods1=new Goods("gd001","電腦",3000); try { /* * public ObjectOutputStream(OutputStream out) throws IOException * ————?jiǎng)?chuàng)建一個(gè)對(duì)象輸出流以寫入指定的輸出流 * public final void writeObject(Object obj) throws IOException * ————將一個(gè)指定的對(duì)象寫入對(duì)象輸出流 */ //將對(duì)象信息寫入文件 FileOutputStream fos=new FileOutputStream("imooc.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(goods1);//將goods1對(duì)象寫入文件imooc.txt中 oos.flush(); oos.close(); fos.close(); /* * public final Object readObject() throws IOException,ClassNotFoundException * ————從對(duì)象輸入流讀出一個(gè)對(duì)象 */ //將對(duì)象信息從文件讀出來(lái) FileInputStream fis=new FileInputStream("imooc.txt"); ObjectInputStream ois=new ObjectInputStream(fis); Goods goods2=(Goods)ois.readObject();//由于readObject方法返回的是Object類,因此要強(qiáng)制轉(zhuǎn)換 System.out.println(goods2); fis.close(); ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }}

這里寫圖片描述


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凭祥市| 成安县| 昭通市| 九寨沟县| 昂仁县| 安乡县| 新沂市| 建宁县| 丽水市| 房产| 蚌埠市| 云霄县| 原平市| 德兴市| 通榆县| 通许县| 奉贤区| 得荣县| 睢宁县| 东乡县| 黄龙县| 宁海县| 三亚市| 惠安县| 天台县| 延吉市| 赞皇县| 镶黄旗| 崇明县| 和田县| 延长县| 漳浦县| 车致| 泸水县| 东方市| 玉树县| 玉龙| 阜南县| 大庆市| 长丰县| 慈溪市|