I/O流
|-----如何认识io流?
(1),方向输入,输出
(2),类型字节,字符
u 流是指一连串流动的字符,是以先进先出方式发送信息的通道
|------inputstream:输入流
|------任务:把d盘下面的文本文件写到console控制台上
Unhandled exception type FileNotFoundException:只要是io流,连接数据库都会产生很多的异常,不可避免的!
available()
:自动的找剩下还有多少没有读完的字节数
返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数
|-----available(),不受
阻塞后,带了另外一个问题:性能问题 一直在读取的过程,其它的线程是不能执行,只能让当前的io读取完成!
|-----inputstream 输入字节流
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub //加载文件 File file=new File("d:/计算机基础知识.txt"); InputStream ist=null; try { ist=new FileInputStream(file); //把硬盘的文件读取过来 byte[] bt=newbyte[ist.available()];//让计算机自已去计算大小 int length= ist.read(bt);//把io读取到字节数组中 while(length!=-1){ //还没有读完 String str=new String(bt);//把数组转换成String类 System.out.println(str); //改变表达式值 length=ist.read(bt);//一直读到返回-1为止 } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //只要是流必须关闭:流(单工模式) try { ist.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
|----outputstream
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub OutputStream ost=null; try { ost=new FileOutputStream("d:/abc.txt");//指定的地址创建一个文件 String str="我爱你!但你不爱我!"; ost.write(str.getBytes());//字符串和字节之间可以任意进行转换 System.out.println("写入成功!"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ try { ost.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
|
|-----任务:把d:某个txt文件用io形式写到e:盘中 (io复制)
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub InputStream ist=null; OutputStream ost=null; try { ist=new FileInputStream("d:/计算机基础知识.txt"); ost=new FileOutputStream("e:/abc.txt"); byte[] bt=newbyte[ist.available()]; int len= ist.read(bt);//向bt数组存储 while(len!=-1){ //写 ost.write(bt);//把bt写到abc.txt len= ist.read(bt); } System.out.println("io流复制完成!"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //注意关闭顺序 从后往前关 try { ost.close(); ist.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
} |
|----字节流 不仅仅可处理文本,二进制类型也可以处理(图片,音(视)频)
|-----字符流
Reader 接口 filereader
Writter 接口 filerwritter
|----filerwritter字符流写的过程类似于StringBuffer()初始化空间时间分配一个大小的位置,
|-----缓冲字符流
|-----缓存(缓冲):由计算机硬件提供一个概念 ,主要的目的:提高读取的能力(性能)
|-----字符流一个规律:xxxReader; xxxWriter;
使用字节流读写二进制文件
|----DataInputStream 与 FileInputStream
-DataOutputStream 与 FileOutputStream
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub InputStream ist=null; DataInputStream dis=null; try { ist=new FileInputStream("d:\\计算机基础知识.txt"); dis=new DataInputStream(ist); //char cc= dis.readChar();//读取是一个数据类型 //System.out.println(cc); byte[] bt=newbyte[ist.available()]; int len=dis.read(bt); while(len!=-1){ String str=new String(bt); System.out.println(str); len=dis.read(bt); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ try { dis.close(); ist.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
|------序列化
|-------瞬时状态(放在内存中的 ram)转换成持久状态,称这个过程叫做序列化
|-----序列化
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub ObjectOutputStream ojs=null; try { ojs=new ObjectOutputStream(new FileOutputStream("e://aaa.txt")); Person person=new Person(110,"鲁直伸"); ojs.writeObject(person); System.out.println("序列化操作完成!"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ try { ojs.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
} |
|------反序列化:将序列化中的文件进行解析!重新构建一个对象,就不用一行一行去读取!
publicstaticvoid main(String[] args) { // TODO Auto-generated method stub ObjectInputStream ois=null; try { ois=new ObjectInputStream(new FileInputStream("e:/aaa.txt")); Person person =(Person)ois.readObject();//重构出来的 System.out.println("id:"+person.getId()); System.out.println("name:"+person.getName()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |