Java項(xiàng)目實(shí)戰(zhàn)開(kāi)發(fā)Day17 2020-04-11

內(nèi)容

1.緩沖流
2.輸入輸出重定向
3.對(duì)象的讀寫(xiě)
4.RandomAccessFile

一.緩沖流


1.緩沖流作用

緩沖流的作用簡(jiǎn)單來(lái)說(shuō)就是能夠增加讀寫(xiě)效率。

2.緩沖流都有哪些

BufferedInputStream BufferedReader
BufferedOutputStream BufferedWriter

3.緩沖流和節(jié)點(diǎn)流在使用上的聯(lián)系/使用順序

1.使用節(jié)點(diǎn)流從磁盤(pán)將數(shù)據(jù)讀取到內(nèi)存的緩沖區(qū)
2.將內(nèi)存緩沖區(qū)的數(shù)據(jù)讀到緩沖流對(duì)應(yīng)的緩沖區(qū)
3.緩沖流從緩沖流的緩沖區(qū)將數(shù)據(jù)讀取到對(duì)應(yīng)的地方去

4.注意點(diǎn)

當(dāng)使用處理流輸出時(shí),需要使用flush來(lái)刷新流

5.1使用緩沖流完成字符文件的讀和寫(xiě)(比較低級(jí)的)

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\Users\\劉金豪\\Desktop\\temp";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\temp";
        
        //使用處理流比較快
        //1.使用節(jié)點(diǎn)流從磁盤(pán)將數(shù)據(jù)讀取到內(nèi)存的緩沖區(qū)
        //2.將內(nèi)存緩沖區(qū)的數(shù)據(jù)讀到處理流對(duì)應(yīng)的緩沖區(qū)
        //3.處理流從處理流的緩沖區(qū)將數(shù)據(jù)讀取到對(duì)應(yīng)的地方去
        try(
            //創(chuàng)建輸入流
            FileInputStream fis = new FileInputStream(src);
            BufferedReader br = new BufferedReader(fis);
                
            //創(chuàng)建輸出流
            FileOutputStream fos = new FileOutputStream(des);   
            OutputStreamWriter osw = new OutputStreamWriter(fos);//將字節(jié)輸出流轉(zhuǎn)換為字符輸出流
            BufferedWriter bw = new BufferedWriter(osw);){
            
            //一個(gè)字符一個(gè)字符地讀
            int ch = 0;
            while(true) {
              //讀取數(shù)據(jù)    
              ch = br.read();
              
              if(ch == -1) {
                  break;
              }
              
              //寫(xiě)入數(shù)據(jù)
              bw.write(ch);
            }
            
            //刷新流
            bw.flush();
        }catch(Exception e) {
            e.printStackTrace();
        }
        
        
    
        
        
            
    }

}

5.2高級(jí)寫(xiě)法

            //一個(gè)字符一個(gè)字符地讀
            int ch = 0;
            while((ch = br.read()) != -1) {
                bw.write(ch);
            }

6.使用字節(jié)緩沖流并用數(shù)組 裝乘 完成文件的讀和寫(xiě)

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
    
    public static void main(String[] args){
        String src = "C:\\Users\\劉金豪\\Desktop\\撲克游戲UML圖.jpg";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\撲克游戲UML圖.jpg";
        
        try(
        //字節(jié)緩沖輸入流
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
    
        //字節(jié)緩沖輸出流
        FileOutputStream fos = new FileOutputStream(des);
        BufferedOutputStream bos = new BufferedOutputStream(fos);){
            
                byte[] buffer = new byte[1024];
                int len = 0;
                    
                while((len = bis.read(buffer)) != -1) {
                    bos.write(buffer);
                    }
                bos.flush();
            }catch(Exception e) {
                e.printStackTrace();
            }
       
    }

}

二.輸入輸出重定向。

1.簡(jiǎn)單介紹

比如本來(lái)輸出到終端,那么我們可以重定向,使它輸出到特定的文件中。
同樣地,本來(lái)是從終端輸入,但是我們可以重定向,使其從特定的文件輸入。

2.輸入輸出重定向使用示例

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
        
        //輸出重定向
 /*     try (FileOutputStream fos = new FileOutputStream(des);
             PrintStream ps = new PrintStream(fos); )
            {
            System.setOut(ps);
            System.out.println("helloworld");//對(duì)應(yīng)的文件就會(huì)有helloworld,如果沒(méi)有這個(gè)文件,系統(tǒng)會(huì)自動(dòng)創(chuàng)建
        } catch (Exception e) {
            e.printStackTrace();
        }
*/      
        //輸入重定向
        try(FileInputStream fis = new FileInputStream(src);){
            Scanner scanner = new Scanner(fis);
            
            while(scanner.hasNext()) {
                System.out.println(scanner.next());//控制臺(tái)就會(huì)輸出 helloworld
            }   
        } catch (Exception e) {
            e.printStackTrace();
        }

            
    }

}

三.對(duì)象的讀寫(xiě)

1.注意點(diǎn)

①如果需要將自己定義的類(lèi)的某個(gè)對(duì)象使用文件保存。那么這個(gè)類(lèi)必須是實(shí)現(xiàn)了Sreializable接口。
(關(guān)于Serializable接口的詳細(xì)介紹請(qǐng)參看我另一篇博客:http://www.itdecent.cn/p/be6313ddba50
②勿忘刷新。

2.使用示例

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
        
  /*    //寫(xiě)入對(duì)象
        try(FileOutputStream fos = new FileOutputStream(des);
        ObjectOutputStream oos = new ObjectOutputStream(fos);){
          //保存對(duì)象
          //如果需要將自己定義的類(lèi)的某個(gè)對(duì)象使用文件保存
          //那么這個(gè)類(lèi)必須是實(shí)現(xiàn)了Sreializable接口
          Person person = new Person();
          oos.writeObject(person);

         //刷新
         oos.flush();
        }catch(Exception e) {
            e.printStackTrace();
        }
 */     
        //讀取對(duì)象
        try(
            FileInputStream fis = new FileInputStream(src);
            ObjectInputStream ois = new ObjectInputStream(fis);){
            
            Person p = (Person)ois.readObject();
            System.out.println(p);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    

}
class Person implements Serializable{
    
}

四.RandomAccessFile

1.引

當(dāng)一個(gè)文件存在時(shí),如果按照之前的寫(xiě)法,那么一般在寫(xiě)入數(shù)據(jù)時(shí)會(huì)把之前的數(shù)據(jù)刪除,也就是從頭寫(xiě)。這樣有時(shí)候不能滿足我們的需求,如果想在文件的某個(gè)位置讀或者寫(xiě),那么我們可以使用RandomAccessFile。

2.最基本的使用示例(使用數(shù)組裝乘)

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
  
        try {
            RandomAccessFile raf = new RandomAccessFile(src,"r");//只讀取
            
            byte[] buffer = new byte[50];
            
            raf.read(buffer);//這是字節(jié)
            //打印的話需要轉(zhuǎn)化成String
            System.out.println(new String(buffer));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

}

3.“從第幾個(gè)開(kāi)始讀取”使用示例

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
  
        try {
            RandomAccessFile raf = new RandomAccessFile(src,"r");//只讀取
            
            raf.seek(2);//從第三個(gè)開(kāi)始讀取
            byte[] buffer = new byte[50];
            
            raf.read(buffer);//這是字節(jié)
            //打印的話需要轉(zhuǎn)化成String
            System.out.println(new String(buffer));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

}

4.“從第幾個(gè)開(kāi)始寫(xiě)”使用示例

import java.io.*;
import java.util.*;

public class 測(cè)試程序   {                                                                                                             
           
    
    public static void main(String[] args){
        String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
        String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
  
        try {
            RandomAccessFile raf = new RandomAccessFile(src,"rw");//可讀可寫(xiě)
        
            raf.seek(2);//從第三個(gè)開(kāi)始寫(xiě),會(huì)覆蓋后面的內(nèi)容
            raf.writeChars("helllll");

            //System.out.println(new String());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

}

總結(jié)

內(nèi)容比較多,雖然比第一遍學(xué)習(xí)更清晰,但是還是有很多混了的地方。不慌!等明天再學(xué)一遍,把有關(guān)文件的博客再看一遍,絕對(duì)沒(méi)問(wèn)題!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. File 類(lèi)的作用? File類(lèi)是java.io包下代表與平臺(tái)無(wú)關(guān)的文件和目錄,通過(guò)File可以操作文件和目...
    一葉知休閱讀 298評(píng)論 0 1
  • 創(chuàng)建一個(gè)好的輸入/輸出(I/O)系統(tǒng)是一項(xiàng)艱難的任務(wù)。挑戰(zhàn)似乎來(lái)自于要涵蓋所有的可能性。不僅存在各種I/O源端和想...
    王偵閱讀 1,230評(píng)論 0 2
  • 本部分總結(jié)一下JAVA IO的相關(guān)知識(shí)。 全部章節(jié)傳送門(mén): JAVA IO學(xué)習(xí)筆記: IO基本知識(shí) JAVA IO...
    簡(jiǎn)單一點(diǎn)點(diǎn)閱讀 1,987評(píng)論 0 0
  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,717評(píng)論 0 5
  • 五、IO流 1、IO流概述 (1)用來(lái)處理設(shè)備(硬盤(pán),控制臺(tái),內(nèi)存)間的數(shù)據(jù)。(2)java中對(duì)數(shù)據(jù)的操作都是通過(guò)...
    佘大將軍閱讀 585評(píng)論 0 0

友情鏈接更多精彩內(nèi)容