JavaIO流四大基类:
- InputStream:字节输入流
- OutputStream:字节输出流
- Reader: 字符输入流
- Writer:字符输出流
字符流读写纯文本文件
/*---字符流---
逐个字符读取(char),读取字符会快
* */
/**
* 读取文件中的字符:输入流 FileReader
*/
@Test
public void test1() throws IOException {
FileReader fr = new FileReader("text.txt");
//读取单个字符,返回Unicode编码
int a = fr.read();
System.out.println(a);
char[] arr = new char[10];
//如果read()没有读取到数据返回-1
while (true){
//读取多个字符,返回实际读取的字符数
int len = fr.read(arr);
if (len == -1) {
break;
}
System.out.print(new String(arr,0,len));
}
//关闭流
fr.close();
}
/**
* 将文本数据写入的到文件 FileWriter
*/
@Test
public void test2() throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("输入内容:");
//nextLine():会读取上衣个输入流的换行符,遇到换行符结束
String str = in.nextLine();
//public FileWriter(String fileName, boolean append)append为true在文件中追加否则会覆盖原来的内容
FileWriter fw = new FileWriter("text.txt",true);
//写入str
fw.write(str);
//关闭流
fw.close();
in.close();
}
/**
* 纯文本文件内容复制
*/
@Test
public void testCopy() throws IOException {
copy("text.txt","text2.txt");
}
public void copy(String srcFileName,String targetFileName) throws IOException {
FileReader fr = new FileReader(srcFileName);
FileWriter fw = new FileWriter(targetFileName);
//一边读一边写
char[] chars = new char[10];
int len;
// 源文件-->fr-->chars数组-->fw-->目标文件
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
//关闭
fr.close();
fw.close();
}
字节流读写文件
/*---字节流---
* 逐个字节读取,可读取任意类型文件
* */
public void testInputStream() throws IOException {
FileInputStream fis = new FileInputStream("text.tx");
byte[] bytes = new byte[10];
int len;
while ((len = fis.read(bytes)) != -1){
System.out.println("对文件进行操作");
}
}
/**
* 复制任意类型的文件
*/
@Test
public void testCopy2() throws IOException {
copy("image_gta.jpg","image2.jpg");
}
public void copy2(String srcFileName,String targetFileName) throws IOException {
FileInputStream fis = new FileInputStream(srcFileName);
FileOutputStream fos = new FileOutputStream(targetFileName);
byte[] bytes = new byte[10];
int len;
//源文件-->fis-->bytes-->fos-->目标文件
while ((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
}
Buffered缓冲流
缓冲IO流:
- 是处理流,负责在其他IO流的基础上增加缓冲功能
- 缓冲:每次读取的数据放到缓冲区中,缓冲区装满后再送到内存中,
- 相当于快递,一次送一个件和等车满了一次送一车的区别
- 缓冲区默认大小: 8192字节/字符
/**
* 缓冲复制文本文件
* @throws IOException
*/
@Test
public void test1() throws IOException {
long start = System.currentTimeMillis();
FileReader fr = new FileReader("text.txt");
BufferedReader bfr = new BufferedReader(fr);
FileWriter fw = new FileWriter("text2.txt");
BufferedWriter bfw = new BufferedWriter(fw);
String str;
//readLine():读取一行数据
//newLine() 换行,不分平台,列如C语言中换行为"\r\n"
while ((str = bfr.readLine()) != null){
bfw.write(str);
bfw.newLine();//换行
}
//注意先关闭buffered
//flush():刷新缓冲区并强制写出数据
bfr.close();
fr.close();
bfw.close();
fw.close();
long end = System.currentTimeMillis();
System.out.println("所用时间:" + (start - end) + "毫秒");
}
转换流
/**
* 解码:将字节输入流转换为字符输入流 InputStreamReader
*/
public class InputStreamReaderDemo {
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("D:"+ File.separator+"io.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
//D:/io.txt(GBK)-->fis(字节)-->isr(GBK字符)
char[] chars = new char[10];
int len = isr.read(chars);
System.out.println(new java.lang.String(chars,0,len));
isr.close();
isr.close();
}
/**
* 编码:将字符流转换为字节流,并设置编码格式 OutputStreamWriter
*/
public class OutputStreamWriterDemo {
@Test
public void test() throws IOException {
//当前平台是UTF-8,系统是GBK
//String 编码的方getBytes()
String str = "不必纠结当下,也不必太担忧未来,人生没有无用的经历,所以我们一直走,天一定会亮.";
FileOutputStream fos = new FileOutputStream("D:"+ File.separator+"io.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
//str(字符)-->osw(GBK字节)-->fos-->文件
osw.write(str);
osw.close();
fos.close();
}
数据IO流
能够输出类中不同数据类型的数据,并且再次读取时还原数据类型
- 列如:退出游戏后保存数据
- JavaIO流体系中运用了装饰者设计模式,可以再原来的功能上附加额外功能
/**
* 保存数据 DataOutputStream
* @throws IOException
*/
@Test
public void test1() throws IOException {
int age = 18;
char gender = '男';
String name = "edc";
double stature = 1.80;
boolean good = true;
//选择字节流
FileOutputStream fos = new FileOutputStream("data.dat");//.dat文件来保存数据
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(age);
dos.writeChar(gender);
dos.writeUTF(name);
dos.writeDouble(stature);
dos.writeBoolean(good);
dos.close();
fos.close();
}
/**
* 读取数据
*/
@Test
public void test2() throws IOException {
FileInputStream fis = new FileInputStream("data.dat");
DataInputStream dis = new DataInputStream(fis);
int age = dis.readInt();
char gender = dis.readChar();
String name = dis.readUTF();
double stature = dis.readDouble();
boolean good = dis.readBoolean();
//注意读的顺序必须和写入的循序一致
System.out.println(age);
System.out.println(gender);
System.out.println(name);
System.out.println(stature);
System.out.println(good);
dis.close();
fis.close();
}
对象序列化与反序列化
序列化:将Java对象保存到磁盘文件中
- 如果要将对象需列化,请实现Serializable接口(包括引用对象数据类型的属性)
- 否则会NotSerializableException
- 反序列化:将字节序列转换成Java对象
/**
* 序列化 ObjectOutputStream
* @throws IOException
*/
@Test
public void test1() throws IOException {
User user = new User("edc",40,"good");
FileOutputStream fos = new FileOutputStream("obj.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//内存中的对象-->oos-->fos-->obj.dat
oos.writeObject(user);
oos.close();
fos.close();
}
/**
* 反序列化 ObjectInputStream
*/
@Test
public void test2() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("obj.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
User user = (User) ois.readObject();
System.out.println(user.toString());
}