java IO流知识梳理

IO就是对文件的读和写

I是读取文件到内存

O是写出内存文件

字节输入流(InputStream):抽象类

     常用子类:  FileInputStream   

      构造方法:

                        public  FileInputStream(String path);

                         public  FileInputStream(File  path);

    常用方法:

                    int  read( );  //读取一个字节的数据,如果没有数据返回-1;

                    int  read( );  //读取数据存入byte数据中,返回读取长度没有数据返回-1;

                    void  close( ); //释放

public static void main(String[] args) throws IOException  {
        InputStream is = new FileInputStream("a.txt");
        int i = is.read();
        System.out.println(i);
        //循环读取(方法一)
        int data;
        while((data=is.read())!=-1) {
            System.out.println(data);
        }
        //循环读取(方法二)
        while(true) {
            int data=is.read();
            if(data==-1) {
                break;
            }
            System.out.println(data);
        }
        is.close();
    }


   BufferedInputStream:缓冲字节输入流

          构造方法:

                      //调用下面构造方法的时候,底层会创建一个长度是8192的byte数组

                      //调用读取方法的时候,每次读取8K的数据,每次操作的字节就是从该数组的对应位                           置获取的。

                      //使用了缓冲区减少了读取的次数

                        public BufferedInputStream(InputStream is);

                     //调用下面的构造方法,底层会创建一个长度是size的byte数组

                          public BufferedInputStream(InputStream is,int size);

public static void main(String[] args) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream("info.txt"));
        int data;
        while((data=is.read())!=-1) {
            System.out.print((char)data);
        }
        is.close();
    }


     ObjectInputStream:(反序列化)

              构造方法:

                                public ObjectInputStream(InputStream is);

              成员方法:

                                  Object readObject();

             注意事项: 

                        a.序列化后,类文件结构不能改变,如果改变了反序列化就会报错。

                         如果不能保证不修改类文件结构,则可以在该类中添加一个成员变量(常量):

                                serialVersionUID

public static void main(String[] args) throws IOException, ClassNotFoundException  {
        Person p = new Person("张良",20);
        p.car = new Car();//给成员变量car赋值
        p.m = new Money();
 
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("p.txt"));
        Object obj = ois.readObject();
        Person p2 = (Person)obj;
        System.out.println(p2);
        ois.close();
        System.out.println(p2==p);//反序列化回来后,会重新构建一个对象
}
 
class Person implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    public Car car;
    public transient Money m;
    
    public Person() {
        super();
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", car=" + car + ", m=" + m + "]";
    }
}


 字节输出流(OutputStream):抽象类   

       常用子类:    FileOutputStream;

       构造方法:     

                         public  FileOutputStream(String path);

                         public  FileOutputStream(File  path);

                        public  FileOutputStream(String path , boolean append);

                         public  FileOutputStream(File  path , boolean  append);

        成员方法:

                           void close();  //释放流

                           void flush();

                           void write(int data);  //写一个字节的数据

                          void write(byte[] b);

                          void write(byte[] b,int offset,int len);

     

  OutputStream os = new FileOutputStream("a.txt");
        os.write(98);
        byte[] b = {97,98,99,100,101};
        os.write(b);
        os.write(b, 0, 3);
        os.write("我是中国人,我深情的爱着我的祖国和人民!!!".getBytes());
        os.close();
        System.out.println("数据写出完毕....");


BufferedOutputStream:缓冲字节输出流

           构造方法:

                            //调用这个构造方法,底层会创建一个长度是8192的byte数组

                            //写出的数据,存入缓冲区(8192的byte数组)中

                            //存满了,或者显示的调用flush(close)方法,会把缓冲区中的数据写出去

                           //有了缓冲区减少了写出的次数

                                public BufferedOutputStream(OutputStream os);

                           //调用这个构造方法,底层会创建一个长度是size的byte数组

                                public BufferedOutputStream(OutputStream os,int size);

public static void main(String[] args) throws IOException {
        OutputStream os = new BufferedOutputStream(new 
          FileOutputStream("a.txt",true),1024*10);
        os.write("测试缓冲字节输出流".getBytes());
        os.flush();
        os.close();
    }
ObjectOutputStream:(序列化)

                        构造方法:

                                        public ObjectOutputStream(OutputStream os);

                        成员方法:

                                        void writeObject(Object obj);

                        注意事项:                  

                        a.序列化的对象必须是Serializable类型

                         b.成员变量的类型也必须是Serializable类型

                         c.使用transient关键字修饰的成员变量不进行序列化        

public static void main(String[] args) throws IOException, ClassNotFoundException  {
        Person p = new Person("张良",20);
        p.car = new Car();//给成员变量car赋值
        p.m = new Money();
 
       ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("p.txt"));
        oos.writeObject(p);
        oos.close();
        System.out.println("序列化成功...");
}
 
class Person implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    public Car car;
    public transient Money m;
    
    public Person() {
        super();
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", car=" + car + ", m=" + m + "]";
    }
}


  字符输入流(Reader)

                  常用方法:

                                          int read(); //读取一个字符数据

                                          int read(char[] ch);  //读取数据存入char数组中,返回读取的长度

                                          void close();

public static void main(String[] args) throws IOException {
        Reader r = new FileReader("b.txt");
        int data = r.read();//读取一个字符的数据
        System.out.println((char)data);
 
        char[] ch = new char[3];
        r.read(ch);
        System.out.println(ch);
}


常用子类: FileReader

               构造方法:

                                //减少读取的次数,提高效率。数组大小不能改变。

                                //调用下列构造方法的时候,底层会创建一个长度是8192的byte数组。

                                //每次调用读取方法,至少读取8192多字节的数据,存入该byte数组中。

                                //把byte数组中对应位置的元素,转换成char类型的值。

                                        public FileReader(String path);

                                        public FileReader(File path);

     BufferedReader:

              构造方法;

                        //调用下面构造方法的时候,底层会创建长度是8192的char数组

                        //每次调用读取方法,读8192byte的数据存入byte数组中

                        //把byte数组中的数据转换存入char数组中,直到char数组存满

                        //我们操作的数据是从char数组中对应位置获取//和FileReader比较,读取次数是 一样的。从byte转换为char转换次数不一样

                           public BufferedReader(Reader r);

                          //调用下面构造方法的时候,底层会创建一个长度是size的char数组

                            public BufferedReader(Reader r,int size);

             成员方法:

                                  String readLine();//如果没有数据则返回null

public static void main(String[] args) throws IOException {
        Reader r = new BufferedReader(new FileReader("b.txt"));
        
        r.close();
    }


      InputStreamReader:

                          构造方法:

                                        public InputStreamReader(InputStream is);

                                         public InputStreamReader(InputStream is,String charsetName);

public static void main(String[] args) throws IOException {
        //转换输出流
        Reader r = new InputStreamReader(new FileInputStream("c.txt"),"UTF-8");
        int data = r.read();
        System.out.println((char)data);
        r.close();
}


字符输出流(Writer):

           成员方法:

                           void write(char[] ch);  //写出char数组数据

                           void write(char[] ch,int offset,int len);

                           void write(int data); //写出一个字符的数据

                           void flush();  //刷新缓冲区,不关闭流,还可以传入数据

                           void write(String str);

                           void write(String str,int offset,int len);

                           void close();  //关闭流,刷新缓冲区

public static void main(String[] args) throws IOException {
        Writer w = new FileWriter("b.txt",true);
        
        w.write("\n");
        //获取操作系统对应的换行符
        String lineSeparator = System.lineSeparator();
        w.write(lineSeparator);
        w.write(98);
 
        w.write(new char[] {'我','爱','你','中','国'});
 
        char[] ch = new char[] {'我','爱','你','中','国'};
        w.write(ch,3,2);
 
        w.write("我爱你中国");
        w.write("我爱你中国",0,3);
 
        w.close();
        System.out.println("操作完毕...");
    }


常用子类:FileWriter

     构造方法:

            //调用下列构造方法,底层会创建一个长度是8192的byte数组

           //写出的字符转换成字节,存入该缓冲区(8192的byte数组)中

           //缓冲区中的数据存不下了,或者调用flush(close)方法,会强制把缓冲区中的 数 据 写 出去。

             public FileWriter(String path);

             public FileWriter(File path);

             public FileWriter(String path,boolean b);

             public FileWriter(File path,boolean b);

Properties是Hashtable的子类:

                   所属包:

                                        java.util;

                  构造方法:

                                        public Properties();

                 成员方法:

                                     Object setProperty(String key,String value);

                                      String getProperty(String key);

                                      Set<String> stringPropertyNames();

public static void main(String[] args) {
        Properties p = new Properties();
        p.setProperty("韩国", "首尔");
        Object obj = p.setProperty("中国", "北京");
        System.out.println(obj);
        obj = p.setProperty("中国", "西安");
        System.out.println(obj);
        System.out.println(p);
        
        String value = p.getProperty("中国");
        System.out.println(value);
        
        Set<String> keys = p.stringPropertyNames();
        System.out.println(keys);
    }
                         void store(OutputStream os,String comments);

                           void store(Writer w,String comments);

public static void main(String[] args) throws IOException {
        Properties p = new Properties();
        p.setProperty("韩国", "首尔");
        p.setProperty("中国", "北京");
        
        Writer w = new FileWriter("info.txt");
        p.store(w, "test Properties writer");//把Properties对象中的数据,以某种格式写出到info.txt文件中去了
        w.close();
        System.out.println("操作完毕....");
    }


BufferedWriter:

    构造方法:

       //调用下面构造方法的时候,底层会创建一个长度是8192的char数组

       //写出数据线存入8192的char数组,直到char数组存满了,或者显示的调用flush方法

       //会把char数组中的内容转换存入到byte数组中,再把byte数组中的数据写出去。

      //和直接使用FileWriter的区别在于,字符转换字节的次数(效率)不相同,写出次数是相同的。

                  public BufferedWriter(Writer w);

       //调用下面构造方法的时候,底层会创建一个长度是size的char数组

                public BufferedWriter(Writer w,int size);

     成员方法:

                        void newLine();//写出一个换行

public static void main(String[] args) throws IOException {
        Writer w = new BufferedWriter(new FileWriter("b.txt"));
        w.write("测试缓冲字符输出流");
        w.close();
    }


OutputStreamWriter:

                构造方法:

                                 public OutputStreamWriter(OutputStream os);

                                  public OutputStreamWriter(OutputStream os,String charsetName);

public static void main(String[] args) throws IOException {
        //转换输出流
        Writer w = new OutputStreamWriter(new FileOutputStream("c.txt"),"UTF-8");
        w.write("我爱你中国");
        w.close();
}


PrintStream:打印流

     成员方法:

                          void println();

                           void print(参数);

      构造方法:

                         public PrintStream(String pathname);

                        public PrintStream(File pathname);

public static void main(String[] args) {
        System.out.println();
        
        //正常结构。默认获取的打印流对象,流向到控制台
        PrintStream xx = System.out;
        xx.println();
        
        //匿名对象
        System.out.println();
    }


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值