Java IO流-节点流-文件字节流

Java IO流 -> 文件字节流

  • 以File、Array、String、Piped这类数据源名开头的一般是节点流,直接对接节点数据。
类型字符流字节流
FileFileReader FileWriterFileInputStream FileOutputStream
Memory ArrayCharArrayReader CharArrayWriterByteArrayInputStream ByteArrayOutputStream
Memory StringStringReader StringWriter——
Pipe(管道)PipedReader PipedWriterPipedInputStream PipedOutputStream
1. 文件字节流FileInputStream/FileOutputStream
  • FileInputStream:以字节方式读取文件,适合读取所有类型的文件(图像、视频等),全字符(纯文本)可以使用FileReader。
  • FileOutputStream : 通过字节方式写出或追加数据到文件,适合所有类型的文件(图像、视频等),全字符(纯文本)可以使用FileWriter。
import java.io.*;
/**
 * 测试文件字节输入流FileInputStream
 */
public class TestFileInputStream {
    public static void main(String[] args) {
        //1.指定节点
        File file = new File("D:\\JavaAno\\code\\JavaSE\\基础语法\\src\\com\\io\\endpoint\\in.txt");

        //2.创建流
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            //3.操作数据
            byte[] buffer = new byte[100];
            for(int len = -1; (len = in.read(buffer)) != -1;){
                System.out.println(new String(buffer, 0, len));
            }
//            int data;
//            while((data = in.read()) != -1){
//                System.out.print((char)data);
//            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
import java.io.*;
/**
 * 测试文件字节输出流FileOutputStream
 */
public class TestFileOutputStream {
    public static void main(String[] args) {
        //1.指定节点
        File file = new File("out.txt");
        //2.创建流
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            //3.操作数据
            byte[] buffer = "This is a new txt".getBytes();
            out.write(buffer,0, buffer.length);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2. 文件字节流封装FileUtil
import java.io.*;

public class FileUtil {
    private InputStream in;
    private OutputStream out;
    private int size = 1024;

    public FileUtil(){
    }

    public FileUtil(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    /**
     * 通过文件字节流实现文件的拷贝
     * @param srcFile
     * @param destFile
     */
    public void copy(String srcFile, String destFile){
        try{
            this.in = new FileInputStream(srcFile);
            this.out = new FileOutputStream(destFile);
            this.copy();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }

    /**
     * 字节数据流的拷贝
     */
    public void copy(){
        if(in == null || out == null){
            throw new RuntimeException("输入输出流不可以为空!");
        }
        try{
            byte[] buf = new byte[size];
            for(int len = -1; (len = in.read(buf)) != -1; len++){
                out.write(buf,0,len);
            }
            out.flush();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            this.close();
        }
    }

    /**
     * 资源释放
     */
    private void close(){
        try{
            if(out != null){
                out.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        try{
            if(in != null){
                in.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值