IO流学习笔记

1.创建文件

createNewFile() 只能创建文件,当testMkdir文件夹不存在时:Exception in thread "main" java.io.IOException: 系统找不到指定的路径

mkdir() 只能创建文件夹,且为单层文件夹,如"F:/testMkdir" , 若创建"F:/testMkdir/test001.txt"则不会创建,也不会异常

mkdirs() 也只能创建文件夹,但可以创建多层文件夹,如"F:/testMkdir/test001.txt",其中"test001.txt"是文件夹,不是文件

File file = new File("F:/testMkdir/test001.txt");
    if (!file.exists()){
        file.createNewFile();
    }

2.字节流

public static void main(String[] args) throws IOException {
        // 新建文件
        File file = new File("F:/test.txt");
        writeOutput(file);
        System.out.println(readInput(file));
    }


    public static void writeOutput(File file) throws IOException {
        // 创建输出流 把str输出到文件中
        //FileOutputStream os = new FileOutputStream(file,true);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));

        String str = "写入内容";
        bos.write(str.getBytes());
        // 关闭流
        bos.close();
    }


    public static String readInput(File file) throws IOException {
        // 创建输入流 将文件中的内容读入
        //FileInputStream is = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        // 一次读取多少个字节
        byte[] bytes = new byte[1024];
        // 用来接收读取到的字节
        StringBuffer sb = new StringBuffer();
        // 读取到的字节长度 为-1时表示没有数据了
        int length = 0;
        while ((length = bis.read(bytes)) != -1){
            // 将读取的内容转换成字符串
            sb.append(new String(bytes,0,length));
        }
        // 关闭流
        bis.close();
        return sb.toString();
    }

3.字符流

public static void main(String[] args) throws IOException {
        File file = new File("F:/text01.txt");
        write(file);
        System.out.println(read(file));
    }

    private static String read(File file) throws IOException {
        FileReader fr = new FileReader(file);

        char[] chars = new char[1024];
        StringBuffer sb = new StringBuffer();
        int length;
        while ((length = fr.read(chars)) != -1){
            sb.append(chars,0,length);
        }
        fr.close();
        return sb.toString();
    }


    private static void write(File file) throws IOException {
        FileWriter fw = new FileWriter(file, true);

        fw.write("第san次测试");
        fw.close();
    }

4.字符缓冲流

public static void main(String[] args) throws IOException {
        File file = new File("F:/text01.txt");
        write(file);
        System.out.println(bufferedRead(file));
    }

    private static String bufferedRead(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));

        StringBuffer sb = new StringBuffer();
        String line; // 按行读取数据
        while ((line = br.readLine()) != null){
            sb.append(line);
        }
        br.close();

        return sb.toString();
    }

    private static void write(File file) throws IOException {
        //FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));

        bw.write("字符缓冲流");
        bw.close();
    }

5.练习

先在write文件中输出"云深不知处",然后从write文件中读取,再输出到read文件中

public static void main(String[] args) throws IOException {
        File writeFile = new File("F:/test/writeTest/write.txt");
        File readFile = new File("F:/test/readTest/read.txt");

        write(writeFile);
        readAndWrite(writeFile,readFile);
    }

    public static void write(File file) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write("云深不知处");
        bw.close();
    }

    public static void readAndWrite(File writeFile, File readFile) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(writeFile));
        BufferedWriter bw = new BufferedWriter(new FileWriter(readFile));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null){
            // 读一行存一行
            bw.write(line);
        }
        br.close();
        bw.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值