Java学习(9)
第九章 IO流
9.1 File类
方法 | 描述 |
---|---|
public File(String pathname) | 根据路径创建对象 |
public String getName() | 获取文件名 |
public String getParent() | 获取文件所在的目录 |
public Fiel getParentFile() | 过去文件所在的目录对应的File对象 |
public String getPath() | 获取文件路径 |
public boolean exists() | 判断对象是否存在 |
public boolean isDirectory() | 判断对象是否为目录 |
public boolean isFile() | 判断对象是否为文件 |
public long length() | 获取文件的大小 |
public boolean createNewFile() | 根据当前对象创建新文件 |
public boolean delete() | 删除对象 |
public boolean mkdir() | 根据当前对象创建新目录 |
public boolean renameTo(File dest) | 为已存在的对象重命名 |
9.2 字节流
IO流按照方向分,可以分为输入流和输出流;按照单位分,可以分为字节流和字符流,字节流指每次处理数据是以字节为单位的,字符流是指每次处理数据是以字符为单位的;按照功能分,可以分为节点流和处理流。
inputStream实现了Closeable接口,该接口的作用是每次操做结束之后完成资源的释放。
方法 | 描述 |
---|---|
public abstract int read() throws IOException | 以字节为单位读取数据 |
public int read (byte b[]) trhows IOException | 将数据存入byte类型数组中,并返回数据长度 |
public int read(byte b[],int off, int len) throws IOException | 将数据存入byte类型数组的指定区间中,并返回数据长度 |
public byte[] readAllBytes() throws IOException | 将数据存入byte类型数组中返回 |
public int available() throws IOException | 返回当前数据流中未读取的数据个数 |
public void close() throws IOException | 关闭数据流 |
OutpurStream实现了Closeable接口和Flushable接口。Flushable接口可以将缓冲区的数据同步到输出中。
方法 | 描述 |
---|---|
public abstract int write(int b) throws IOException | 以字节为单位读取数据 |
public void write (byte b[]) trhows IOException | 将byte类型数据数组中的数据写出 |
public void write (byte b[],int off, int len) throws IOException | 将byte类型数组中指定区间的数据写出 |
public void flush() throws IOException | 可以强制将缓冲区的数据同步到输出流中 |
public void close() throws IOException | 关闭数据流 |
9.3字符流
方法 | 描述 |
---|---|
public int read() throws IOException | 以字符为单位堵数据 |
public int read(char cbuff[]) throws IOException | 将数据读入char类型数组,并返回数据长度 |
public abstract int read (char cbuf[],int off, int len)throws IOException | 将数据读入char类型数组的指定区间,并返回数据长度 |
public abstract void close() throws IOExcetion | 关闭数据流 |
public long transferTo(Writer out) | 将数据直接读入字符输出流 |
方法 | 描述 |
---|---|
public void write(int c) throws IOException | 以字符为单位写数据 |
public void write(char cbuf[])throws IOException | 将char类型数组中的数据写出 |
public abstract void write (char cbuf[],int off , int len)throws IOException | 将char类型数组中指定区间的数据写出 |
public void write(String str)throws IOException | 将String类型的数据写出 |
public void write(String str, int off, int len)throws IOException | 将String类型指定区间的数据写出 |
public abstract void flush() throws IOException | 可以强制将缓冲区的数据同步到输出流中 |
public abstract void close() throws IOException | 关闭数据 |
9.4处理流
见Java IO流
9.5缓冲流
见Java IO流
详见书 区别stream 和reader 和writer之间的区别