Java IO學習總結(二)

來源:互聯網
上載者:User

標籤:idt   seek   while   exit   檔案名稱   檔案的   new   模式   oid   

一、輸入輸出資料流體系

1)常用流分類

分類 位元組輸入資料流 位元組輸出資料流 字元輸入資料流 字元輸出資料流
抽象基類 InputStream OutputStream Reader Writer
訪問檔案 FileInputStream FileOutputStream FileReader FileWriter
緩衝流 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
轉換流     InputStreamReader OutputStreamWriter
物件流程 ObjectInputStream ObjectOutputStream    
訪問管道 PipedInputStream PipedOutputStream PipedReader PipedWriter
訪問字串     StringReader StriingWriter
訪問數組 ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
抽象基類 FilterInputStream FilterOutputStream FilterReader FilterWriter
列印流   PrintStream   WriterStream
推回輸入資料流 PushbackInputStream   PushbackReader  
特殊流 DataInputStream DataOutputStream    

表中黑色加粗的類代表節點流,必須與指定的物理節點關聯,斜體字標出的類代表抽象基類,無法直接建立執行個體。

如果進行輸入輸出的是常值內容,則考慮使用字元流,如果輸入輸出的是二進位內容,則應考慮使用位元組流。

2)轉換流

Java中只有位元組轉換成字元的轉換流,沒有字元流轉換為位元組流

轉換流樣本

public class InputStreamReaderTest {    public static void main(String[] args) {        try {            // 將System.in轉換成字元流Reader            InputStreamReader isr = new InputStreamReader(System.in);                        // 把普通的Reader流封裝成BufferedReader流方便操作            BufferedReader br = new BufferedReader(isr);                        String line = null;            while((line = br.readLine()) != null ){                if (line.equals("exit")){                    System.exit(1);                }                System.out.println("輸入的內容:" + line);            }                    } catch (IOException e) {            e.printStackTrace();        }            }}

3)Java虛擬機器讀取其他進程的資料

程式碼範例

public class ReaderProcessTest {    public static void main(String[] args) {        try {            // 建立進程            Process pro = Runtime.getRuntime().exec("java");                        // 從子線程中輸出資訊到本程式,對本程式則是輸入資訊            BufferedReader br = new BufferedReader(new InputStreamReader(pro.getErrorStream()));                        String reader = null;            while((reader = br.readLine()) != null){                System.out.println(reader);            }        } catch (IOException e) {            e.printStackTrace();        }    }}

4)RandomAccessFile類

RandomAccessFile可以自由訪問檔案任意位置,如果只需要訪問檔案部分內容,則考慮使用此類。

RandomAccessFile建立時,構造器中指定mode參數,該參數指定RandomAccessFile的訪問模式,該參數有4個值:

“r”以唯讀方式開啟指定檔案,

“rw”以讀寫方式開啟指定檔案,

“rws”類似於rw,要求將檔案內容和中繼資料得每個更新都同步寫入底層的存放裝置

“rwd”類似於rw,要求將檔案內容得每個更新同步寫入底層的存放裝置

程式碼範例:

public class RandomTest {    public static void main(String[] args) {        try {            RandomAccessFile raf = new RandomAccessFile("d:\\test.txt", "rw");            System.out.println("擷取檔案指標當前位置:" + raf.getFilePointer());                        // 從10的位置開始讀取            raf.seek(10);            byte[] buff = new byte[64];            int hasRead = 0;            while((hasRead = raf.read(buff)) != -1){                System.out.println("讀取的內容:" + new String(buff,0,hasRead));            }                        // 指標移到指定檔案最後追加,每運行一次追加一次            raf.seek(raf.length());            raf.write("我是追加的內容!\r\n".getBytes());                    } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

向指定檔案的指定位置插入內容

public class RandomAccessFileTest {    public static void main(String[] args) {        RandomAccessFileTest.insertFile("d:\\test.txt", "我是插入的內容", 10);    }        /**     * 指定檔案指定位置插入內容     * 1、建立臨時檔案,把插入點後面的內容儲存在臨時檔案中     * 2、在插入點後面追加內容     * 3、將臨時檔案中的內容追加在檔案最後     * @param fileName 檔案名稱     * @param content 插入內容     * @param pos 指定位置     */    public static void insertFile(String fileName,String content,long pos){                try {            // 建立臨時檔案            File temp = File.createTempFile("temp", null);            // jvm退出時刪除臨時檔案            temp.deleteOnExit();                        // 建立檔案輸入輸出資料流            FileInputStream fis = new FileInputStream(temp);            FileOutputStream fos = new FileOutputStream(temp);            // 建立可讀寫資料的流            RandomAccessFile raf = new RandomAccessFile("d:\\test.txt","rw");            // 指標移到插入的位置            raf.seek(pos);            // 將指標後得內容輸出到臨時檔案中            byte[] buff = new byte[64];            int hasRead = 0;            while((hasRead = raf.read(buff)) != -1){                fos.write(buff, 0, hasRead);            }                        // 再次將指標移動到插入位置            raf.seek(pos);            // 在此後面追加內容            raf.write(content.getBytes());            //將臨時檔案中的內容追加到檔案中            while((hasRead = fis.read(buff)) != -1){                raf.write(buff, 0, hasRead);            }                    } catch (IOException e) {            e.printStackTrace();        }    }}

Java IO學習總結(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.