轉載請註明出處,謝謝!html
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <p align="center"><font color="#FF3030" size="5">寫在前面</font></p> <p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>java
InputStream
OutputStream
Reader
Writer
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>windows
<p align="center"><font color="#FF3030" size="5">目 錄</font></p>設計模式
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <br/>數組
<p align="center"><font face="仿宋" color="#004D88" size="4">1.read()一次讀取一個字節</font></p>dom
<p align="center"><font face="仿宋" color="#004D88" size="4">2.read()方法返回值爲何是int</font></p>ide
<p align="center"><font face="仿宋" color="#004D88" size="4">3.定義小數組實現緩衝</font></p>優化
<p align="center"><font face="仿宋" color="#004D88" size="4">5.flush方法和close方法</font></p>this
<p align="center"><font face="仿宋" color="#004D88" size="4">6. 字節流讀寫中文</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">7.流的標準處理異常代碼1.6版本及其之前</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">8.流的標準處理異常代碼1.7版本以後</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">9.拷貝文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">10.錄入數據拷貝到文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">11.字符流 FileReader</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">12.字符流 FileWriter</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">13.字符流的拷貝</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">14.什麼狀況下使用字符流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">15.字符流是否能夠拷貝非純文本的文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">16.自定義字符數組的拷貝</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">17.帶緩衝的字符流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">18.readLine()和newLine()方法</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">19.LineNumberReader</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">20.裝飾設計模式</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">21.使用指定的碼錶讀寫字符</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">22.序列流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">23.序列流整合多個</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">24.內存輸出流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">26.對象操做流ObjecOutputStream</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">27.對象操做流ObjectInputStream</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">28.對象操做流優化</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">29.id號</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">30.打印流的概述和特色</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">31.標準輸入輸出流概述和輸出語句</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">32.修改標準輸入輸出流拷貝圖片</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">33.兩種方式實現鍵盤錄入</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">34.隨機訪問流概述和讀寫數據</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">35.數據輸入輸出流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">36.Properties的概述和做爲Map集合的使用</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">37.獲取Properties中的每個鍵</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">38.Properties的load()和store()功能</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">39.ZIP文檔</font></p>
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>
<br/>
<br/>
<br/>
<span id = "1"></span>
FileInputStream fis = new FileInputStream("烏合之衆.txt"); //建立一個文件輸入流對象,並關聯烏合之衆.txt int b; //定義變量,記錄每次讀到的字節 while((b = fis.read()) != -1) { //將每次讀到的字節賦值給b並判斷是不是-1 System.out.println(b); //打印每個字節 } fis.close(); //關閉流釋放資源
<br/>
<span id = "2"></span>
read()
方法讀取的是一個字節,爲何返回是int
,而不是byte
byte
,有可能在讀到中間的時候遇到11111111
,那麼這11111111
是byte
類型的-1
,程序是遇到-1就會中止
,後面的數據就讀不到了。因此在讀取的時候用int
類型接收,會在其前面補上24個0湊足4個字節,那麼byte
類型的-1
就變成int
類型的255
了這樣能夠保證整個數據讀完,而結束標記的-1
就是int
類型。<br/> <span id = "3"></span>
write(byte[] b)
write(byte[] b, int off, int len)
寫出有效的字節個數
定義小數組的標準格式
FileInputStream fis = new FileInputStream("李志 - 梵高先生.flac"); FileOutputStream fos = new FileOutputStream("梵高先生.flac"); int len; byte arr[] = new byte[8*1024]; //自定義字節數組 while((len=fis.read(arr))!=-1){ fos.write(arr, 0, len); //寫出字節數組寫出有效個字節個數 } fis.close(); fos.close();
<br/> <span id = "4"></span>
BufferedInputStream
BufferedInputStream
內置了一個緩衝區(數組)BufferedInputStream
中讀取一個字節時,BufferedInputStream
會一次性從文件中讀取8192
個, 存在緩衝區中, 而後返回給程序一個字符。8192
個BufferedOutputStream
BufferedOutputStream
也內置了一個緩衝區(數組)BufferedOutputStream
纔會把緩衝區中的數據一次性寫到文件裏。組合流過濾器實現拷貝
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("李志 - 梵高先生.flac")); //建立緩衝區對FileInputStream對象的裝飾 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2004-梵高先生.flac")); //建立緩衝區對FileOutputStream對象的裝飾 int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close();//只關裝飾後的對象便可 bos.close();
Buffered
的讀取哪一個更快?
8192
個字節大小和Buffered
比較的話,定義小數組會略勝一籌,由於讀和寫操做的是同一個數組,而Buffered
操做的是兩個數組
.<br/> <span id = "5"></span>
flush()
方法
close()
方法
close()
方法,不但會關閉流,還會再關閉流以前刷新緩衝區,關閉後不能再寫出<br/> <span id = "6"></span>
<br/> <span id = "7"></span> ## ~~<font color="#FF6600" size="4">**流的標準處理異常代碼1.6版本及其之前**</font>~~
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("from.txt"); fos = new FileOutputStream("to.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); } } finally { try { if(fis != null) fis.close(); }finally { if(fos != null) fos.close(); } }
<br/> <span id = "8"></span>
try( FileInputStream fis = new FileInputStream("from.txt"); FileOutputStream fos = new FileOutputStream("to.txt"); ){ int b; while((b = fis.read()) != -1) { fos.write(b); } }
{. . .}
執行後就會自動調用流對象的close方法將流關掉.<br/> <span id = "9"></span>
Scanner sc = new Scanner(System.in); System.out.println("請輸入一個文件路徑"); String line = sc.nextLine(); //將鍵盤錄入的文件路徑存儲在line中 File file = new File(line); //封裝成File對象 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getName()); int len; byte[] arr = new byte[8192]; //定義緩衝區 while((len = fis.read(arr)) != -1) { fos.write(arr,0,len); } fis.close(); fos.close();
<br/> <span id = "10"></span> ## <font color="#FF6600" size="4">**錄入數據拷貝到文件**</font>
Scanner sc = new Scanner(System.in); FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("請輸入:"); while(true) { String line = sc.nextLine(); if("quit".equals(line)) break; fos.write(line.getBytes()); fos.write("\r\n".getBytes()); } fos.close();
<br/> <span id = "11"></span> ## <font color="#FF6600" size="4">**字符流 FileReader**</font>
字符流是什麼
字符
, 就要先讀取到字節
數據, 而後轉爲字符
. 若是要寫出字符, 須要把字符轉爲字節再寫出.FileReader
FileReader fr = new FileReader("from.txt"); //建立輸入流對象,關聯from.txt int ch; while((ch = fr.read()) != -1) { //將讀到的字符賦值給ch System.out.println((char)ch); //將讀到的字符強轉後打印 } fr.close(); //關流
<br/> <span id = "12"></span> ## <font color="#FF6600" size="4">**字符流 FileWriter**</font>
字符
轉爲字節
寫出FileWriter fw = new FileWriter("to.txt"); fw.write("write"); fw.close();
<br/> <span id = "13"></span> ## <font color="#FF6600" size="4">**字符流的拷貝**</font>
FileReader fr = new FileReader("from.txt"); FileWriter fw = new FileWriter("to.txt"); int ch; while((ch = fr.read()) != -1) { fw.write(ch); } fr.close(); fw.close();
<br/> <span id = "14"></span>
<span id = "15"></span>
"?"
代替,寫出的時候會將"?"
字符轉換成字節寫出去。如此這般,寫出以後的文件就錯亂了。<br/> <span id = "16"></span>
FileReader fr = new FileReader("form.txt"); //建立字符輸入流,關聯aaa.txt FileWriter fw = new FileWriter("to.txt"); //建立字符輸出流,關聯bbb.txt int len; char[] arr = new char[1024*8]; //建立字符數組 while((len = fr.read(arr)) != -1) { //將數據讀到字符數組中 fw.write(arr, 0, len); //從字符數組將數據寫到文件上 } fr.close(); //關流釋放資源 fw.close();
<br/> ## <span id = "17"></span> <font color="#FF6600" size="4">**帶緩衝的字符流**</font>
BufferedReader
的read()
方法讀取字符時會一次讀取若干字符到緩衝區, 而後逐個返回給程序, 減小讀取次數, 以期提升效率。BufferedWriter
的write()
方法寫出字符時會先寫到緩衝區, 緩衝區寫滿時纔會寫到文件, 減小寫入次數, 以期提升效率。BufferedReader br = new BufferedReader(new FileReader("form.txt")); //建立字符輸入流對象,關聯aaa.txt BufferedWriter bw = new BufferedWriter(new FileWriter("to.txt")); //建立字符輸出流對象,關聯bbb.txt int ch; while((ch = br.read()) != -1) { //read一次,會先將緩衝區讀滿,從緩衝去中一個一個的返給臨時變量ch bw.write(ch); //write一次,是將數據裝到字符數組,裝滿後再一塊兒寫出去 } br.close(); //關流 bw.close();
<br/> <span id = "18"></span>
BufferedReader
的readLine()
方法能夠讀取一行字符(不包含換行符號)BufferedWriter
的newLine()
能夠輸出一個跨平臺的換行符號"\r\n"BufferedReader br = new BufferedReader(new FileReader("aaa.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt")); String line; while((line = br.readLine()) != null) { bw.write(line); //bw.write(""); //只支持windows系統 bw.newLine(); //跨平臺的 } br.close(); bw.close();
<br/> <span id = "19"></span>
LineNumberReader
是BufferedReader
的子類, 具備相同的功能, 而且能夠統計行號
getLineNumber()
方法能夠獲取當前行號setLineNumber()
方法能夠設置當前行號LineNumberReader lnr = new LineNumberReader(new FileReader("form.txt")); String line; lnr.setLineNumber(100); //設置行號 while((line = lnr.readLine()) != null) { System.out.println(lnr.getLineNumber() + ":" + line);//獲取行號 } lnr.close();
<br/> <span id = "20"></span>
interface Coder { public void code(); }
class Persion implements Coder { @Override public void code() { System.out.println("It's none of my business during the daytime"); System.out.println("Write java at night"); } }
class XPersion implements Coder { private Persion s; //被包裝的類的引用 public XPersion (Persion s) { //構造方法將被包裝的對象做爲參數傳入 this.s = s; } @Override public void code() { //對其原有功能進行升級 s.code(); System.out.println("Get cervical spondum buff"); System.out.println("......"); System.out.println("sudden death"); System.out.println("......"); } }
<br/> <span id = "21"></span>
InputStreamReader(字節流,編碼表)
OutputStreamWriter(字節流,編碼表)
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("UTF-8.txt"), "UTF-8")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("GBK.txt"), "GBK")); int ch; while((ch = br.read()) != -1) { bw.write(ch); } br.close(); bw.close();
<br/> <span id = "22"></span>
SequenceInputStream(InputStream, InputStream)
FileInputStream fis1 = new FileInputStream("a.txt"); //建立輸入流對象,關聯a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立輸入流對象,關聯b.txt SequenceInputStream sis = new SequenceInputStream(fis1, fis2); //將兩個流整合成一個流 FileOutputStream fos = new FileOutputStream("c.txt"); //建立輸出流對象,關聯c.txt int b; while((b = sis.read()) != -1) { //用整合後的輸入流 fos.write(b); //寫到指定文件上 } sis.close(); fos.close();
<br/> <span id = "23"></span>
SequenceInputStream(Enumeration)
FileInputStream fis1 = new FileInputStream("a.txt"); //建立輸入流對象,關聯a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立輸入流對象,關聯b.txt FileInputStream fis3 = new FileInputStream("c.txt"); //建立輸入流對象,關聯c.txt Vector<InputStream> v = new Vector<>(); //建立vector集合對象 v.add(fis1); //將流對象添加 v.add(fis2); v.add(fis3); Enumeration<InputStream> en = v.elements(); //獲取枚舉引用 SequenceInputStream sis = new SequenceInputStream(en); //en傳遞給SequenceInputStream的構造方法 FileOutputStream fos = new FileOutputStream("d.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); fos.close();
<br/> <span id = "24"></span>
new ByteArrayOutputStream()
write(int), write(byte[])
toByteArray()
FileInputStream fis = new FileInputStream("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while((b = fis.read()) != -1) { baos.write(b); } //byte[] newArr = baos.toByteArray(); //將內存緩衝區中全部的字節存儲在newArr中 //System.out.println(new String(newArr)); System.out.println(baos); fis.close();
<br/> <span id = "25"></span>
FileInputStream fis = new FileInputStream("a.txt"); //建立字節輸入流,關聯a.txt ByteArrayOutputStream baos = new ByteArrayOutputStream(); //建立內存輸出流 byte[] arr = new byte[5]; //建立字節數組,大小爲5 int len; while((len = fis.read(arr)) != -1) { //將文件上的數據讀到字節數組中 baos.write(arr, 0, len); //將字節數組的數據寫到內存緩衝區中 } System.out.println(baos); //將內存緩衝區的內容轉換爲字符串打印 fis.close();
<br/> <span id = "26"></span>
new ObjectOutputStream(OutputStream)
,writeObject()
public class ObjectOutputStream { /** * @param args * @throws IOException * 將對象寫出,序列化 */ public static void main(String[] args) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//建立對象輸出流 oos.writeObject(p1); oos.writeObject(p2); oos.close(); } }
<br/> <span id = "27"></span>
new ObjectInputStream(InputStream)
, readObject()
public class ObjectInputStream { /** * @param args * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException * 讀取對象,反序列化 */ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); } }
<br/> <span id = "28"></span>
Person p1 = new Person("Tom", 20); Person p2 = new Person("Jerry", 22); Person p3 = new Person("Jack", 10); Person p4 = new Person("Herry", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt")); oos.writeObject(list); //寫出集合對象 oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在運行期會被擦除,索引運行期至關於沒有泛型 //想去掉黃色能夠加註解@SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); } ois.close();
<br/> <span id = "29"></span>
<br/> <span id = "30"></span>
toString()
結果輸出, 而且自動加上換行, 並且可使用自動刷出的模式System.out
就是一個PrintStream
, 其默認向控制檯輸出信息PrintStream ps = System.out; ps.println(97); //底層用的是Integer.toString(x),將x轉換爲數字字符串打印 ps.println("a string"); ps.println(new Person("Tom", 20)); Person p = null; ps.println(p); //若是是null,就返回null,若是不是null,就調用對象的toString()
print()
, println()
PrintWriter(OutputStream out, boolean autoFlush, String encoding)
PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true); //若是爲 true,則 println、printf 或 format 方法將刷新輸出緩衝區 pw.write(97); pw.print("Hello"); pw.println("你好"); pw.close();
<br/> <span id = "31"></span>
System.in
是InputStream
, 標準輸入流, 默承認以從鍵盤輸入讀取字節數據System.out
是PrintStream
, 標準輸出流, 默承認以向Console中輸出字符和字節數據System.setIn(InputStream)
System.setOut(PrintStream)
System.setIn(new FileInputStream("a.txt")); //修改標準輸入流 System.setOut(new PrintStream("b.txt")); //修改標準輸出流 InputStream in = System.in; //獲取標準輸入流 PrintStream ps = System.out; //獲取標準輸出流 int b; while((b = in.read()) != -1) { //從a.txt上讀取字節 ps.write(b); //將數據寫到b.txt上 } in.close(); ps.close();
<br/> <span id = "32"></span>
System.setIn(new FileInputStream("png.png")); //改變標準輸入流 System.setOut(new PrintStream("copy.png")); //改變標準輸出流 InputStream is = System.in; //獲取標準輸入流 PrintStream ps = System.out; //獲取標準輸出流 int len; byte[] arr = new byte[1024 * 8]; while((len = is.read(arr)) != -1) { ps.write(arr, 0, len); } is.close(); ps.close();
<br/> <span id = "33"></span>
BufferedReader
的readLine
方法。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner
<br/> <span id = "34"></span>
A:隨機訪問流概述
RandomAccessFile
類不屬於流,是Object
類的子類。但它融合了InputStream
和OutputStream
的功能。B:read()
,write()
,seek()
<br/> <span id = "35"></span>
DataInputStream
, DataOutputStream
能夠按照基本數據類型大小讀寫數據DataOutputStream(OutputStream)
, writeInt()
, writeLong()
DataInputStream(InputStream)
, readInt()
, readLong()
DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt")); dos.writeInt(998); dos.writeInt(1998); dos.writeInt(2998); dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt")); int x = dis.readInt(); int y = dis.readInt(); int z = dis.readInt(); System.out.println(x); System.out.println(y); System.out.println(z); dis.close();
<br/> <span id = "36"></span>
Properties prop = new Properties(); prop.put("abc", 123); System.out.println(prop);
<br/> <span id = "37"></span>
public Object setProperty(String key,String value)
public String getProperty(String key)
public Enumeration<String> stringPropertyNames()
Properties prop = new Properties(); prop.setProperty("name", "Tom"); prop.setProperty("tel", "18000000000"); //System.out.println(prop); Enumeration<String> en = (Enumeration<String>) prop.propertyNames(); while(en.hasMoreElements()) { String key = en.nextElement(); //獲取Properties中的每個鍵 String value = prop.getProperty(key); //根據鍵獲取值 System.out.println(key + "="+ value); }
<br/> <span id = "38"></span>
Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); //將文件上的鍵值對讀取到集合中 prop.setProperty("tel", "18912345678"); prop.store(new FileOutputStream("config.properties"), null); //第二個參數是對列表參數的描述,能夠給值,也能夠給null System.out.println(prop); Output: {tel=18912345678}
<br/>
<span id = "39"></span>
private static void zipdemo1() throws IOException { try ( ZipInputStream zin = new ZipInputStream(new FileInputStream("code.zip")); ByteArrayOutputStream baos = new ByteArrayOutputStream();//內存輸入流 ) { ZipEntry entry; int len; byte arr[] = new byte[1024]; while ((entry = zin.getNextEntry()) != null) { String name = entry.getName();//壓縮項目的文件名 System.out.println("文件"+name+"\n解壓後大小:"+entry.getSize()); while ((len = zin.read(arr)) != -1) {//讀取數據內容 baos.write(arr, 0, len);//內容寫入內存輸入流 } System.out.println("緩衝區大小:"+baos.size()); System.out.println(baos); baos.reset();//將此 byte 數組輸出流的 count 字段重置爲零,從而丟棄輸出流中目前已累積的全部輸出 zin.closeEntry(); } } } Output: 文件code.txt 解壓後大小:29 緩衝區大小:29 AW9C2-JN9T2-H5CBV-24QT7-G4YB8 文件code1.txt 解壓後大小:39 緩衝區大小:39 激活碼:W9WW6-JN9W2-M8CTX-24QR7-M4HB8
<span id = "zz"></span>
——@guoyangde http://www.cnblogs.com/LittleTreasureBox/p/8904016.html