操作文件的流

Posted aikang525

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作文件的流相关的知识,希望对你有一定的参考价值。

package com.mepu;

import org.junit.Test;

import java.io.*;

/**
 * @author: 艾康
 * @date: 2019/12/31 17:03
 * @description: TODO
 * @modifiedBy:
 * @version: 1.0
 * io流
 */
public class IOTest {
    /*
    读入
     */
    @Test
    public void test1() {
        //获取到file
        File file = new File("Hello.txt");
        FileReader stream = null;
        try {
            //获取字符流对象
            stream = new FileReader(file);
            //使用char读取数据读取数据
            char[] cf = new char[1024];
            int len;
            while ((len = stream.read(cf)) != -1) {
                String str = new String(cf, 0, len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        //关闭流对象
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /*
    写出
     */
    @Test
    public void test2(){
        //创建file对象,如果创建对象不存在时会直接创建
        FileWriter fileWriter = null;
        try {
            File file = new File("Hello1.txt");
            //创建字符输出流,如果不传入第二次参数则默认为false则覆盖文件传入true时追加
            fileWriter = new FileWriter(file,true);
            //写入数据
            fileWriter.write("bye 2019 
");
            fileWriter.write("Hello 2020 
");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //字节流
    @Test
    public void test3(){
        FileInputStream inputStream = null;
        try {
            //创建file对象
            File file = new File("Hello.txt");
            //创建字节输入流
            inputStream = new FileInputStream(file);
            //读取数据
            byte[] buffer = new byte[5];
            int len;
            while ((len = inputStream.read(buffer)) !=-1 ){
                String s = new String(buffer, 0, len);
                System.out.print(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
    缓冲流
     */
    @Test
    public void test4(){
        BufferedInputStream stream = null;
        try {
            //创建文件对象
            File file = new File("Hello.txt");
            //创建字节流对象
            FileInputStream fileInputStream = new FileInputStream(file);
            //创建缓冲输入流对象
            stream = new BufferedInputStream(fileInputStream);
            byte[] bytes = new byte[5];
            int len;
            while ((len = stream.read(bytes)) != -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流对象
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
    转换流
     */
    @Test
    public void test5(){
        try {
            FileInputStream inputStream = new FileInputStream("Hello.txt");
            //设置字符集
            InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
            char[] c = new char[5];
            int len;
            while ((len = streamReader.read(c))!=-1){
                String s = new String(c, 0, len);
                System.out.println(s);
            }
            streamReader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

以上是关于操作文件的流的主要内容,如果未能解决你的问题,请参考以下文章

操作文件的流

GUN C中的流

文件的流操作和speex

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程