輸入輸出流(IO)—文件字節流(FileInputStream & FileOutputStream)的基本操作及運用

FileInputStream類是InputStream類的子類,稱爲文件字節輸入流。按字節讀取文件中的數據。

構造方法:FileInputStream(String name)

                    FileInputStream(File file)

讀取文件並輸出:

    public void test() throws IOException {
        File file = new File("hello.txt");
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            int a = in.read();
            String str = new String();
            while (a != -1) {
                str += (char) a;
                a = in.read();
            }
            System.out.println(str);
        } catch (IOException e) {
            System.out.println(e);
        }
    }


輸出結果爲:文件hello.txt中的內容。


FileOutputStream類是OutputStream類的子類,稱爲文件字節輸出流。提供基本的文件按字節寫入能力。

構造方法:FileOutputStream(String name)

                    FileOutputStream(File file)

寫入文件並輸出:

    public void test1() {
        File file = new File("hello.txt");
        byte b[] = "Welcome to read my blog!".getBytes();//字符串中的內容爲將寫入的內容,需調用getBytes()函數轉換爲字節形式
        FileOutputStream out = null;
        FileInputStream in = null;
        try {
            out = new FileOutputStream(file);
            out.write(b);
            out.flush();
            in = new FileInputStream(file);
            int n = 0;
            while ((n = in.read(b, 0, 1)) != -1) {
                String str = new String(b, 0, n);
                System.out.println(str);
            }
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }


輸出結果爲:Welcome to read my blog!      //此處內容與寫入內容一致。


文件字節流的應用:存在一個已知文件oldfile,用文件字節流進行複製到新的文件newfile,並輸出newfile中的內容

    public void CopyFile(String oldFilename, String newFilename) {
        File nfile = new File(newFilename);
        File ofile = new File(oldFilename);
        FileOutputStream out = null;
        FileInputStream in = null;
        String str = new String();
        try {
            in = new FileInputStream(ofile);
            int a = in.read();
            while (a != -1) {
                str += (char) a;
                a = in.read();
            }
        } catch (IOException e) {
            System.out.println(e);
        }
        byte b[] = str.getBytes();
        try {
            out = new FileOutputStream(nfile);
            out.write(b);
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e);
        }

    }

    @Test
    public void test2() throws IOException {
        File oldfile = new File("hello.txt");
        File newfile = new File("hel.txt");
        FileInputStream in = null;
        CopyFile(oldfile.getName(), newfile.getName());
        try {
            in = new FileInputStream(newfile);
            int a = in.read();
            String str = new String();
            while (a != -1) {
                str += (char) a;
                a = in.read();
            }
            System.out.println(str);
        } catch (IOException e) {
            System.out.println(e);
        }

    }


輸出結果爲:文件 oldfile 中的內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章