节点流

io使用FileInputStream类读取文件内容

package com.demo.demo;

import java.io.*;

/**
 * @author WenLiang
 * @return 1.创建源 2.选择流 3.相应的操作 4.关闭流
 * @create 2020-06-07 16:53
 */
public class TestIO {
    public static void main(String[] args) {
        File file = new File("D:/workspace/demo/target/a.txt");
        TestIO testIO = new TestIO();
//        testIO.demo01(file);
        testIO.demo02(file);
    }
    // 使用最基础的字节流读取文件内容,一个字节一个字节的读取
    public void demo01(File file){
        // 选择流
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            // 单个字节读取的操作
            int temp;
            // 循环读取内容,当内容不为空时输出
            while ((temp = fileInputStream.read()) != -1){
                System.out.println((char) temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 关闭流
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 使用最基础的字节流读取文件内容,多字节分段读取
    public void demo02(File file){
        // 选择流
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            // 分段读取的操作
            byte[] bytes = new byte[1024];//设置一个缓冲容器
            int size = -1;// 接收的长度
            // 循环读取内容,当内容不为空时输出
            while ((size = fileInputStream.read(bytes)) != -1){
                // 把读取的值赋值给str字符串,从第0个开始,到读取文件的长度(bytes字节数组需要解码)
                System.out.println(bytes);
                String str = new String(bytes, 0, size);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

io使用FileOutputStream类文件字节输出流

package com.demo.demo;

import java.io.*;

/**
 * @author WenLiang
 * @return 1.创建源 2。选择流 3.操作(写出内容) 4.释放资源
 * @create 2020-06-07 17:34
 */
public class TestIO02 {
    public static void main(String[] args) {
        // 1.创建源,如果没有则创建
        File file = new File("test.txt");
        TestIO02 testIO02 = new TestIO02();
        testIO02.demo01(file);
    }
    public void demo01(File file){
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
//            3.操作
            String msg = "hello world";
//            转换String为字节数组
            byte[] bytes = msg.getBytes();
            try {
                outputStream.write(bytes, 0, bytes.length);
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 

利用io流复制文件

package com.demo.demo;

import java.io.*;

/**
 * @author WenLiang
 * @return 文件的拷贝
 * @create 2020-06-07 17:54
 */
public class TestIO03 {
    public static void main(String[] args) {
        TestIO03 testIO03 = new TestIO03();
        testIO03.copy("test.txt", "test_copy.txt");
    }

    public void copy(String path,String newPath){
        File file = new File(path);
        File fileCopy = new File(newPath);
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileOutputStream = new FileOutputStream(fileCopy);
            byte[] bytes = new byte[1024];
            int size = -1;
            while ((size = fileInputStream.read(bytes)) != -1){
                fileOutputStream.write(bytes, 0, bytes.length);
                fileOutputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 

 
posted @ 2020-06-07 20:13  wen呀  阅读(14)  评论(0)    收藏  举报