69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

Java 文件讀寫(xiě)流

java 文件讀寫(xiě)流

java.io 包含了用于文件讀寫(xiě)的兩個(gè)流類(lèi): fileinputstream 和 fileoutputstream。

 

1. fileinputstream 讀文件流

fileinputstream 用于從文件讀取數(shù)據(jù),它的對(duì)象可以用關(guān)鍵字 new 來(lái)創(chuàng)建。

1)inputstream 構(gòu)造方法

可以使用字符串類(lèi)型的文件名來(lái)創(chuàng)建一個(gè)輸入流對(duì)象來(lái)讀取文件:

outputstream f = new fileoutputstream("c:/java/hello")

也可以使用一個(gè)文件對(duì)象來(lái)創(chuàng)建一個(gè)輸入流對(duì)象來(lái)讀取文件。我們首先得使用 file() 方法來(lái)創(chuàng)建一個(gè)文件對(duì)象:

file f = new file("c:/java/hello");
outputstream fout = new fileoutputstream(f);

2)inputstream 操作方法

創(chuàng)建了 inputstream 對(duì)象,就可以使用下面的方法來(lái)讀取流或者進(jìn)行其它操作。

序號(hào) 方法及描述
1 public void close() throws ioexception{}
關(guān)閉此文件輸入流并釋放與此流有關(guān)的所有系統(tǒng)資源。拋出ioexception異常。
2 protected void finalize()throws ioexception {}
這個(gè)方法清除與該文件的連接。確保在不再引用文件輸入流時(shí)調(diào)用其 close 方法。拋出ioexception異常。
3 public int read(int r)throws ioexception{}
這個(gè)方法從 inputstream 對(duì)象讀取指定字節(jié)的數(shù)據(jù)。返回為整數(shù)值。返回下一字節(jié)數(shù)據(jù),如果已經(jīng)到結(jié)尾則返回-1。
4 public int read(byte[] r) throws ioexception{}
這個(gè)方法從輸入流讀取r.length長(zhǎng)度的字節(jié)。返回讀取的字節(jié)數(shù)。如果是文件結(jié)尾則返回-1。
5 public int available() throws ioexception{}
返回下一次對(duì)此輸入流調(diào)用的方法可以不受阻塞地從此輸入流讀取的字節(jié)數(shù)。返回一個(gè)整數(shù)值。

除了 inputstream 外,還有一些其他的輸入流

 

2. fileoutputstream 寫(xiě)文件流

該類(lèi)用來(lái)創(chuàng)建一個(gè)文件并向文件中寫(xiě)數(shù)據(jù)。如果該流在打開(kāi)文件進(jìn)行輸出前,目標(biāo)文件不存在,那么該流會(huì)創(chuàng)建該文件。

1)fileoutputstream 構(gòu)造方法

有兩個(gè)構(gòu)造方法可以用來(lái)創(chuàng)建 fileoutputstream 對(duì)象。使用字符串類(lèi)型的文件名來(lái)創(chuàng)建一個(gè)輸出流對(duì)象:

outputstream f = new fileoutputstream("c:/java/hello")

也可以使用一個(gè)文件對(duì)象來(lái)創(chuàng)建一個(gè)輸出流來(lái)寫(xiě)文件。我們首先得使用file()方法來(lái)創(chuàng)建一個(gè)文件對(duì)象:

file f = new file("c:/java/hello");
outputstream fout = new fileoutputstream(f);

2)outputstream 操作方法

創(chuàng)建 outputstream 對(duì)象完成后,就可以使用下面的方法來(lái)寫(xiě)入流或者進(jìn)行其他的流操作。

序號(hào) 方法及描述
1 public void close() throws ioexception{}
關(guān)閉此文件輸入流并釋放與此流有關(guān)的所有系統(tǒng)資源。拋出ioexception異常。
2 protected void finalize()throws ioexception {}
這個(gè)方法清除與該文件的連接。確保在不再引用文件輸入流時(shí)調(diào)用其 close 方法。拋出ioexception異常。
3 public void write(int w)throws ioexception{}
這個(gè)方法把指定的字節(jié)寫(xiě)到輸出流中。
4 public void write(byte[] w)
把指定數(shù)組中w.length長(zhǎng)度的字節(jié)寫(xiě)到outputstream中。

除了outputstream外,還有一些其他的輸出流,更多的細(xì)節(jié)參考下面鏈接:

 

3. 文件讀寫(xiě)范例

下面是一個(gè)演示 inputstream 和 outputstream 用法的例子:

import java.io.*;

public class filestreamtest {
    public static void main(string[] args) {
        try {
            byte bwrite[] = { 11, 21, 3, 40, 5 };
            outputstream os = new fileoutputstream("test.txt");
            for (int x = 0; x < bwrite.length; x++) {
                os.write(bwrite[x]); // writes the bytes
            }
            os.close();
    
            inputstream is = new fileinputstream("test.txt");
            int size = is.available();
    
            for (int i = 0; i < size; i++) {
                system.out.print((char) is.read() + "  ");
            }
            is.close();
        } catch (ioexception e) {
            system.out.print("exception");
        }
    }
}

上面的程序首先創(chuàng)建文件test.txt,并把給定的數(shù)字以二進(jìn)制形式寫(xiě)進(jìn)該文件,同時(shí)輸出到控制臺(tái)上。

以上代碼由于是二進(jìn)制寫(xiě)入,可能存在亂碼,你可以使用以下代碼范例來(lái)解決亂碼問(wèn)題:

//文件名 :filestreamtest2.java
import java.io.*;
    
public class filestreamtest2 {
    public static void main(string[] args) throws ioexception {
    
        file f = new file("a.txt");
        fileoutputstream fop = new fileoutputstream(f);
        // 構(gòu)建fileoutputstream對(duì)象,文件不存在會(huì)自動(dòng)新建
    
        outputstreamwriter writer = new outputstreamwriter(fop, "utf-8");
        // 構(gòu)建outputstreamwriter對(duì)象,參數(shù)可以指定編碼,默認(rèn)為操作系統(tǒng)默認(rèn)編碼,windows上是gbk
    
        writer.append("中文輸入");
        // 寫(xiě)入到緩沖區(qū)
    
        writer.append("\r\n");
        // 換行
    
        writer.append("english");
        // 刷新緩存沖,寫(xiě)入到文件,如果下面已經(jīng)沒(méi)有寫(xiě)入的內(nèi)容了,直接close也會(huì)寫(xiě)入
    
        writer.close();
        // 關(guān)閉寫(xiě)入流,同時(shí)會(huì)把緩沖區(qū)內(nèi)容寫(xiě)入文件,所以上面的注釋掉
    
        fop.close();
        // 關(guān)閉輸出流,釋放系統(tǒng)資源
    
        fileinputstream fip = new fileinputstream(f);
        // 構(gòu)建fileinputstream對(duì)象
    
        inputstreamreader reader = new inputstreamreader(fip, "utf-8");
        // 構(gòu)建inputstreamreader對(duì)象,編碼與寫(xiě)入相同
    
        stringbuffer sb = new stringbuffer();
        while (reader.ready()) {
            sb.append((char) reader.read());
            // 轉(zhuǎn)成char加到stringbuffer對(duì)象中
        }
        system.out.println(sb.tostring());
        reader.close();
        // 關(guān)閉讀取流
    
        fip.close();
        // 關(guān)閉輸入流,釋放系統(tǒng)資源
    
    }
}

下一節(jié):java 文件操作

java語(yǔ)言 教程

相關(guān)文章