IO流 字节字符输入输出总结

package com.zlj.work;

import java.io.*;
import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //创建一个file文件
        File file =  new File("D:/aaa/1.txt");
        //使用字节输入流
        FileInputStream fis = new FileInputStream(file);
        //缓冲
        BufferedInputStream bis = new BufferedInputStream(fis);
        //创建一个缓冲数组
        byte[] buf = new byte[4 * 1024];
        //读取到缓存数组中
//      bis.read(buf);//将1.txt内容读取到缓存数组中
        //看看buf这个数组中是否有值
        // 将一个字符数组转换为字符串
//        System.out.println(new String(buf));
//        System.out.println(Arrays.toString(buf));
        //toCharArray() 方法将字符串转换为字符数组。
        int length; //-1 指到达了字符串的末尾,
        while ((length = bis.read(buf))!= -1){
            System.out.println(new String(buf ,0 ,length));
            //System.out.println(Arrays.toString(buf));
        }
        //关闭相关流
        bis.close();
        fis.close();

    }
}

案例1

package com.zlj.work;

import java.io.*;

//将java中的代码写入磁盘里面
public class Demo2 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:/aaa/2.txt");
        //2.创建一个字节输出流
        FileOutputStream fos = new FileOutputStream(file);
        //3.缓冲区
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //4.字符串
        String str = "不能说的秘密" ;//将字符串写入文件中
       //参数是byte 数据时一个字符串
        //将字符串转换为字节数组 getBytes()
        byte[] bytes = str.getBytes();
        //写入
        //bos.write(bytes);
        bos.write(bytes,6,6);
        //先刷新在关
        bos.flush();
        //关闭流
        bos.close();
        fos.close();
    }
}

案例2

package com.zlj.work;

import java.io.*;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        copyvideo();
    }
    public static void copyvideo() throws IOException {
//        File file = new File("D:/aaa/1.mp4");
//        FileInputStream fis = new FileInputStream(file);
        long start = System.currentTimeMillis();
        //字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:/aaa/1.mp3")));
        //字节输出流
        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(new File("D:/aaa/ccc/2.mp3")));

        byte[] b = new byte[4 * 1024];

        int length;
        while ((length = bis.read()) != -1){
           // System.out.println(length);
            bos.write(b ,0,length);

        }

        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

案例3

package com.zlj.io;

import java.io.*;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:/aaa/1.txt");
        FileReader fr = new FileReader(file);
        //缓冲
        BufferedReader br = new BufferedReader(fr);
        //缓冲数组
        char[] cbuf = new  char[4];
        //先学第一个

//        int length = br.read(cbuf);
//        System.out.println(cbuf);//afbk
//        System.out.println(length);//4
//        int length1 = br.read(cbuf);
//        System.out.println(cbuf);//jfbk
//        System.out.println(length1);// 1  read第二遍之后覆盖了之前的
        int length;
        while ((length = br.read(cbuf)) != -1){
            System.out.println(length);
            System.out.println(new String(cbuf,0,length));
        }
        br.close();
        fr.close();
    }
}

案例4

package com.zlj.io;

import java.io.*;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        //新建
        File file = new File("D:/aaa/2.txt");
        FileWriter fs = new FileWriter(file);
        //缓冲
        BufferedWriter bw = new BufferedWriter(fs);
        String s = "hellow";
        char[] cb = s.toCharArray();
        bw.write(cb,0, cb.length);
        bw.close();
        fs.close();
    }
}

案例5

package com.zlj.test;

import java.io.File;

public class Demo1 {
    public static void main(String[] args) {

        File file = new File("D:/aaa");

        System.out.println(file.getName());
        findJava(file,1);

    }
    //等级
    public  static void findJava(File file,int level){
        //递归tree等级
        //通过空格控制缩进
        String str = "";
        for (int i = 0; i < level; i++) {
            str += "    ";
        }
        //tree目录 把文件遍历一下
        File[] files = file.listFiles();

        for (int i = 0; i <files.length ; i++) {
            System.out.println(str + files[i].getName());
            //判断是不是文件,遇到数组定义时一定要注意下标
            if(files[i].isDirectory()){
                findJava(files[i],level+1);


            }
        }


    }
}

案例5

package com.zlj.test;

import java.io.File;

//一个文件夹下(文件夹地址:C:\Users\IdeaProjects)有多少个java文件
public class Demo2 {
    static int count = 0;
    public static void main(String[] args) {

        File file = new File("D:\\IDEA");
        test(file);
        System.out.println(count);
    }

    public static void test(File file) {
    File[] files = file.listFiles();
        for (File file1 : files) {
            if(file1.isDirectory()){
                test(file1);
            }else {
                if (file1.getName().endsWith(".java")){
                    count++;
                    System.out.println(file1.getName());
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值