黑马程序员——Java中IO流笔记(下)

来源:互联网 发布:平安车险网络直销 编辑:程序博客网 时间:2024/05/15 01:59

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


IO流


目录:    

                一、基本数据类型操作流
二、内存操作流
三、打印流
四、标准输入输出流
五、随机访问流
六、序列化流

//======================================================================


一、基本数据类型操作流


   DataInputStream 基本数据类型输入流,读取基本数据类型
   DataOutputStream 基本数据类型输出流,写入基本数据类型
   
   1)DataOutputStream
     构造方法,DataOutputStream(OutputStream out)
     传递字节输出流,基本数据类型输出流,将基本类型写入到一个字节输出流中 
     传递 FileOutputStream
     
     写的方法  write基本类型的名字  
writeInt  writeBoolean writeDouble
     writeInt(int a)
     
   2)DataInputStream
     构造方法,DataInputStream(InputStream in)
     传递字节输入流,从字节输入流包装的文件中,读取JAVA基本数据类型
     传递 FileInputStream
     
     读取的方法 readInt()  返回值int
     
     读取基本数据类型,文件结尾,不可能返回-1  -1是有效的基本数据类型
     读取文件的结尾,抛出异常 java.io.EOFException


 示例:  

/* * 从文件中,读取JAVA基本数据类型 */public static void myReadInt(){//创建基本数据类型输入流,构造器传递字节输入流DataInputStream dis = null;  try{dis = new DataInputStream(new FileInputStream("c:\\data.txt"));//读取基本类型int   readInt()/*int x = dis.readInt();System.out.println(x);*/int len = 0;while(true){len = dis.readInt();System.out.println(len);}   }catch(IOException ex){       }finally{  try{   dis.close();  }catch(IOException ex){    }}}示例:/* * 将基本数据类型int,写入到文件中 */public static void myWriteInt()throws IOException{//创建基本数据类型输出流,构造方法中,传递字节输出流DataOutputStream dos = new DataOutputStream(new FileOutputStream("c:\\data.txt"));//写入int基本类型,调用方法 writeInt()dos.writeInt(97);dos.writeInt(98);dos.writeInt(99);dos.writeInt(-1);dos.close();}}

二、内存操作流



操作字节数组
ByteArrayInputStream
ByteArrayOutputStream


操作字符数组
CharArrayReader
CharArrayWrite


操作字符串
StringReader
StringWriter




特点:


   JAVA中内存操作流对象
操作字节数组的流对象
ByteArrayInputStream 将字节数组,从内存中读取出来
ByteArrayOutputStream 将字节数组,写入内存中

内存流共性,只操作内存,其余存储容器不操作,不会占用操作系统资源
流对象,不需要关闭


示例:

//写数据public static void method_2()throws IOException{ByteArrayOutputStream bos = new ByteArrayOutputStream();for(int x = 0 ; x < 5 ;x++){bos.write(("hello "+x).getBytes());}//读取字节数组,数据源,ByteArrayOutputStream写进去的字节数组ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());int len = 0 ;while((len = bis.read())!=-1){System.out.print((char)len);}}/* * ByteArrayInputStream 读取字节数组 * 构造方法,传递字节数组 */public static void method_1(){//创建字节数组输入流,构造方法,直接传递字节数组ByteArrayInputStream bis = new ByteArrayInputStream("abcd".getBytes());//读取read()int len = 0 ;while((len = bis.read())!=-1){System.out.println(len);}}

三、打印流


打印流概述

字节流打印流
字符打印流


打印流特点
只能操作目的地,不能操作数据。
可以操作任意类型的数据。
如果启动了自动刷新,能够自动刷新。
可以操作文件的流


构造方法:
1)PrintStream 打印流的数据目的,数据目的都会放在构造方法中
构造方法中接收的数据目的三种 ,File对象,OutputStream,String文件名
      
2)PrintWriter 打印流的数据目的,放在构造方法中
构造方法中接收的数据目的四种,File对象,OutputStream,String文件名,Writer


示例:

/* * 打印流复制文本文件 * 打印流PrintWriter,写入数据目的 * 读取数据源,readLine */import java.io.*;public class PrintWriterCopyText {public static void main(String[] args) throws IOException{BufferedReader bfr = new BufferedReader(new FileReader("c:\\1.txt"));PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"),true);//读取一行,打印一行,不需要打换行 printlnString line = null;while((line = bfr.readLine())!=null){pw.println(line);}pw.close();bfr.close();}}示例/* * 打印流向数据目的中打印数据 */public class PrintWriterDemo {public static void main(String[] args) throws IOException{method_3();PrintWriter pw = new PrintWriter(new FileWriter(new File("c:\\1.txt")),true);}/* * 打印流向字符输出流中打印数据 */public static void method_3()throws IOException{//创建打印流对象,传递字符输出流PrintWriter pw = new PrintWriter(new FileWriter("c:\\4.txt"),true);pw.println("abc");pw.println("bcd");pw.close();}/* * 打印流向字节输出流中打印数据 */public static void method_2()throws IOException{//创建打印流对象,传递字节输出流PrintWriter pw = new PrintWriter(new FileOutputStream("c:\\3.txt"),true);pw.println("haah");pw.println("heihei");pw.println("xixix");pw.println("hehe");pw.close();}/* * 打印流向字符串的文件名中,打印数据 */public static void method_1()throws IOException{//创建打印流对象,写字符串文件名PrintWriter pw = new PrintWriter("c:\\2.txt");pw.println("第一行");pw.flush();pw.println("第二行");pw.flush();pw.close();}/* * 打印流向File包装的文件中打印数据 */public static void method()throws IOException{File file = new File("c:\\1.txt");//创建打印流对象,构造方法传递File对象PrintWriter pw = new PrintWriter(file);pw.println("abcdef");pw.flush();pw.close();}}

四、标准输入输出流


System类中的字段:in,out。
它们各代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
System.in的类型是InputStream.
System.out的类型是PrintStream是OutputStream的子类FilterOutputStream 的子类.


示例:

/*标准输入流 * public static final InputStream in * System.in 运算结果字节输入流 -- BufferedInputStream * 输入流可以对应于键盘,对应于文件,由用户指定另一个输入源 */import java.io.*;public class SystemInDemo {public static void main(String[] args) throws IOException{//System.in 返回InputStream的子类的对象InputStream in = System.in;//InputStream read方法读取int len = 0 ;StringBuilder builder = new StringBuilder();while((len = in.read())!=-1){if(len == '\r')continue;if(len == '\n'){//判断用户输入的是不是over//将缓冲区变成String对象,equalsif(builder.toString().equals("over"))break;System.out.println(builder);//清空缓冲区上一次存储的内容builder.delete(0,builder.length());}else{builder.append((char)len);}}in.close();}}

标准输出流
   public static final PrintStream out
   System.out 运算结果字节输入流 打印流
   准备开始输出了,数据目的,控制台,显示器,用户指定另一个输出的目的


示例:

public class SystemOutDemo {public static void main(String[] args)throws IOException {//获取标准输出流OutputStream out = System.out;//创建转换流对象,封装字节输出流,标准输出流OutputStreamWriter osw = new OutputStreamWriter(out);//创建字符输出流缓冲区对象,包装转换流BufferedWriter bfw = new BufferedWriter(osw);bfw.write("abcd");bfw.newLine();bfw.flush();bfw.write("12345");bfw.newLine();bfw.flush();bfw.close();}} 

五、随机访问流


1)RandomAccessFile概述



   RandomAccessFile类不属于流,是Object类的子类。
但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。


2)构造方法:


    传递File对象,String mode
    传递String文件名,String mode


3)成员方法
void seek(long l)开头测量的文件指针,
读写发生在设置的索引的下一个字节处


示例:

/* * 随机读写流,写文件 * 进行随机操作 */public static void write()throws IOException{//创建随机读写流对象,传递字符串文件名 rwRandomAccessFile raf = new RandomAccessFile("c:\\random.txt", "rw");//写 张三 65raf.write("张三".getBytes());raf.writeInt(65);//写李四 66raf.seek(16);raf.write("李四".getBytes());raf.writeInt(66);//写王五 67 , 写到李四的前面raf.seek(8);raf.write("王五".getBytes());raf.writeInt(67);raf.close();}}

六、序列化流


1)序列化流: ObjectOutputStream

构造方法:
ObjectOutputStream(OutputStream out) 传递字节输出流
FileOutputStream("文件名")


2)反序列化流: ObjectInputStream


构造方法:
ObjectInputStream(InputStream in)传递字节输入流
FileInputStream("文件名")
示例:

/*     *   Object readObject(); 如果没有对应的class文件,出现类找不到异常*/import java.io.*;public class ObjectStreamDemo {public static void main(String[] args)throws IOException , ClassNotFoundException{readObj();}/* * jiang person.txt中的对象进行反序列化 */public static void readObj()throws IOException, ClassNotFoundException{//创建读取对象流对象,传递字节输入流,包装文件ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:\\person.txt"));//调用读取对象的方法 readObject()Object obj = ois.readObject();System.out.println(obj);ois.close();}

应用示例:

/* * 将Person对象写到文件中,序列化 */public static void writeObj()throws IOException{//创建对象的序列化流,传递字节输出流,包装一个文件ObjectOutputStream oos = newObjectOutputStream(new FileOutputStream("c:\\person.txt"));Person p = new Person("zhangsan",33);//调用写对象的方法 writeObjectoos.writeObject(p);oos.close();}}/* * Person不具备序列化能力 * 类通过实现 java.io.Serializable 接口以启用其序列化功能 */public class Person implements Serializable{private String name;private int age;public Person(String name,int age){this.name = name;this.age = age;}public String toString(){return "Person " + name + "..." + age;}}


0 0