public class Test1 {
@Test
public void test01(){
System.out.println("============");
}
@Test
public void test02() throws Exception{
FileReader fr = new FileReader("D:/AAA/bbb.txt");
FileWriter fw = new FileWriter("C:/AAA/a.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();
}
@Test
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);
}
@Test
public void testInputStream2() throws Exception{
InputStream is = new FileInputStream("D:/AAA/f.txt");
byte[] bytes = new byte[3];
int c=0;
while ((c=is.read(bytes)) !=-1){
String str = new String(bytes,0,c);
System.out.println(str);
}
is.close();
}
//图片复制
@Test
public void testCopy() throws Exception{
InputStream is = new FileInputStream("D:/AAA/1.jpg");
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();
}
@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.close();
}
@Test
public void testObjectStream() throws Exception{
OutputStream out = new FileOutputStream("D:/AAA/a.txt");
ObjectOutputStream oos = new ObjectOutputStream(out);
//Role r=new Role("吕布","1级",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();
}
}
缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream BufferOutputStream BufferReader BufferWriter 提高IO的效率,降低IO的次数。
对象流--对java对象进行io操作
在操作Io流的时候 都是将字符串读写操作 将java对象在文件中进行读写
Student st=new Student();
1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
---java对象所属的类必须实现序列化接口.implements Serializable
2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。