Java IO之 字节输出流(OutoutStream)&字节输入流(InputStream)

    xiaoxiao2022-07-02  105

    1.字节输出流

    如果要想通过程序进行内容输出,则可以使用java.io.OutputStream。

    OutputStream类实现了Closeable,Flushable两个接口,这两个接口中的方法:

    Closeable: public void close() throws IOException;Flushable: public void flush() throws IOException;

    在OutputStream类中还定义有其他方法:

    将给定的字节数组内容全部输出:public void write(byte b[]) throws IOException将部分字节数组内容输出:public void write(byte b[], int off, int len) throws IOException输出单个字节:public abstract void write(int b) throws IOException;

    如果要进行文件的操作,可以使用FileOutputStream类来处理, 这个类的构造方法如下:

    接收File类(覆盖):public FileOutputStream(File file) throws FileNotFoundException接收File类(追加):public FileOutputStream(File file, boolean append)

    eg:实现文件的内容输出

    import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class TestOutputStream { public static void main(String[] args)throws Exception { File file = new File("C:\\Users\\64576\\Desktop\\Hello.txt"); if( !file.getParentFile().exists()){ file.getParentFile().mkdirs(); } OutputStream outputStream = new FileOutputStream(file,true); String msg = "Hello everyone!"; outputStream.write(msg.getBytes()); outputStream.close(); } }

    在进行文件输出的时候,所有的文件会自动帮助用户创建,不在需要调用createFile()方法手工创建。

    这个时候程序如果重复执行,并不会出现内容追加的情况而是一直在覆盖。如果需要文件内容追加,则需要调用 FileOutputStream提供的另外一种构造方法。

    eg:文件内容追加

    import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class TestOutputStream1 { public static void main(String[] args) throws Exception{ File file = new File("C:\\Users\\64576\\Desktop\\Hello.txt"); if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } OutputStream outputStream = new FileOutputStream(file,true); String msg = "你好呀!"; outputStream.write(msg.getBytes()); outputStream.close(); } }

    eg:部分内容输出

    import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class TestOutputStream1 { public static void main(String[] args) throws Exception{ File file = new File("C:\\Users\\64576\\Desktop\\Hello.txt"); if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } OutputStream outputStream = new FileOutputStream(file,true); String msg = "你好呀!"; outputStream.write(msg.getBytes(),0,5); outputStream.close(); } }

    2.字节输入流(InputStream)

    InputStream类只实现了Closeable接口,在InputStream类中提供有如下方法:

    读取数据到字节数组中,返回数据的读取个数。 如果此时开辟的字节数组大小大于读取的数据大小,则返 回的就是读取个数;如果要读取的数据大于数组的内容,那么这个时候返回的就是数组长度;如果没有数据了还在读,则返回-1: public int read(byte b[]) throws IOException.

    最常用方法 :

    读取部分数据到字节数组中,每次只读取传递数组的部分内容,如果读取满了则返回长度(len),如果没有 读取满则返回读取的数据个数,如果读取到后没有数据了返回-1: public int read(byte b[], int off, int len) throws IOException

    读取单个字节,每次读取一个字节的内容,直到没有数据了返回-1: public abstract int read() throws IOException;

    同OutputStream的使用一样,InputStream是一个抽象类,如果要对其实例化,同样也需要使用子类。如果要对文件进行处理,则使用FileInputStream类。

    eg:实现文件信息的读取

    import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class TestInputStream { public static void main(String[] args) throws Exception{ File file = new File("C:\\Users\\64576\\Desktop\\Hello.txt"); if(file.exists()){ InputStream inputStream = new FileInputStream(file); byte[] data = new byte[1024]; int len = inputStream.read(data); String result = new String(data,0,len); System.out.println("读取内容为:" + result); inputStream.close(); } } }
    最新回复(0)