字节流
方法名 | 说明 |
void write(int b) | 将指定的字节写入此文件输出流 一次写一个字节数据 |
void write(byte[] b) | 将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据 |
void write(byte[] b, int off, int len) | 将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据 |
方法 | 说明 |
FileInputStream(File file) | 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。 |
FileInputStream(String name) | 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名 |
方法 | 说明 |
public int read(byte[] b) throws IOException | 从输入流读取最多b.length个字节的数据放入数组中 |
public void write(byte b[], int off, int len) throws IOException | 从字节数组下标off开始,将其中len个数据写入到文件的输出流中 |
方法名 | 说明 |
BufferedOutputStream(OutputStream out) | 创建字节缓冲输出流对象 |
BufferedInputStream(InputStream in) | 创建字节缓冲输入流对象 |
方法--一次读写一个字节 | 说明 |
public int read() throws IOException | 从输入流中读出8192个字节到缓冲数组中,再从缓冲数组中取出一个字节 |
public void write(int b) throws IOException | 将字节b写入到缓冲数组中,当缓冲数组满时,一次性写入目标文件 |
方法--一次读一个数组 | 说明 |
public int read(byte b[]) throws IOException | 从输入流中读出8192个字节到缓冲数组中,再从缓冲数组中取出数组b.length个字节到数组b中 |
public void write(byte b[], int off, int len) throws IOException | 将数组b中的元素,从下标0开始,向缓冲数组中写入len个字节,当缓冲数组满时,一次性写入目的地 |
- BufferedOutputStream:字节缓冲输出流
- BufferedInputStream: 字节缓冲输入流
- 字节缓冲流
- 字节流读取文件
- 字节流写出
- InputStream:这个抽象类是表示字节输入流的所有类的超类
- OutputStream:这个抽象类是表示字节输出流的所有类的超类
- 使用FileInputStream和FileOutputStream读写文件
- 字节流的抽象基类
字符流
中文乱码问题
编码方法
方法 | 说明 |
public byte[] getBytes() | 使用平台的默认字符集将该String编码为一系列字节,将结果存储到新的字节数组中 |
public byte[] getBytes(String charsetName) | 使用命名的字符集将这个String编码成一个字节序列,将结果存储到一个新的字节数组中 |
解码方法
方法 | 说明 |
String(byte[] bytes) | 通过使用平台的默认字符集解码指定的字节数组来构造新的 String |
String(byte[] bytes, String charsetName) | 构造一个新的String由指定用指定的字节的数组解码 |
字符流写数据
方法名----构造方法 | 说明 |
FileWriter(File file) | 根据给定的 File 对象构造一个 FileWriter 对象 |
FileWriter(File file, boolean append) | 根据给定的 File 对象构造一个 FileWriter 对象 |
FileWriter(String fileName) | 根据给定的文件名构造一个 FileWriter 对象 |
FileWriter(String fileName, boolean append) | 根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象 |
方法名----成员方法 | 说明 |
void write(int c) | 写一个字符 |
void write(char[] cbuf) | 写入一个字符数组 |
void write(char[] cbuf, int off, int len) | 写入字符数组的一部分 |
void write(String str) | 写一个字符串 |
void write(String str, int off, int len) | 写一个字符串的一部分 |
方法名 | 说明 |
flush() | 刷新流,之后还可以继续写数据 |
close() | 关闭流,释放资源,但是在关闭之前会先写出数据。一旦关闭,就不能再写数据 |
字符流读数据
方法名 | 说明 |
FileReader(File file) | 在给定从中读取数据的 File 的情况下创建一个新 FileReader |
FileReader(String fileName) | 在给定从中读取数据的文件名的情况下创建一个新 FileReader |
方法名 | 说明 |
int read() | 一次读一个字符数据 |
int read(char[] cbuf) | 一次读一个字符数组数据 |
字符缓冲流
方法名---输入流 | 说明 |
BufferedReader(Reader in) | 创建字符缓冲输入流对象 |
String readLine() | 读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符如果流的结尾已经到达,则为null |
方法名---输出流 | 说明 |
BufferedWriter(Writer out) | 创建字符缓冲输出流对象 |
public void write(int c) | 写一个字符 |
public void write(char[] cbuf, int off, int len) | 写入字符数组的一部分 |
public void write(String s, int off, int len) | 写一个字符串的一部分 |
public void flush() | 刷新流 |
void newLine() | 写一行行分隔符,行分隔符字符串由系统属性定义 |