Java基础——输入/输出(二)

一.字节流与字符流

 

1.1 InputStream和Reader

InputStream是抽象类,本身并不能创建实例来执行输入,它里面包含如下几个方法:

1.Int read():从输入流中读取单个字节,返回所读取的字节数据。

2.int read(byte[] b):从输入流中最多读取b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数。

3.int read(byte[] b,int off,int len):从输入流中最多读取len个字节的数据,并将其存储在数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数。

4. int available() throws IOException:返回流中的有效字节数。
.long skip(long n) throws IOException: 忽略numBytes个字节,返回实际忽略的字节数

6.abstract void close() throws IOException: 关闭输入

正如前面提到的,InputStream和Reader都是抽象类,本身不能创建实例,但它们分别有一个用于读取文件的输入流:FileIputStream和FileRead,它们都是节点流——会直接和指定文件关联。

下面示范使用FileIputStream来读取自身是效果。

import java.io.*;

public class FileInputStreamTest
{
    public static void main(String[] args) throws IOException
    {
        // 创建字节输入流
        FileInputStream fis = new FileInputStream(
                "D:/j2se1/testWorkspace/Test1234/src/FileInputStreamTest.java");//这里我用了绝对地址
        // 创建一个长度为1024的“竹筒”
        byte[] bbuf = new byte[1024];
        // 用于保存实际读取的字节数
        int hasRead = 0;
        // 使用循环来重复“取水”过程
        while ((hasRead = fis.read(bbuf)) > 0 )
        {
            // 取出“竹筒”中水滴(字节),将字节数组转换成字符串输入!
            System.out.print(new String(bbuf , 0 , hasRead ));
        }
        // 关闭文件输入流,放在finally块里更安全
        fis.close();
    }
}

结果如下:

import java.io.*;

public class FileInputStreamTest
{
    public static void main(String[] args) throws IOException
    {
        // 创建字节输入流
        FileInputStream fis = new FileInputStream(
                "D:/j2se1/testWorkspace/Test1234/src/FileInputStreamTest.java");
        // 创建一个长度为1024的“竹筒”
        byte[] bbuf = new byte[1024];
        // 用于保存实际读取的字节数
        int hasRead = 0;
        // 使用循环来重复“取水”过程
        while ((hasRead = fis.read(bbuf)) > 0 )
        {
            // 取出“竹筒”中水滴(字节),将字节数组转换成字符串输入!
            System.out.print(new String(bbuf , 0 , hasRead ));
        }
        // 关闭文件输入流,放在finally块里更安全
        fis.close();
    }
}

 

 

1.2 OutputStream和Writer

OutputStrean和Writer也非常相似。两个流提供了如下方法:

1.void close() :关闭此输出流并释放与此流有关的所有系统资源。 
2.void flush() :刷新此输出流并强制写出所有缓冲的输出字节。 

3.void write(byte[] b): 将 b.length 个字节从指定的字节数组写入此输       出流。 

4.void write(byte[] b, int off, int len: 将指定字节数组中从偏移量 off         开始的 len 个字节写入此输出流。 

5.abstract void write(int b) :将指定的字节写入此输出流。

下面程序使用FileInputStream来执行输入,并使用FileOutputStream来执行输出,用于实现复制FileOutpputStreamTest.java文件的功能。

import java.io.*;

public class FileOutputStreamTest
{
    public static void main(String[] args)
    {
        try(
            // 创建字节输入流
            FileInputStream fis = new FileInputStream(
                "FileOutputStreamTest.java");
            // 创建字节输出流
            FileOutputStream fos = new FileOutputStream("newFile.txt"))
        {
            byte[] bbuf = new byte[32];
            int hasRead = 0;
            // 循环从输入流中取出数据
            while ((hasRead = fis.read(bbuf)) > 0 )
            {
                // 每读取一次,即写入文件输出流,读了多少,就写多少。
                fos.write(bbuf , 0 , hasRead);
            }
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
}

 

posted @ 2015-04-02 14:12  leejun同学  阅读(254)  评论(0编辑  收藏  举报