练习25——IO流的所有用法(一)


import org.junit.Test;

import java.io.*;
import java.util.Date;

/**
 * @author abu
 * @create 2019/7/27
 * @since 1.0.0
 */
public class TestStream {
    @Test
    //使用字符流实现输出
    public void test1(){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("user1"));
            String str = "在某些情况下,一个客户不想或者不能直接引用另一个对象,\n" +
                    "而代理对象可以在客户端和目标对象之间起到中介的作用。\n" +
                    "例如权限原因导致客户端对象不能直接访问目标对象,此时可以通过中介来架起两者的桥梁。\n" +
                    "代理模式的作用是为其它对象提供一种代理去控制或访问目标对象.\n";
            bw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
    @Test
    //使用字节流实现输出
    public void test2(){
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(new File("user.txt")));
            String str = "在某些情况下,一个客户不想或者不能直接引用另一个对象,\n" +
                    "而代理对象可以在客户端和目标对象之间起到中介的作用。\n" +
                    "例如权限原因导致客户端对象不能直接访问目标对象,此时可以通过中介来架起两者的桥梁。\n" +
                    "代理模式的作用是为其它对象提供一种代理去控制或访问目标对象.\n";
            bos.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null){
                try {
                    bos.close();
                }catch (IOException e) {
                    e.printStackTrace();
            }
            }
        }
    }

    public void test(){
        //将输入的字符串都变成大写,当输入"e"、"exit"时候退出
        InputStream is = System.in;
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String str;
        try {
            System.out.println("请输入字符串呀!!!");
            while(true){
                str = br.readLine();
                if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
                    break;
                }
                System.out.println(str.toUpperCase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    //File有关方法1
    public void test3(){
        File file1 = new File("D:\\movie\\尚硅谷20天java核心技术教程\\尚硅谷_宋红康_JavaSEcode\\day15\\dbcp.txt");
        File file2 = new File("user1");
        File file3 = new File("test.txt");

        System.out.println(file1.getName());
        System.out.println(file1.getPath());
        System.out.println(file1.getAbsoluteFile());
        System.out.println(file1.getAbsoluteFile());
        System.out.println(file1.getParent());

        System.out.println(file1.renameTo(file2));
        System.out.println(file2.renameTo(file3));

        System.out.println();

        System.out.println(file2.exists());
        System.out.println(file3.exists());
        System.out.println(file3.canWrite());
        System.out.println(file3.canRead());
        System.out.println(file3.isFile());
        System.out.println(file3.isDirectory());
        System.out.println(new Date(file3.lastModified()));
        System.out.println(file3.length());

    }
    //File有关方法2
    @Test
    public void test4(){
        File file1 = new File("test.txt");
        System.out.println(file1.delete());

        if( !file1.exists()){
            try {
                System.out.println(file1.createNewFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //mkDir():创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true
        //mkDirs():创建一个文件目录。若上层文件目录不存在,一并创建
        File file2 = new File("D:\\movie\\尚硅谷20天java核心技术教程\\尚硅谷_宋红康_JavaSEcode\\day15\\dbcp.txt");
        if (!file2.exists()){
            System.out.println(file2.mkdirs());
        }
        //遍历文件夹内文件名,list()
        File file3 = new File("D:\\movie\\尚硅谷20天java核心技术教程\\尚硅谷_宋红康_JavaSEcode\\day15");
        String[] str1 = file3.list();
        for(int i = 0; i < str1.length; i ++){
            System.out.println(str1[i]);
        }

        File[] files = file3.listFiles();
        for(int i = 0; i < files.length; i ++){
            System.out.println(files[i]);
        }
    }
    @Test
    public void test5(){
        long start = System.currentTimeMillis();
        String src = "user.txt";
        String dest = "user1.txt";
        copyFile(src,dest);
        long end = System.currentTimeMillis();
        System.out.println("复制文件花了多久呀~~~  " + (end - start));

    }
    //实现文件复制的方法
    public void copyFile(String src, String dest){
        //1、提供读入写出文件
        File file1 = new File(src);
        File file2 = new File(dest);
        //2、提供相应的流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = null;
            fos = null;
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //3、实现文件的复制
            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1){
                fos.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test6(){
        //1、创建File对象
        FileOutputStream fos = null;
        try {
            File file1 = new File("user2.txt");
            //2、创建FileOutputStream对象
            fos = new FileOutputStream(file1);
            //3、写入的操作
            fos.write(new String("you big pig").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭输出流
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    @Test
    public void test7(){
        FileInputStream fis = null;
        try {
            File file = new File("user1.txt");
            fis = new FileInputStream(file);
            /*int b;
            while ((b = fis.read()) != -1){
                System.out.println((char)b);
            }*/
            byte[] b = new byte[20];
            int len;
            while((len = fis.read(b)) != -1){
                System.out.println(new String(b,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    //什么格式都可以读
    public void test8(){
        FileReader fr = null;
        try {
            File file = new File("user1.txt");
            fr = new FileReader(file);
            char[] c = new char[20];
            int len;
            while ((len = fr.read(c)) != -1 ){
                System.out.println(new String(c,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test9(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //解码
            File file1 = new File("user2.txt");
            FileInputStream fis = new FileInputStream(file1);
            InputStreamReader isr = new InputStreamReader(fis,"GBK");
            br = new BufferedReader(isr);
            //编码
            File file2 = new File("user3.txt");
            FileOutputStream fos = new FileOutputStream(file2);
            OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
            bw = new BufferedWriter(osw);

            String str;
            while ((str = br.readLine()) != null){
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } 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();
                }
            }
        }
    }
    @Test
    public void test10(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            File file1 = new File("user1.txt");
            File file2 = new File("user3.txt");
            FileReader fr = new FileReader(file1);
            FileWriter fw = new FileWriter(file2);
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

            /*char[] c = new char[20];
            int len;
            while ((len = br.read(c)) != -1 ){
                System.out.println(new String(c,0,len));
            }*/

            String str;
            while ((str = br.readLine()) != null){
                System.out.println(str);
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值