简单的IO流

IO流

1.字符流的写入和读出

  /**
     * 写入后读出
     */
    @Test
    public void test11(){
        try {
            FileWriter fw = new FileWriter(new File("aa.txt"));
            fw.write("中国人真争气");
            if (fw!=null)
            {

                fw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            FileReader fr = new FileReader(new File("aa.txt"));
            char[] chars = new char[1024];
            int let;
            while ((let=fr.read(chars))!=-1)
            {
                String s = new String(chars, 0, let);
                System.out.print(s);
            }
            if (fr!=null)
            {
                fr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

2.字节流的读取和写入

    /**
     * 复制图片
     *
     */
    @Test
    public  void test13()
    {
        try {
            FileInputStream fis = new FileInputStream(new File("trump.gif"));
            FileOutputStream fos = new FileOutputStream(new File("taa.gif"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1)
            {
                fos.write(bytes,0,len);
            }
            if(fis!=null&&fos!=null)
            {
                fis.close();
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.流的转换


/**转换流
 * InputStreamReader 字节输入流-》字符输入流
 * OutputStreamWriter 字符输出流-》字节输出流
 * 属于字符流
 * 作用:进行字节流和字符流的转换
 * @author lushixiong
 * @create 2020--07--03-10:03
 */
public class IOStreamTest4 {
    @Test
    public void test1()
    {
        FileInputStream fi = null;
        InputStreamReader ir = null;
        try {
            fi = new FileInputStream("hello2.txt");
            ir = new InputStreamReader(fi, "GBK");
            char[] chars = new char[5];
            int len;
            while ((len=ir.read(chars))!=-1)
            {
                String s = new String(chars, 0, len);
                System.out.print(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fi.close();
                ir.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

4.使用对象流进行序列化

package IOStream;

import org.junit.Test;

import java.io.*;

/**对象流
 * @author lushixiong
 * @create 2020--07--03-11:05
 */
public class IOStreamTest6 {
    //序列化过程
    @Test
    public void test1()
    {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("objec.txt"));
            oos.writeObject(new String("我爱美国天安门"));
            oos.flush();//刷新
            oos.writeObject(new Person("王五",12));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos!=null)
            {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    //反序列化过程
    @Test
    public void test2()
    {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("objec.txt"));

            Object obj = ois.readObject();
            String str = (String) obj;

            Person p = (Person) ois.readObject();


            System.out.println(str);
            System.out.println(p);


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}

package IOStream;

import java.io.Serializable;

/**Person需要满足如下的要求,方可序列化
 * 1.需要实现接口:Serializable
 * 2.当前类提供一个全局常量:serialVersionUID
 * 3.除了当前Person类需要实现Serializable接口之外,还必须保证其内部所有属性
 *   也必须是可序列化的。(默认情况下,基本数据类型可序列化)
 *
 *
 * 补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
 * @author lushixiong
 * @create 2020--07--03-14:32
 */
public class Person implements Serializable {
    private String name;
    private int age;
    public static final long serialVersionUID = 42778390854939L;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person() {
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值