io流常用操作记录

io流常用操作记录

package atguigu.io;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author gmq
 * @Description
 * @date 2020/9/23
 */
public class Tyg {

    //用字节流来读,中文出现乱码.
    @Test
    public void main12() throws Exception {
        File file = new File("D:\\aaa\\base\\Bootstrap (2).yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件已经存在-----");
            FileInputStream fileInputStream = new FileInputStream(file);
            int read = fileInputStream.read();
            while (read > 0) {
                System.out.println(read + "--" + (char) read);
                read = fileInputStream.read();
            }
        }
    }

    // 读文件, 中文乱码.
    @Test
    public void main13() throws Exception {
        File file = new File("D:\\aaa\\base\\333bootstrap.yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            byte[] bytes = new byte[10];//1kb
            FileInputStream fileInputStream = new FileInputStream(file);
            int read = fileInputStream.read(bytes);
            while (read > 0) {
                read = fileInputStream.read(bytes);
                String s = new String(bytes);
                System.out.print(s);
                // System.out.println(read);
            }
        }
    }

    //1.标准写数据到文件, 写入的字符需要自行转码.
    @Test
    public void main234() throws Exception {
        File file = new File("D:\\aaa\\base\\333bootstrap.yml");
        // BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new
        // FileOutputStream(file, true), "UTF-8"));
        // FileWriter可以大幅度简化代码
        BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
        // 要写入的字符串
        String string = "松下问童子,言师采药去。只在此山中,云深不知处。";
        bw.newLine();
        bw.write(string);
        bw.close();
    }

    //2.标准写数据到文件, FileOutputStream设置的格式好像没有起作用, 还是看具体的内容是什么格式的.
    @Test
    public void main247() throws Exception {
        File file = new File("D:\\aaa\\base\\333bootstrap.yml");
        BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8));
        // FileWriter可以大幅度简化代码
        //BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
        // 要写入的字符串
        String string = "33333311突突突额额哦哦哦哦哦哦。";
        fw.newLine();
        fw.write(string);
        fw.close();
    }


    //正常的读文件方式, 代码混乱.
    @Test
    public void main16() {
        File file = new File("D:\\aaa\\base\\333bootstrap.yml");
        if (!file.exists()) {
            try {
                boolean newFile = file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            FileInputStream fi = null;
            InputStreamReader input = null;
            BufferedReader bufferedReader = null;
            try {
                fi = new FileInputStream(file);
                //原始文件utf-8, 用gbk读, 乱码
                //InputStreamReader input = new InputStreamReader(fi, "gbk");
                input = new InputStreamReader(fi, StandardCharsets.UTF_8);
                bufferedReader = new BufferedReader(input);
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("异常111------");
            } finally {
                try {
                    fi.close();
                    input.close();
                    bufferedReader.close();
                    System.out.println("关完了------");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("异常222------");
                }
            }
        }
    }


    @Test // 写, 没有关闭流.文件没有写入.
    public void main17() throws Exception {
        File file = new File("D:\\aaa\\base\\bootstrap.yml");
        File file1 = new File("D:\\aaa\\base\\333bootstrap.yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件已存在");
            try {
//                使用InputStreamReader
                FileInputStream fileInputStream = new FileInputStream(file);
                //获取字符输入流
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                FileOutputStream fileOutputStream = new FileOutputStream(file1);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                    bufferedWriter.write(line);
                    bufferedWriter.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读文件的各种方法.
     * @throws Exception
     */
    @Test
    public void main19() throws Exception {
        //FileInputStream ww = new FileInputStream("D:\\aaa\\base\\bootstrap.yml");
        Path path = Paths.get("D:\\aaa\\batch\\20200921\\uuuu.TXT");

        //------------------------------------
        //第一种方法:
        InputStream inputStream = Files.newInputStream(path);
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        inputStream.close();
        in.close();

        System.out.println("------1---------");
        //第二种方法:
        List<String> ad = Files.readAllLines(path, StandardCharsets.UTF_8);
        for (String ss : ad) {
            System.out.println(ss);
        }

        // 第3种方法:以下是流:
        System.out.println("------以下是流---------");
        Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
        lines.forEach((v) -> {
            System.out.println(v.length());
            System.out.println(v);
        });
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值