<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-159  評論-114  文章-7  trackbacks-0
    需求,java程序與外屆交流數(shù)據(jù)。

    實際上是OS與JVM內(nèi)存交互數(shù)據(jù)。

    那么針對程序,有輸出流,也有輸入流。

    java.io包下,F(xiàn)ile類,它表示文件系統(tǒng)上的一個文件或者目錄。

    方法,總體來說是對磁盤文件的磁盤操作,不能看內(nèi)容,主要是為了獲得文件和文件屬性。

    File dir = new File("d:\");
    File f = new File(dir,"11.txt");// -->創(chuàng)建文件對象,只是一個對象,沒有反映到磁盤上。
    f.createNewFile();//-->創(chuàng)建文件
    f.mkdir();//創(chuàng)建目錄

    delete();調(diào)用完馬上就刪除了
    deleteOnExit();程序退出時,可以作為臨時文件。

    new File("d:\\....\\11.txt");
    File.separator


    file.list() 所有文件名 String[]
    file.listFiles() File[]數(shù)組

    注意如何實現(xiàn) dir *.java的效果呢,需要一個過濾器

    public File[] listFiles(FilenameFilter filter)

    public File[] listFiles(FileFilter filter)


    FilenameFilter需要實現(xiàn)

    boolean accept(File dir, String name)

    返回true,表示要包含在結(jié)果中的

    new FilenameFilter(){
          public boolean acccept(File dir,String name)
          {
               if(name.indexOf(".java") == -1) return false;
               else return true;
          }
    }


    ===================================

    File類不能讀取文件內(nèi)容。

    所以真正讀取文件內(nèi)容,需要在虛擬機中的對象和DS數(shù)據(jù)源之間建立一個數(shù)據(jù)輸入輸出流。

    按照以JVM為方向,數(shù)據(jù)流向分為輸入流,輸出流。

    按照傳輸數(shù)據(jù)單元:又有字節(jié)流和字符流。8位為一字節(jié)。

    按功能又叫做節(jié)點流,比如專門以文件為數(shù)據(jù)源與程序建立的數(shù)據(jù)流,通道,就是FileInputStream。

    給節(jié)點流,添加輔助功能的流,也就是構(gòu)造方法的參數(shù)為其他流的流,一定是過濾流。(裝飾模式應(yīng)用)



    所有字節(jié)輸入流的父類,都是InputStream。比如有子類,F(xiàn)ileInputStream,還有javax.sound.sampled.AudioInputStream

    它有一個read()方法,一次讀入一個字節(jié),由于是跨出JVM的操作,因此極其消耗資源。

    所以一般用read(byte[] b),給一個字節(jié)數(shù)組,方法返回,字節(jié)數(shù)據(jù)會讀滿,返回值為實際,讀入的字節(jié)數(shù),用于處理,最后一次時,可能并沒有裝滿,要保證文件一個字節(jié)都不差,必須爭取取出相應(yīng)個數(shù)的字節(jié)內(nèi)容。返回-1,表示沒有可以讀的了。

    read(byte[] b, int off, int len),從數(shù)組off起始位置讀入,len試圖讀入的個數(shù)。可控的。

    read(b)  <==> read(b,0,b.length);

    OutputStream

    write(int b) -> 寫一個字節(jié) 

    由于輸入、輸出流是跨出虛擬機邊界的資源,虛擬機很難回收,這時對于資源的占用,是一種極大的浪費。

    就好像,你能讓你媽幫你收拾屋子,洗衣服,但是你打開水龍口,打完水不管水龍頭,試一試。

    或者你在麥當勞吃完飯,盤子放那,有服務(wù)生來幫你收走,但你試試把盤子拿到大街上,放在外面,服務(wù)生是不能回收的。

    雖然Java幫你回收不使用的對象,但對于關(guān)閉輸入輸出流的工作,程序員是要自己負責!因為跨出虛擬機邊界,虛擬機不管。

    ============================

    節(jié)點流,F(xiàn)ileInputStream,當然也是字節(jié)流。有Stream,只是記憶方便。

    構(gòu)造器參數(shù),文件名,文件對象File

    FileInputStream fi = new FileInputStream("1.txt");
    byte b[] = new byte[100];
    int length;
    while((length = (fi.read(b))!=-1){
          String s = new String(b);
          System.out.println(s);
    }
    fi.close();

    FileOutputSteam(File file,boolean append)//第二個選項,為true,表示要追加文件內(nèi)容。

    構(gòu)造器一個FileOutputStream對象時,磁盤會同時生成一個文件(如果已存在同名文件,會先刪除)

    FileOutputSteam fo = new FileOutputStream("1.txt",true);
    String s = new String("hi my girl");
    byte[] b = s.getBytes();
    while(fo.write(b)!= -1){};
    fo.close();


    為了能夠輕松的將簡單數(shù)據(jù)類型的數(shù)據(jù),直接輸出到流中,需要對已存在字節(jié)輸出流添加功能。

    所以這種流叫做過濾流,例如java.io.DataOutputStream。它的構(gòu)造函數(shù)參數(shù)為OutputStream對象。

    FileOutputStream file = new FileOutputStream("DST1.txt");
    DataOutputStream out = new DataOutputStream(file);

    out.writeUTF(str);

    注意使用這個過濾流,輸出和輸入的順序要一致。

    輸出:

    _byte_ _short_ _ _ _int

    輸入:
    不能夠
    _ _short_ byte_ _ _ _

    這樣讀,會將byte和short的前一個字節(jié)一起讀為short類型,顯然是錯誤的。

    =================================

    給輸入輸出流添加緩沖功能的過濾流,叫做BufferedOutputStream out = new BufferedOutputStream(other outputstream);

    虛擬機中拿出一個塊區(qū)域當作緩沖區(qū),把數(shù)據(jù)先寫入緩沖區(qū),如果緩沖區(qū)滿了,再一次性將內(nèi)容寫到磁盤。

    大大的減少了虛擬機快越邊界的操作,效率大幅度提高。是犧牲空間換時間的典型做法。

    任何帶緩沖的流,都會有一個flush()的方法,可以不管緩沖區(qū)滿不滿,都情空緩沖區(qū)。

    還有直接調(diào)用.close,也會自動調(diào)用.flush()方法。



    對于過濾流,只關(guān)閉最外層的流,即可。

    ===============================

    管道,節(jié)點流

    PipedOutputStream / PipedInputStream

    pos.connect(pis);

    用于給兩個線程交換數(shù)據(jù)。

    import java.io.*;
    public class PipedStreamTest
    {
        
    public static void main(String[] args)
        
    {
            PipedOutputStream pos
    =new PipedOutputStream();
            PipedInputStream pis
    =new PipedInputStream();
            
    try
            
    {
                pos.connect(pis);
                
    new Producer(pos).start();
                
    new Consumer(pis).start();
            }

            
    catch(Exception e)
            
    {
                e.printStackTrace();
            }

            
        }

    }


    class Producer extends Thread
    {
        
    private PipedOutputStream pos;
        
    public Producer(PipedOutputStream pos)
        
    {
            
    this.pos=pos;
        }

        
    public void run()
        
    {
            
    try
            
    {
                pos.write(
    "Hello,welcome you!".getBytes());
                pos.close();
            }

            
    catch(Exception e)
            
    {
                e.printStackTrace();
            }

        }

    }


    class Consumer extends Thread
    {
        
    private PipedInputStream pis;
        
    public Consumer(PipedInputStream pis)
        
    {
            
    this.pis=pis;
        }

        
    public void run()
        
    {
            
    try
            
    {
                
    byte[] buf=new byte[100];
                
    int len=pis.read(buf);
                System.out.println(
    new String(buf,0,len));
                pis.close();
            }

            
    catch(Exception e)
            
    {
                e.printStackTrace();
            }

        }

    }



    練習實現(xiàn),文件拷貝功能,條件一個字節(jié)都能看,比如PDF,備份文件可以看,電影圖片都可以正常讀取。

    import java.io.*;
    public class TestFileCopy{
        
    public static void main(String[] args){
            BufferedInputStream in
    =null;
            BufferedOutputStream out
    =null;
            
    try{
                in
    =new BufferedInputStream(new FileInputStream(args[0]));
                out
    =new BufferedOutputStream(new FileOutputStream(args[1]));
                
    byte[] b=new byte[1024];
                
    int len;
                
    while((len=in.read(b))!=-1){
                    out.write(b,
    0,len);
                }

                out.flush();
            }

            
            
    catch(Exception e){
                e.printStackTrace();
            }

            
    finally{
                
    if (in!=null){
                    
    try{
                        in.close();
                    }

                    
    catch(Exception e){}
                }

                
    if (out!=null){
                    
    try{
                        out.close();
                    }

                    
    catch(Exception e){}
                }

            }

            
        }

    }

    注意,out.write(b,0,len);保證了最后一次,b沒有裝滿時,可以只寫裝入的數(shù)據(jù)。

    ==============================

    字符流

    java.io.Reader
    java.io.Writer

    單位為字符,一次一個字符,主要對于純文本。

    read(char[] cbuf)

    基本方法,參數(shù)為字符數(shù)組,而不是字節(jié)數(shù)組。一個字符2字節(jié)。

    FileReader,字符文件輸入流。

    那么一個字符有什么存在意義呢,

    'A'影射編碼成為整數(shù)65,而反過來,整數(shù)解碼成為'A'。如果編解碼不統(tǒng)一就會出現(xiàn)亂碼。

    ASCii,包括數(shù)字和字符,一個字節(jié),256個字符

    這是最早的編碼,所以以后的任何編碼都兼容ASCii,所以英文總沒有問題。

    ISO-8859-1,一個字節(jié) 1B

    GB2312,國標,2B,65535,‘喆’就不這個字符集中,只有常用漢字。

    GBK,2B。包含所有漢字。

    UNICODE 2B

    UTF-8,不一定占用幾個字節(jié)。

    解決亂碼問題的宗旨,就是保證編碼解碼方式一致。

    ---------------------------

    java.io.InputStreamReader

    InputStreamReader(InputStream in)

    這是一個字符流,而構(gòu)造函數(shù)參數(shù)是一個字節(jié)流。

    通常稱之為從字節(jié)六到字符流的橋轉(zhuǎn)換。

    InputStreamReader(InputStream in, String charsetName)

    第二個參數(shù)就是編碼方式參數(shù),指對字節(jié)流按照什么解碼方式將文件讀入。

    OutputStreamWriter(OutputStream out, String charsetName)

    輸出是指按什么編碼方式輸出字節(jié)流。

    import java.io.*;

    public class TestReaderWriter{

        
    static void write() throws Exception
        
    {
            FileOutputStream fos 
    = new FileOutputStream("test.dat");
            OutputStreamWriter osw 
    = new OutputStreamWriter(fos,"UTF-8");
            BufferedWriter out 
    = new BufferedWriter(osw);
            
            out.write(
    "慢慢爐火純青了!");
            out.newLine();
            out.write(
    "學完C++,Java確實很簡單,但很棒!");
            out.flush();
        }


        
    static void read() throws Exception
        
    {
            FileInputStream fis 
    = new FileInputStream("test.dat");
            InputStreamReader isr 
    = new InputStreamReader(fis,"UTF-8");
            BufferedReader in 
    = new BufferedReader(isr);
            
            String line;
            
    while((line = in.readLine())!=null)
            
    {
                System.out.println(line);
            }

        }


        
    public static void main(String[] args) throws Exception
        
    {
            write();
            read();    
            System.out.println(System.getProperty(
    "file.encoding"));
        }


    }

    PrintWriter,也是一個帶緩沖的字符輸出流,可以直接由一個OutputStream或者Writer構(gòu)造,但功能更強大,也就是方法更多。

    PrintWriter(OutputStream out, boolean autoFlush)

    PrintWriter(Writer out, boolean autoFlush)


    println(String x)

    PrintStream,也是同理,帶緩沖的字節(jié)輸出流,功能比BufferedOutputStream更強。

    ==========================================

    java.io.ObjectOutputStream 過濾流

    輸出一個對象,是對DataOutputStream的增強。

    writeObject(Object obj)

    對象序列化,對象需要實現(xiàn)Serializable接口,這個接口是一個標記接口。

    由于對象有價值的東西就是屬性,所以只關(guān)系對象的屬性文件,如果某個屬性沒有序列化的意義,那么就用transient修飾,表示不持久化該屬性。

    恢復(fù)時,為默認屬性。

    要求屬性也是Serializable的。

    用命令serialver判斷一個類是否可以序列化。

    =====================================

    早期持久化,就是通過文件存儲信息,通過定義特定分割符分開信息。

    StringTokenizer st = new StringTokenizer(s,"t");
    while(st.hasMoreTokens()){
       System.out.println();
    }

    ======================================

    void open{
       BufferedReader in = null;
       try{
          FileInputStream fis = new FileInputStream("data");
          InputStreamReader ir = new InputStreamReader(fis);
          in = new BufferedReader(ir);
       }catch(Exception e)
       {
          
       }
       finally{
          if(in!=null)
             try{
                   in.close();
             }catch(Exception){}
       }
    }



    posted on 2005-12-25 00:08 北國狼人的BloG 閱讀(435) 評論(0)  編輯  收藏 所屬分類: 達內(nèi)學習總結(jié)
    主站蜘蛛池模板: 国产精品永久免费10000| 亚洲va中文字幕无码久久不卡 | 亚洲视频在线不卡| 久久亚洲私人国产精品| 色婷婷亚洲十月十月色天| 亚洲自偷自偷精品| 精品亚洲成a人片在线观看| 亚洲美女中文字幕| 亚洲精品在线不卡| 亚洲一卡二卡三卡四卡无卡麻豆| 亚洲理论片在线观看| 亚洲伦理中文字幕| 亚洲精品国产第一综合99久久| 亚洲av无码专区亚洲av不卡| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲人成色77777在线观看| 亚洲人精品亚洲人成在线| 亚洲中文无码亚洲人成影院| 亚洲精品无码av片| 国产AV无码专区亚洲AV琪琪| 一级毛片a免费播放王色电影| 中文字幕无线码免费人妻| 暖暖免费日本在线中文| 8x8x华人永久免费视频| 97在线观看永久免费视频| 免费无码A片一区二三区 | 69精品免费视频| 67194成是人免费无码| 国产精品免费看久久久无码| 免费大片黄手机在线观看| 国产亚洲午夜高清国产拍精品| 中文字幕亚洲综合久久菠萝蜜| 国产成人精品日本亚洲网站| 99人中文字幕亚洲区| 亚洲一级免费毛片| 边摸边吃奶边做爽免费视频99| 本免费AV无码专区一区| 182tv免费视视频线路一二三| 好爽又高潮了毛片免费下载| 亚洲精品国产精品国自产观看| 亚洲国产成人精品无码区在线观看 |