01輸入和輸出
* A:輸入和輸出
* a: 參照物
* 究竟是輸入仍是輸出,都是以Java程序爲參照
* b: Output
* 把內存中的數據存儲到持久化設備上這個動做稱爲輸出(寫)Output操做
* 程序到文件稱爲輸出
* c: Input
* 把持久設備上的數據讀取到內存中的這個動做稱爲輸入(讀)Input操做
* 文件到程序稱爲輸入
* d: IO操做
* 把上面的這種輸入和輸出動做稱爲IO操做
02字節輸出流OutputStream
* A: 字節輸出流OutputStream
* a.概念
* IO流用來處理設備之間的數據傳輸
* Java對數據的操做是經過流的方式
* Java用於操做流的類都在IO包中
* 流按流向分爲兩種:輸入流,輸出流。
* 流按操做類型分爲兩種:
* 字節流 : 字節流能夠操做任何數據,由於在計算機中任何數據都是以字節的形式存儲的
* 字符流 : 字符流只能操做純字符數據,比較方便。
* b.IO流經常使用父類
* 字節流的抽象父類:
* InputStream
* OutputStream
* 字符流的抽象父類:
* Reader
* Writer
* c.IO程序書寫
* 使用前,導入IO包中的類
* 使用時,進行IO異常處理
* 使用後,釋放資源
* d: 方法介紹
* void close(): 關閉此輸出流並釋放與此流有關的全部系統資源。
* void write(byte[] b): 將 b.length 個字節從指定的 byte 數組寫入此輸出流
* void write(byte[] b, int off, int len) :將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此輸出流。
* abstract void write(int b) : 將指定的字節寫入此輸出流。
03字節輸出流FileOutputStream寫字節
* A: 字節輸出流FileOutputStream寫字節
* a: FileOutputStream
* 寫入數據文件,學習父類方法,使用子類對象
* b: FileOutputStream構造方法
* 做用:綁定輸出的輸出目的
* FileOutputStream(File file)
* 建立一個向指定 File 對象表示的文件中寫入數據的文件輸出流。
* FileOutputStream(File file, boolean append)
* 建立一個向指定 File 對象表示的文件中寫入數據的文件輸出流,以追加的方式寫入。
* FileOutputStream(String name)
* 建立一個向具備指定名稱的文件中寫入數據的輸出文件流。
* FileOutputStream(String name, boolean append)
* 建立一個向具備指定 name 的文件中寫入數據的輸出文件流,以追加的方式寫入。
* c: 流對象使用步驟
* 1. 建立流子類的對象,綁定數據目的
* 2. 調用流對象的方法write寫
* 3. close釋放資源
* d: 注意事項
* 流對象的構造方法,能夠建立文件,若是文件存在,直接覆蓋
* e: 案例代碼
/*
* FileOutputStream
* 寫入數據文件,學習父類方法,使用子類對象
*
* 子類中的構造方法: 做用:綁定輸出的輸出目的
* 參數:
* File 封裝文件
* String 字符串的文件名
*
* 流對象使用步驟
* 1. 建立流子類的對象,綁定數據目的
* 2. 調用流對象的方法write寫
* 3. close釋放資源
*
* 流對象的構造方法,能夠建立文件,若是文件存在,直接覆蓋
*/
public class FileOutputStreamDemo {
public static void main(String[] args)throws IOException {
FileOutputStream fos = new FileOutputStream("c:\\a.txt");
//流對象的方法write寫數據
//寫1個字節
fos.write(97);
//關閉資源
fos.close();
}
}
04字節輸出流FileOutputStream寫字節數組
* A: 字節輸出流FileOutputStream寫字節數組
* a: 方法介紹
* void write(byte[] b): 將 b.length 個字節從指定的 byte 數組寫入此輸出流
* void write(byte[] b, int off, int len) :將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此輸出流。
* b: 案例代碼
/*
* FileOutputStream
* 寫入數據文件,學習父類方法,使用子類對象
*
* 子類中的構造方法: 做用:綁定輸出的輸出目的
* 參數:
* File 封裝文件
* String 字符串的文件名
*
* 流對象使用步驟
* 1. 建立流子類的對象,綁定數據目的
* 2. 調用流對象的方法write寫
* 3. close釋放資源
*
* 流對象的構造方法,能夠建立文件,若是文件存在,直接覆蓋
*/
public class FileOutputStreamDemo {
public static void main(String[] args)throws IOException {
FileOutputStream fos = new FileOutputStream("c:\\a.txt");
//流對象的方法write寫數據
//寫字節數組
byte[] bytes = {65,66,67,68};
fos.write(bytes);
//寫字節數組的一部分,開始索引,寫幾個
fos.write(bytes, 1, 2);
//寫入字節數組的簡便方式
//寫字符串
fos.write("hello".getBytes());
//關閉資源
fos.close();
}
}
05文件的續寫和換行符號
* A: 文件的續寫和換行符號
* a: 文件的續寫
* FileOutputStream構造方法, 的第二個參數中,加入true
* b: 換行符號
* 在文件中,寫入換行,符號換行 \r\n
* \r\n 能夠寫在上一行的末尾, 也能夠寫在下一行的開頭
* c: 案例代碼
/*
* FileOutputStream 文件的續寫和換行問題
* 續寫: FileOutputStream構造方法, 的第二個參數中,加入true
* 在文件中,寫入換行,符號換行 \r\n
* \r\n 能夠寫在上一行的末尾, 也能夠寫在下一行的開頭
*/
public class FileOutputStreamDemo1 {
public static void main(String[] args)throws IOException {
File file = new File("c:\\b.txt");
FileOutputStream fos = new FileOutputStream(file,true);
fos.write("hello\r\n".getBytes());
fos.write("world".getBytes());
fos.close();
}
}
06IO中的異常處理
* A: IO中的異常處理
* a:IO流的異常處理
* try catch finally
* b: 細節
* 1. 保證流對象變量,做用域足夠
* 2. catch裏面,怎麼處理異常
* 輸出異常的信息,目的看到哪裏出現了問題
* 停下程序,重新嘗試
* 3. 若是流對象創建失敗了,須要關閉資源嗎
* new 對象的時候,失敗了,沒有佔用系統資源
* 釋放資源的時候,對流對象判斷null
* 變量不是null,對象創建成功,須要關閉資源
* c: 案例代碼
public class FileOutputStreamDemo3 {
public static void main(String[] args) {
//try 外面聲明變量,try 裏面創建對象
FileOutputStream fos = null;
try{
fos = new FileOutputStream("s:\\a.txt");
fos.write(100);
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件寫入失敗,重試");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("關閉資源失敗");
}
}
}
}
07字節輸入流InputStream
* A: 字節輸入流InputStream
* a: 方法介紹
* abstract int read() :
* 從輸入流中讀取數據的下一個字節。
* int read(byte[] b)
* 從輸入流中讀取必定數量的字節,並將其存儲在緩衝區數組 b 中。
* int read(byte[] b, int off, int len)
* 將輸入流中最多 len 個數據字節讀入 byte 數組。
* void close()
* 關閉此輸入流並釋放與該流關聯的全部系統資源。
* b: 案例代碼
/*
* 字節輸入流
* java.io.InputStream 全部字節輸入流的超類
* 做用: 讀取任意文件,每次只讀取1個字節
* 讀取的方法 read
* int read() 讀取1個字節
* int read(byte[] b) 讀取必定量的字節,存儲到數組中
*/
public class InputStreamDemo {
}
08字節輸入流FileInputStream讀取字節
* A: 字節輸入流FileInputStream讀取字節
* a: 方法介紹
* abstract int read() :
* 從輸入流中讀取數據的下一個字節,返回-1表示文件結束
* int read(byte[] b)
* 從輸入流中讀取必定數量的字節,並將其存儲在緩衝區數組 b 中。
* 讀入緩衝區的字節總數,若是由於已經到達文件末尾而沒有更多的數據,則返回 -1。
* int read(byte[] b, int off, int len)
* 將輸入流中最多 len 個數據字節讀入 byte 數組。
* void close()
* 關閉此輸入流並釋放與該流關聯的全部系統資源。
* b: 案例代碼
/*
* FileInputStream讀取文件
*
* 構造方法: 爲這個流對象綁定數據源
*
* 參數:
* File 類型對象
* String 對象
* 輸入流讀取文件的步驟
* 1. 建立字節輸入流的子類對象
* 2. 調用讀取方法read讀取
* 3. 關閉資源
*
* read()方法,
* read()執行一次,就會自動讀取下一個字節
* 返回值,返回的是讀取到的字節, 讀取到結尾返回-1
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("c:\\a.txt");
//讀取一個字節,調用方法read 返回int
//使用循環方式,讀取文件, 循環結束的條件 read()方法返回-1
int len = 0;//接受read方法的返回值
while( (len = fis.read()) != -1){
System.out.print((char)len);
}
//關閉資源
fis.close();
}
}
/*
* int i = fis.read();
System.out.println(i);
i = fis.read();
System.out.println(i);
i = fis.read();
System.out.println(i);
i = fis.read();
System.out.println(i);
*/
09字節輸入流FileInputStream讀取字節數組
* A: 字節輸入流FileInputStream讀取字節數組
* a: 方法介紹
* int read(byte[] b)
* 從輸入流中讀取必定數量的字節,並將其存儲在緩衝區數組 b 中。
* 讀入緩衝區的字節總數,若是由於已經到達文件末尾而沒有更多的數據,則返回 -1。
* int read(byte[] b, int off, int len)
* 將輸入流中最多 len 個數據字節讀入 byte 數組。
* b: 案例代碼
/*
* FileInputStream讀取文件
* 讀取方法 int read(byte[] b) 讀取字節數組
* 數組做用: 緩衝的做用, 提升效率
* read返回的int,表示什麼含義 讀取到多少個有效的字節數
*/
public class FileInputStreamDemo1 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c:\\a.txt");
// 建立字節數組
byte[] b = new byte[2];
int len = fis.read(b);
System.out.println(new String(b));// ab
System.out.println(len);// 2
len = fis.read(b);
System.out.println(new String(b));// cd
System.out.println(len);// 2
len = fis.read(b);
System.out.println(new String(b));// ed
System.out.println(len);// 1
len = fis.read(b);
System.out.println(new String(b));// ed
System.out.println(len);// -1
fis.close();
}
}
10字節輸入流FileInputStream讀取字節數組的實現原理
* A:字節輸入流FileInputStream讀取字節數組的實現原理
* a: 原理
* 參見day23_source文件夾中的"讀取數組的原理.jpg"
* b: 案例代碼
public class FileInputStreamDemo1 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c:\\a.txt");
//建立字節數組
byte[] b = new byte[1024];
int len = 0 ;
while( (len = fis.read(b)) !=-1){
System.out.print(new String(b,0,len));
}
fis.close();
}
}
11文件複製原理
* A: 文件複製原理
* a: 見day23_source/文件複製原理.jpg
12字節流複製文件讀取單個字節
* A: 字節流複製文件讀取單個字節
* a: 案例代碼
/*
* 將數據源 c:\\a.txt
* 複製到 d:\\a.txt 數據目的
* 字節輸入流,綁定數據源
* 字節輸出流,綁定數據目的
*
* 輸入,讀取1個字節
* 輸出,寫1個字節
*/
public class Copy {
public static void main(String[] args) {
//定義兩個流的對象變量
FileInputStream fis = null;
FileOutputStream fos = null;
try{
//創建兩個流的對象,綁定數據源和數據目的
fis = new FileInputStream("c:\\t.zip");
fos = new FileOutputStream("d:\\t.zip");
//字節輸入流,讀取1個字節,輸出流寫1個字節
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件複製失敗");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}
}
}
}
}
13字節流複製文件讀取字節數組
* A: 字節流複製文件讀取字節數組
* a: 案例代碼
/*
* 字節流複製文件
* 採用數組緩衝提升效率
* 字節數組
* FileInputStream 讀取字節數組
* FileOutputStream 寫字節數組
*/
public class Copy_1 {
public static void main(String[] args) {
long s = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("c:\\t.zip");
fos = new FileOutputStream("d:\\t.zip");
//定義字節數組,緩衝
byte[] bytes = new byte[1024*10];
//讀取數組,寫入數組
int len = 0 ;
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件複製失敗");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}
}
}
long e = System.currentTimeMillis();
System.out.println(e-s);
}
}
14編碼表
* A: 編碼表
* a: 定義:
* 生活中字符和計算機二進制的對應關係表,就是編碼表
* b: 分類
* 一、ascii: 一個字節中的7位就能夠表示。對應的字節都是正數。0-xxxxxxx
* 二、iso-8859-1:拉丁碼錶 latin,用了一個字節用的8位。1-xxxxxxx 負數。
* 三、GB2312:簡體中文碼錶。包含6000-7000中文和符號。用兩個字節表示。兩個字節第一個字節是負數,第二個字節多是正數
* GBK:目前最經常使用的中文碼錶,2萬的中文和符號。用兩個字節表示,其中的一部分文字,第一個字節開頭是1,第二字節開頭是0
* GB18030:最新的中文碼錶,目前尚未正式使用。
* 四、unicode:國際標準碼錶:不管是什麼文字,都用兩個字節存儲。
* Java中的char類型用的就是這個碼錶。char c = 'a';佔兩個字節。
* Java中的字符串是按照系統默認碼錶來解析的。簡體中文版 字符串默認的碼錶是GBK。
* 五、UTF-8:基於unicode,一個字節就能夠存儲數據,不要用兩個字節存儲,並且這個碼錶更加的標準化,在每個字節頭加入了編碼信息(後期到api中查找)。
* 六、能識別中文的碼錶:GBK、UTF-8;正由於識別中文碼錶不惟一,涉及到了編碼解碼問題。
* 對於咱們開發而言;常見的編碼 GBK UTF-8 ISO-8859-1
* 文字--->(數字) :編碼。 「abc」.getBytes() byte[]
* (數字)--->文字 : 解碼。 byte[] b={97,98,99} new String(b)
15字符輸出流寫文本FileWriter類
* A: 字符輸出流寫文本FileWriter類
* a: 方法介紹
* void write(int c)
* 寫入單個字符
* void write(String str)
* 寫入字符串
* void write(String str, int off, int len)
* 寫入字符串的某一部分
* void write(char[] cbuf)
* 寫入字符數組
* abstract void write(char[] cbuf, int off, int len)
* 寫入字符數組的某一部分
* b: 案例代碼
/*
* 字符輸出流
* java.io.Writer 全部字符輸出流的超類
* 寫文件,寫文本文件
*
* 寫的方法 write
* write(int c) 寫1個字符
* write(char[] c)寫字符數組
* write(char[] c,int,int)字符數組一部分,開始索引,寫幾個
* write(String s) 寫入字符串
*
* Writer類的子類對象 FileWriter
*
* 構造方法: 寫入的數據目的
* File 類型對象
* String 文件名
*
* 字符輸出流寫數據的時候,必需要運行一個功能,刷新功能
* flush()
*/
public class WriterDemo {
public static void main(String[] args) throws IOException{
FileWriter fw = new FileWriter("c:\\1.txt");
//寫1個字符
fw.write(100);
fw.flush();
//寫1個字符數組
char[] c = {'a','b','c','d','e'};
fw.write(c);
fw.flush();
//寫字符數組一部分
fw.write(c, 2, 2);
fw.flush();
//寫如字符串
fw.write("hello");
fw.flush();
fw.close();
}
}
16字符輸入流讀取文本FileReader類
* A: 字符輸入流讀取文本FileReader類
* a: 方法介紹
* int read()
* 讀取單個字符
* int read(char[] cbuf)
* 將字符讀入數組
* abstract int read(char[] cbuf, int off, int len)
* 將字符讀入數組的某一部分。
* b: 案例代碼
/*
* 字符輸入流讀取文本文件,全部字符輸入流的超類
* java.io.Reader
* 專門讀取文本文件
*
* 讀取的方法 : read()
* int read() 讀取1個字符
* int read(char[] c) 讀取字符數組
*
* Reader類是抽象類,找到子類對象 FileReader
*
* 構造方法: 綁定數據源
* 參數:
* File 類型對象
* String文件名
*/
public class ReaderDemo {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("c:\\1.txt");
/*int len = 0 ;
while((len = fr.read())!=-1){
System.out.print((char)len);
}*/
char[] ch = new char[1024];
int len = 0 ;
while((len = fr.read(ch))!=-1){
System.out.print(new String(ch,0,len));
}
fr.close();
}
}
17flush方法和close方法區別
* A: flush方法和close方法區別
*a: flush()方法
* 用來刷新緩衝區的,刷新後能夠再次寫出,只有字符流才須要刷新
*b: close()方法
* 用來關閉流釋放資源的的,若是是帶緩衝區的流對象的close()方法,不但會關閉流,還會再關閉流以前刷新緩衝區,關閉後不能再寫出
18字符流複製文本文件
* A: 字符流複製文本文件
* a: 案例代碼
/*
* 字符流複製文本文件,必須文本文件
* 字符流查詢本機默認的編碼表,簡體中文GBK
* FileReader讀取數據源
* FileWriter寫入到數據目的
*/
public class Copy_2 {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("c:\\1.txt");
fw = new FileWriter("d:\\1.txt");
char[] cbuf = new char[1024];
int len = 0 ;
while(( len = fr.read(cbuf))!=-1){
fw.write(cbuf, 0, len);
fw.flush();
}
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("複製失敗");
}finally{
try{
if(fw!=null)
fw.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}finally{
try{
if(fr!=null)
fr.close();
}catch(IOException ex){
throw new RuntimeException("釋放資源失敗");
}
}
}
}
}
19總結
* 把今天的知識點總結一遍。