您的位置 首页 java

java之基本流

在互联网上传输的实际是属于二进制流,包括在计算机内部一样的是二进制流在传输。

在java中有两种最基本流,字节流与字符流

字节输入流

定义:抽象类,不能实例化,必须通过其相关子类来实例化

 //实现了关闭接口,可使用带资源的try语句
public abstract class InputStream implements Closeable  

方法:均在子类中实现

public abstract int read() throws IOException——读取一个字节,并返回读取的字节值,读到尾返回-1

public int read(byte b[]) throws IOException——将读取的字节保存在字节数组中,返回的是字节数组长度,读到尾返回-1

public int read(byte b[], int off, int len) throws IOException ——将读取的字节保存在字节数组(off开始len个长度的字节)中,返回的是字节数组长度,读到尾返回-1

public void close() throws IOException——流的关闭

字节输出流

定义:抽象类,不能实例化,必须通过其相关子类来实例化

 public abstract class OutputStream implements Closeable, Flushable  

方法:均在子类中实现

public abstract void write(int b) throws IOException——将一个字节输出

public void write(byte b[]) throws IOException——将一组字符输出

public void write(byte b[], int off, int len) throws IOException——将一组字符(off开始len个长度的字符)输出

public void flush() throws IOException——强制刷新到文件中

public void close() throws IOException——流的关闭

字符输入流

定义:抽象类,不能实例化,必须通过其相关子类来实例化

 public abstract class Reader implements Readable, Closeable  

方法:均在子类中实现

方法描述public int read() throws IOException——读取一个字符,并返回读取的字节值,读到尾返回-1

public int read(char cbuf[]) throws IOException——将读取的字符保存在字符数组中,返回的是字符数组长度,读到尾返回-1

abstract public int read(char cbuf[], int off, int len) throws IOException——将读取的字符保存在字符数组(off开始len个长度的字符)中,返回的是字符数组长度,读到尾返回-1

public void close() throws IOException——流的关闭

字符输出流

定义:抽象类,不能实例化,必须通过其相关子类来实例化

 public abstract class Reader implements Readable, Closeable  

方法:均在子类中实现

public void write(int c) throws IOException——将一个字符输出

public void write(char cbuf[]) throws IOException——将一组字符输出

abstract public void write(char cbuf[], int off, int len) throws IOExceptio——将一组字符(off开始len个长度的字符)输出

public void write(String str) throws IOException——将字符串直接输出

public void write(String str, int off, int len) throws IOException ——将字符串(off开始len个长度的字符)直接输出

public Writer append(CharSequence csq) throws IOException——追加输出字符序列

public Writer append(CharSequence csq, int start, int end) throws IOExceptio——追加输出字符序列(off开始len个长度的字符序列)

public Writer append(char c) throws IOException——追加输出一个字符

public void close() throws IOException——流的关闭

文章来源:智云一二三科技

文章标题:java之基本流

文章地址:https://www.zhihuclub.com/183843.shtml

关于作者: 智云科技

热门文章

网站地图