java_IO流总结.docx_第1页
java_IO流总结.docx_第2页
java_IO流总结.docx_第3页
java_IO流总结.docx_第4页
java_IO流总结.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

*第19天 IO流*基本内容*字符流:Reader常用子类:InputStreamReader-(继承)FileReader,BufferedReaderWriter常用子类:OutputStreamWriter-(继承)FileWriter,BufferedWriter字节流:OutputStream 常用子类:FileOutputStream,DataOutputStream,ObjectOutputStream,BufferedOutputStream,ByteArrayOutputStreamInputStream常用子类:FileInputStream,DataInputStream,ObjectOutputStream,BufferedInputStream,ByteArrayInputStream打印流:PrintStream常用子对象:System.out,System.in,System.err=*字节流:OutputStream,InputStream*一、字节流:FileInputStream,FileInputStream/文件读写import java.io.*;public class Text1 public static void main(String args) FileInputStream fis=null;FileInputStream fos=null; try fis=new FileInputStream(e:tempfrom.zip);fos=new FileOutputStream(e:tempto.zip);byte buffer=new byte1024;while (true) int temp = fis.read(buffer,0,buffer.length);if(temp=-1)break;fos.write(buffer,0,temp); catch (Exception e) e.printStackTrace(); finally try /捕获io流关闭异常fis.close(); fos.close(); catch (Exception e2) 二、字节缓冲流:BufferedInputStream,BufferedOutputStream-/文件读写public class Text2 public static void main(String args) String fileName=e:老罗视频Android01-09.zip;String toName=e:temp老罗视频Android01-09.zip;FileInputStream fis=null;FileOutputStream fos=null;BufferedInputStream bis=null;BufferedOutputStream bos=null;try fis=new FileInputStream(fileName);fos=new FileOutputStream(toName);bis=new BufferedInputStream(fis);bos=new BufferedOutputStream(fos);long time1=System.currentTimeMillis();/打印时间while (true) byte buffer=new byte1024;int temp=bis.read(buffer,0,buffer.length);if (temp=-1) break;bos.write(buffer,0,temp);long time2=System.currentTimeMillis();System.out.println(time2-time1)/60000);/求时间差 catch (IOException e) e.printStackTrace();finally try bis.close();bos.close(); catch (IOException e) e.printStackTrace();三、数据流:DataInputStream,DataOutputStreampublic class Text3 public static void main(String args) throws IOException int x=8;char ch=a;double f=5.6;DataOutputStream dos=new DataOutputStream(new FileOutputStream(e:tempdis.txt);DataInputStream dis=new DataInputStream(new FileInputStream(e:tempdis.txt);dos.writeInt(x);dos.writeChar(ch);dos.writeDouble(f);dos.close();int getx=dis.readInt();/8char getch=dis.readChar();/adouble getf=dis.readDouble();/5.6四、对象数据流:ObjectInputStream,ObjectDataOutputStream-/写入对象数据public class Task4 public static void main(String args) throws Exception ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream(e:tempobs.txt);ObjectInputStream ois=new ObjectInputStream(new FileInputStream(e:tempobs.txt);obs.writeObject(new Student(杨过,20,99);obs.writeObject(new Student(小龙女,16,77);obs.writeObject(new Student(小小龙女,11,77);obs.writeObject(new Student(小小小龙女,7,77);while(true)try Student stu=(Student)ois.readObject();System.out.println(stu); catch (Exception e) break;class Student implements Serializable/实现序列化接口/*Externalizable*/String name;int age;double score;transient String id;/不希望属性被序列化,不会在写入文件时保存public Student(String name, int age, double score) = name;this.age = age;this.score = score;Overridepublic String toString() return Student name= + name + , age= + age + , score= + score + ;/*class Student1 implements Externalizable/自定义序列化String name;int age;double score;transient String id;/不希望属性被序列化,不会在写入文件时保存public Student1() public Student1(String name, int age, double score) = name;this.age = age;this.score = score;Overridepublic String toString() return Student name= + name + , age= + age + , score= + score + ;Overridepublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException System.out.println(反序列化);=(String)in.readObject();this.age=in.readInt();this.score=in.readDouble();Overridepublic void writeExternal(ObjectOutput out) throws IOException System.out.println(序列化);out.writeObject(name);out.writeInt(age);out.writeDouble(score);*/五、内存流ByteArrayOutputStream,ByteArrayInputStream-public class Text5 public static void main(String args) throws IOException ByteArrayInputStream bis=new ByteArrayInputStream(6217001820008037164.getBytes();ByteArrayOutputStream bos=new ByteArrayOutputStream();int m=0;while(true)int ch=bis.read();/bis.read(byte,0,len)if(ch=-1)break;/*int retch;if (ch=a) retch=Character.toUpperCase(char)ch);elseretch=ch;*/bos.write(ch);/bis.write(byte,0,len)m+;if (m=4) bos.write( .getBytes();m=0;System.out.println(new String(bos.toByteArray();=*字符流:Reader,Writer*一、字符流:FileReader FileWriter:-/文本文件复制public class CopyFile public static void main(String args) File from=new File(e:tempfrom2.txt);File to=new File(e:tempto3.txt);FileReader fr=null;FileWriter fw=null;try fr=new FileReader(from);fw=new FileWriter(to);char buffer=new char100;while (true) int ret=fr.read(buffer,0,buffer.length);if (ret=-1) break;fw.write(buffer,0,buffer.length); catch (IOException e) e.printStackTrace();finally try fr.close();fw.close(); catch (IOException e) e.printStackTrace();二、字符缓冲流:BufferedReader,BufferedWriter- /文本文件读写public class Text2 public static void main(String args) String fileName=e:tempfrom.txt;String toName=e:tempto11.txt;FileReader fr=null;FileWriter fw=null;BufferedReader br=null;BufferedWriter bw=null;try fr=new FileReader(fileName);br=new BufferedReader(fr);fw=new FileWriter(toName);bw=new BufferedWriter(fw);while(true)String line=br.readLine();if (line=null) break;System.out.println(line);bw.write(line,0,line.length();bw.newLine(); catch (IOException e) e.printStackTrace();finally try br.close();bw.flush();bw.close(); catch (IOException e) e.printStackTrace();三、字符转换流:InputStreamReader OutputStreamWriter-/文件读写public class Text3 public static void main(String args) throws IOException String srcfilename = D:徐莎_备课java_day18安排.txt;/* readLine();-BufferedReader对象的方法 * 构建BufferedReader对象需要Readerd对象参数 * 由于FileReader使用的默认字符集进行解码,不能用FileReader * 可以用FileInputStream * 考虑如何把FileInputStream转换成Reader的对象 * 可以使用转换流:InputstreamReader * InputStreamReader(InputStream in, String charsetName) */FileInputStream fis = new FileInputStream(srcfilename);/原始流InputStreamReader isr = new InputStreamReader(fis, gb2312);/把字节流转换成字符流BufferedReader br = new BufferedReader(isr);/创建装饰流对象 String line = br.readLine();System.out.println(line); line = br.readLine();System.out.println(line); br.close();=*打印流:PrintStream*public class PrintstreamDemo public static vo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论