首先,我们看这个问题,
要求: D:/AAA/d.txt 复制到 C:/AAA/d.txt.
完成文件内容的复制。
@Test
public void test02() throws Exception{
//1.创建一个字符输入流
FileReader fr=new FileReader("D:/AAA/d.txt");
//2.创建一个字符输出流
FileWriter fw=new FileWriter("C:/AAA/f.txt");
int c=0;//读取到字符的个数
char[] cs=new char[10];//每次读取的内容放入该数组中
while( (c=fr.read(cs)) !=-1 ){
fw.write(cs,0,c);
fw.flush(); //刷新
}
fw.close();
fr.close();
}
@Test
public void testOutStream() throws Exception{
OutputStream os=new FileOutputStream("D:/AAA/f.txt");
String str="abcd";
//把字符串转换为字节数组.
byte[] bytes = str.getBytes();
os.write(bytes);
os.flush();
os.close();
}
2.字节输入流@Test33
public void testInputStream() throws Exception{
InputStream is=new FileInputStream("D:/AAA/f.txt");
byte [] bytes=new byte[3];
int c=is.read(bytes); //一次读取三个字节 并把读取的内容放入字节数组中 返回读取到字节的个数
System.out.println(bytes+"=============>个数:"+c);
c=is.read(bytes); //一次读取三个字节 并把读取的内容放入字节数组中 返回读取到字节的个数
System.out.println(bytes+"=============>个数:"+c);
c=is.read(bytes); //一次读取三个字节 并把读取的内容放入字节数组中 返回读取到字节的个数
System.out.println(bytes+"=============>个数:"+c);
}
//如果文件中内容非常大 使用循环来读取
@Test
public void testInputStream2() throws Exception{
InputStream is=new FileInputStream("D:/AAA/f.txt");
byte [] bytes=new byte[300];
int c=0; //读取到的个数
while( (c=is.read(bytes))!=-1 ){
//把byte数组转换为字符串
String str=new String(bytes,0,c);
System.out.println(str);
}
is.close();
}
@Test
public void testCopy() throws Exception{
//1.创建字节输入流 视频
InputStream is=new FileInputStream("D:/AAA/1.jpg");
//2.字节输出流
OutputStream fos=new FileOutputStream("C:/AAA/2.jpg");
byte[] bytes=new byte[10];
int c=0;
while( (c=is.read(bytes)) !=-1 ){
fos.write(bytes,0,c);
}
is.close();
fos.close();
}
3.1缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream BufferOutputStream BufferReader BufferWriter 提高IO的效率,降低IO的次数。
@Test
public void TestBuffer() throws Exception{
OutputStream out=new FileOutputStream("D:/AAA/g.txt");
BufferedOutputStream bos=new BufferedOutputStream(out);//缓存流要作用再基础流上
String str="abcdefhijglmn";
byte[] bytes = str.getBytes();
bos.write(bytes); //因为你写的内容 暂时放入缓存池中 并没有直接放入文件中。 所以文件中没有你的内容。
//bos.flush();//刷新缓存池---把池子中的内容输入到文件上
bos.close(); //关闭----先刷新缓冲池 再关闭流资源
}
我们现在操作IO流的时候 都是将字符串读写操作 可不可以将java对象在文件中进行读写呢? 可以的 Student st=new Student();对象
将java对象进行读写操作 意义在于持久化信息 例如: 游戏存档。
// 因为运行的时候 所有的数据都是在运行内存中的 持久化 将运行内存的数据 保存到硬盘上 存档(写) 读档(读)
@Test //存档:----序列化:
public void testObjectStream() throws Exception{
OutputStream out=new FileOutputStream("D:/AAA/a.txt");
//对象输出流
ObjectOutputStream oos=new ObjectOutputStream(out);
//使用对象输出流调用输出方法 输出的类对象 该类必须实现Serializable 序列化接口
Role r=new Role("吕布","7级",1,"弑父");
oos.writeObject(r);
oos.close();
}
//测试 读档: ----反序列化:
@Test
public void testObjectStream2()throws Exception{
InputStream input=new FileInputStream("D:/AAA/a.txt");
ObjectInputStream ois=new ObjectInputStream(input);
Object o = ois.readObject();
System.out.println(o);
ois.close();
}
3.1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
---java对象所属的类必须实现序列化接口.implements Serializable
3.2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。



