ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java 处理流

2021-07-18 09:06:13  阅读:155  来源: 互联网

标签:src Java String 处理 destPath dest new 字节


处理流:

作用:增强功能、提供性能,节点流之上
1.缓冲流
输入流:BufferedInputStream(字节) BufferedReader(字符)

输出流:BufferedOutputStream(字节) BufferedWriter(字符)


2、转换流: 字节流 转为字符流   处理乱码(编码集、解码集)
1)、编码与解码概念
 编码: 字符  ---编码字符集>二进制
 解码:二进制  --解码字符集->      字符
2)、乱码:
(1)编码与解码的字符集不统一
(2)字节缺少,长度丢失
(3)文件乱码
InputStreamReader(字节输入流,"解码集")
OutputStreamWriter(字符输出流,"编码集")

一、缓冲流

【1】对I/O进行缓冲是一种常见的性能优化,缓冲流为I/O流增加了内存缓冲区,增加缓冲区的两个目的:
(1)允许Java的I/O一次不只操作一个字符,这样提高整个系统的性能;
(2)由于有缓冲区,使得在流上执行skip、mark和reset方法都成为可能。

【2】缓冲流:它是要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,
提高了读写的效率,同时增加了一些新的方法。例如:BufferedReader中的readLine方法,
BufferedWriter中的newLine方法。

1.字节缓冲流:

将InputStream和OutputStream用Buffered包装起来。

package SAMPLE.IO流;
import java.io.*;
/*字节流实现文件拷贝
   1、选择文件:源文件src,目标文件dest。
   2、选择流:
       src-->InputStream
       dest-->OutputStream
   3、操作(拷贝):
       数据以字节流byte[]形式从src经过流流入dest,拷贝完成。
   4、释放资源
 */
public class 字节流实现文件拷贝 {
    public static void main(String[] args) {
        String srcPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\justice.jpg";
        String destPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\111.jpg";
        copy(srcPath,destPath);
    }
    public static void copy(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);

        try(InputStream is = new BufferedInputStream(new FileInputStream(src));
            OutputStream os = new BufferedOutputStream(new FileOutputStream(dest,true))) {
            int len =-1;
            byte[] flush = new byte[1024];
            while((len=is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.字符缓冲流:

相比于字符流新增了readLine()、newLine()方法。

package A;
import java.io.*;
/*字节流实现文件拷贝
   1、选择文件:源文件src,目标文件dest。
   2、选择流:
       src-->Reader
       dest-->Writer
   3、操作(拷贝)
   4、释放资源
 */
public class 字符流实现文件拷贝 {
    public static void main(String[] args) {
        String srcPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\justice.jpg";
        String destPath = "C:\\Users\\kvnoe\\Desktop\\Jcase\\111.jpg";
        copy(srcPath,destPath);
    }
    public static void copy(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);
        try(Reader is = new BufferedReader(new FileReader(src));
            Writer os = new BufferedWriter(new FileWriter(dest,true))) {
            //新增操作方法
            String line =null;
            while(null!=(line= ((BufferedReader) is).readLine())){
                os.write(line);
                //wr.append("\r\n");
                ((BufferedWriter) os).newLine(); //换行符号
            }
            os.flush();//强制刷出
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:src,Java,String,处理,destPath,dest,new,字节
来源: https://blog.csdn.net/qq_57389269/article/details/118873007

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有