练习27——IO流的所有用法(二)


package test5;

import org.junit.Test;

import java.io.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/30
 * @since 1.0.0
 */
public class TestExer {
    public String nextString(){
        String str = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return str;
        }
    }
    public int nextInt(){
        return Integer.parseInt(nextString());
    }
    public boolean nextBoolean(){
        return Boolean.parseBoolean(nextString());
    }
    @Test
    public void test0(){
        TestExer te = new TestExer();
        System.out.println("请输入字符串");
        System.out.println(te.nextString());
        System.out.println("请输入布尔值");
        System.out.println(te.nextBoolean());
        System.out.println("请输入整数");
        System.out.println(te.nextInt() + 1);

    }
    @Test
    //使用字节流实现输出
    public void test1(){
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(new File("user4.txt")));
            String str = null;
            str = "山外青山楼外楼\n西湖歌舞几时休\n暖风熏得游人醉\n却把杭州作汴州";
            bos.write(str.getBytes());
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    //使用字符流实现输出
    public void test2(){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(new File("user5.txt")));
            String str = null;
            str = "少年握槊\n气凭陵\n酒圣诗豪余事";
            bw.write(str);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    //字符流实现输入
    public void test3(){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(new File("user4.txt")));
            String str = null;
            while ((str = br.readLine()) != null){
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    //字节流实现输入
    public void test4(){
        BufferedInputStream bis = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File("user4.txt"));
            bis = new BufferedInputStream(fis);

            int len;
            byte[] b = new byte[1024];
            while ((len = fis.read(b)) != -1){
                String str = new String(b,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    //文件的复制
    public void test5(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("user4.txt")));
            bw = new BufferedWriter(new FileWriter(new File("user6.txt")));
            char[] c = new char[20];
            int len;
            while((len = br.read(c)) != -1){
                bw.write(c,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2、对象输入输出

package test5;

import org.junit.Test;
import test2.Person;

import java.io.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/31
 * @since 1.0.0
 */
public class TestObjectInputOutStream {
    @Test
    public void testObjectOutputStream(){
        Person p1 = new Person("summer",18);
        Person p2 = new Person("forever",10);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File("person.txt")));
            oos.writeObject(p1);
            oos.flush();
            oos.writeObject(p2);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testObjectInputStream(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File("person.txt")));
            Person p1 = (Person)ois.readObject();
            System.out.println(p1);
            Person p2 = (Person)ois.readObject();
            System.out.println(p2);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值