JAVA IO流各类关系图
分为 字符流和字节流
字符流:Reader Writer (都是抽象类)
字节流:InputStream OutputStream (都是抽象类)
计算机所有的存储都是按字节存储的。
使用字符流读取字符的时候,也就是把字节转换成了字符,需要指定匹配的编码,不然会出现乱码。
常用的类:
字符流——读写文本操作 FileReader FileWriter
字符流——带缓存读写文本 BufferedReader BufferedWriter
字节流——读写文件 FileInpuStream FileOutputStream
字节流——带缓存读写文件 BufferedInputStream BufferedOutputStream
对象的序列化和反序列化 ObjectOutputStream ObjectInputStream
关于缓冲流:
缓冲区就是内存里的一块区域,把数据先存内存里,然后一次性写入磁盘。
在内存中读写数据,比直接在磁盘上操作数据的速度,所以在操作频繁的时候,用缓冲流会大大提高效率
用缓冲区的操作数据时,最后一定要用close关闭文件,或者用flush把缓冲区的数据刷新到磁盘。
字符流——读写文本操作 FileReader FileWriter
import java.io.*;
public class TestJavaIo {
public static void main(String[] args) throws IOException {
try {
//读
FileReader fr = new FileReader("test.txt");
char[] ch = new char[1024];
int x = 0;
while((x = fr.read(ch)) != -1) {
String s = new String(ch,0,x);
System.out.print(s);
}
fr.close();
//写
FileWriter fw = new FileWriter("test_2.txt");
String s2 = "abcdefg";
fw.write(s2);
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
字符流——带缓存读写文本 BufferedReader BufferedWriter
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的
读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。charset
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的
将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。charset
import java.io.*;
public class TestJavaIo {
public static void main(String[] args) throws IOException {
//字节流——读写文件 BufferedInputStream BufferedOutputStream
try {
//读
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(isr);
String s = "";
while((s = br.readLine()) != null) {
System.out.println(s);
}
fis.close();
//写
FileOutputStream fos = new FileOutputStream("test_2.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
BufferedWriter bw = new BufferedWriter(osw);
String s2 = "abcdef";
bw.write(s2);
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
BufferedReader类函数:
void close() 关闭流并释放与之关联的任何系统资源。
void mark(int readAheadLimit) 标记流中的当前位置。
boolean markSupported() 告诉这个流是否支持mark()操作,它确实支持。
int read ()读取单个字符。
int read(char[] cbuf, int off, int len)将字符读入数组的一部分。
String readLine() 读取一行文本。
boolean ready() 表示此流是否已准备好读取。
void reset() 将流重置为最近的标记。
long skip(long n) 跳过字符。
BufferedWriter类函数:
void close() 关闭流,先冲洗它。
void flush() 刷新流。
void newLine() 写入行分隔符。并不是所有的都是平台都使用换行符'\n'
void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
void write(int c) 写一个字符。
void write(String s, int off, int len) 写入字符串的一部分。
字节流——读写文件 FileInpuStream FileOutputStream
import java.io.*;
import java.util.*;
public class TestJavaIo {
public static void main(String[] args) throws IOException {
try {
//读
byte[] b1 = new byte[1024];
FileInputStream fis = new FileInputStream("test.txt");
int read_count = 0;
while((read_count = fis.read(b1)) != -1) {
String s1 = new String(b1,0,read_count-1);
System.out.println(s1);
}
fis.close();
//写
FileOutputStream fos = new FileOutputStream("test_2.txt");
String s2 = "abcdefghik";
byte[] b2 = s2.getBytes();
fos.write(b2);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
字节流——带缓存读写文件 BufferedInputStream BufferedOutputStream
一定要关闭文件或者用flush把缓存中的数据刷新到内存
public class TestJavaIo {
public static void main(String[] args) throws IOException {
try {
//读
byte[] b1 = new byte[1024];
FileInputStream fis = new FileInputStream("test.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int read_count = 0;
while((read_count = bis.read(b1)) != -1) {
String s1 = new String(b1,0,read_count-1);
System.out.println(s1);
}
fis.close();
//写
FileOutputStream fos = new FileOutputStream("test_2.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
String s2 = "abcdefghik";
byte[] b2 = s2.getBytes();
bos.write(b2);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
对象的序列化和反序列化 ObjectOutputStream ObjectInputStream
序列化的对象必须实现接口 Serializable
ObjectOutputStream必须和ObjectInputStream定义同一类型的流 和流内容
ObjectOutputStream 用readObject() 反序列化的时候,可以利用读取结束捕获异常来判断结束
import java.io.*;
public class TestObject {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//序列化
Worker w = new Worker("LCB", 23, 2500.34);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Work.txt"));
oos.writeObject(w);
oos.close();
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Work.txt"));
Worker w2 = (Worker)ois.readObject();
System.out.println(w2);
ois.close();
}
}
class Worker implements Serializable{
private String name;
private int age;
private double wage;
Worker(String name,int age,double wage){
this.name = name;
this.age = age;
this.wage = wage;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", wage=" + wage + "]";
}
}