File ,Io流的简单操作以及上传下载

关于字节流,字符流,缓冲流对文件的读入和写出的基本操作。

   /**
     * 字节流的读入,写出
     * FileInputStream
     * FileOutputStream
     *
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
        String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
        File file = new File(path);
        if (file.exists()) {
            FileInputStream stream = new FileInputStream(file);
            //stream.read()读取字节的二进制,返回十进制的整数
            byte[] bytes = new byte[1024];
            int by = 0;
            FileOutputStream outputStream = new FileOutputStream(path2, true);//true为追加
            while ((by = stream.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, by));
                outputStream.write(bytes, 0, by);//写入文件
                String sh = "哈哈哈---新写入的文件";
                outputStream.write(sh.getBytes());
            }
            outputStream.close();
            stream.close();//关闭资源
        }
    }

    /**
     * FileReader
     * FileWriter
     * 字符流的读入写出
     * 和单纯的字节流的读入写出,大同小异
     */
    @Test
    public void test02() throws IOException {
        String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
        String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
        Reader reader = new FileReader(path);
        Writer writer = new FileWriter(path2, true);//自动添加
        char[] chars = new char[1024];
        int ch = 0;
        while ((ch = reader.read(chars)) != -1) {
            System.out.println(new String(chars, 0, ch));//读出文件。
            writer.write(chars, 0, ch);//写出文件
        }

        writer.close();//关闭资源
        reader.close();//关闭资源
    }

    /**
     * BufferedInputStream
     * BufferedOutputStream
     * 字节流的读入和写出
     *
     * @throws IOException
     */
    @Test
    public void test03() throws IOException {
        String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
        String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
        BufferedInputStream bi = new BufferedInputStream(new FileInputStream(path));//字节缓冲读入流
        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(path2, true));//字节缓冲写出流
        byte[] bytes = new byte[1024];
        int b = 0;
        while ((b = bi.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, b));
            bo.write(bytes, 0, b);//字节缓冲流写出。------
            bo.write(new String(bytes, 0, b).getBytes());//----
        }
        //刚才没写就导致了写出的内容为空。
        bo.flush();//有可能缓冲区没满,强制写出
        bo.close();
        bi.close();
    }

    /**
     * BufferedReader
     * BufferedWriter
     * 关于字符缓冲流的输入和输出。
     *
     * @throws IOException
     */
    @Test
    public void test04() throws IOException {
        String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
        String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";

        /**
         * 读取一行数据,不会有换行符,需要手动添加换行符
         */
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));//字符读入缓冲流
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path2, true));
        String input = "";
        while ((input = bufferedReader.readLine()) != null) {
            System.out.print(input);
            System.out.println("\n");//这样子读出有了换行。
            bufferedWriter.write(input);
            bufferedWriter.write("\n");//这样子读出就有了换行
        }
        bufferedWriter.flush();//刷新缓冲区
        bufferedWriter.close();
        bufferedReader.close();
    }

主要应用:下载和预览

其中下载和预览的主要区别是:

下载有response的响应头设置:

                response.setContentType("application/force-download");// 设置强制下载不打开
               //设置相应头
                response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());

要特别注意:这里的OutputStream是response获取的,直接new是不行的。

    }
     /**
     * 展示文件,主要是先读取,再输出
     * @param path
     * @param response
     */
    @RequestMapping(value = "/showFile")
    @ResponseBody
    public void showFile(
            @RequestParam(name = "path",required = false) String path, HttpServletResponse response,
            HttpServletRequest request) {
        System.out.println(path);
        //header中的这个*指所有域名,参数指的是域名。。可以指定允许特定的域名
        response.setHeader("Access-Control-Allow-Origin", "*");
        File file = new File(path);
        System.out.println("file"+file.exists());
        System.out.println("----------");
        if (file.exists()) {
            System.out.println("文件存在");
            response.setContentType("application/pdf");
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                byte[] buffer = new byte[fis.available()];
                bis = new BufferedInputStream(fis);
                response.reset(); // 非常重要
                OutputStream outputStream = response.getOutputStream();
                int i=0;
                while ( (i=bis.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, i);
                }
                outputStream.flush();
                bis.close();
                outputStream.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            System.out.println("文件不存在---------------------------");
        }

    }


    /**
     * 下载文件
     * @param path
     * @param response
     */
    @RequestMapping(value = "/downLoadFile")
    public void downLoadFile(@RequestParam(name = "path") String path, HttpServletResponse response){
        File file = new File(path);
        if (file.exists()) {
            byte[] buffer = new byte[2048];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                response.setContentType("application/force-download");// 设置强制下载不打开
               //设置相应头
                response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream outputStream = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    outputStream.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                bis.close();
                outputStream.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

无偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值