递归和I/O流

递归

递归:是一种方法内部调用方法本身的编程技巧。

计算5! = 5*4*3*2*1。
5! = 4! * 5
4! = 3! * 4
3! = 2! * 3
2! = 1! * 2
1! = 1;

public static void main(String[] args){
    m(5);// 120
}
public static int m(int n){
    if(n == 1){// 递归:要给一个结束点(出口)
        return 1;
    }
    return n*m(n-1);// 递归调用

列出某指定文件夹下的所有文件

public static void main(String[] args) {
    File file = new File("F:/abc/");
    m(file);
}

// 用于列出某指定文件夹下的所有文件
public static void m(File dir){
    File[] fileArr = dir.listFiles();
    for(File f : fileArr){
        if(f.isDirectory()){
            m(f);// 如果是文件夹,递归调用方法本身
        }else {
            System.out.println(f);
        }
    }
}

I/O流

  • 1、为什么要使用IO流?
    持久化:将内存中的数据存储在硬盘上的过程。IO流、JDBC、Hibernate

  • 2、以程序作为参照物,则分为:
    InputStream输入流:由文件到程序(抽象类)
    OutputStream输出流:由程序到文件(抽象类)
    文件输入输出流:FileInputStream FileOutputStream

  • 3、流的特点:
    流只能顺序的操作
    流都是成对出现的(方向不同)

  • 注:
    1、FileInputStream如果没有文件则报异常,而FileOutputStream会自动新建文件
    2、FileInputStream读取到文件末尾,得到-1的值,可以作为读取结束的标志。

  • I/O流的分类:

    • 以方向来说:输入流和输出流
    • 以包装设计模式来说:基本流和包装流
    • 能处理字节还是字符:字节流和字符流
    • 所有的以InputStream和OutputStream结尾的都是字节流
    • 所有的以Reader和Writer结尾的都是字符流。
    • InputStreamReader:连接字节流和字符流的桥梁,称之为转换流
    • OutputStreamWriter:连接字节流和字符流的桥梁,称之为转换流

      • 什么时候使用字节流,什么时候使用字符流?
    • 1、关于文件本身的操作(拷贝、下载、上传等),使用字节流。
    • 2、关于文本文件内容的操作,使用字符流
    • BufferedReader/BufferedWriter(功能欠缺)
    • PrintWriter和BufferedReader配对使用
// 读取指定文件的内容(F:/abc/a.txt)
File file = new File("F:/abc/a.txt");
FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
    int n1 = fis.read();// 一次读取一个字节
    System.out.println((char)n1);
    int n2 = fis.read();
    System.out.println((char)n2);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e){
    e.printStackTrace();
} finally{// 肯定会执行的代码 一般用于关闭资源或处理临时文件
    try {
        if(fis != null){
            fis.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

覆盖式写入数据

File file = new File("E:/abc/");
FileOutputStream fos = null;
try {
    // append:是否以追加的形式写内容到文件,默认是false,为覆盖模式
    fos = new FileOutputStream(file, true);// 追加模式
    fos.write(97);
    fos.write(100);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fos != null) {
            fos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

拷贝文件

public static void main(String[] args) {
    File fileSrc = new File("F:/abc/a.txt");
    File fileDest = new File("F:/abc/b.txt");
    copyFile(fileSrc,fileDest);// 复制文件
}
/**
 * 完成fileSrc到fileDest的拷贝工作
 * @param fileSrc 源文件的路径
 * @param fileDest 目标文件的路径
 */
public static void copyFile(String fileSrc,String fileDest){
    copyFile(new File(fileSrc),new File(fileDest));
}

/**
 * 完成fileSrc到fileDest的拷贝工作
 * @param fileSrc 源文件
 * @param fileDest 目标文件
 */
public static void copyFile(File fileSrc,File fileDest){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(fileSrc);
        fos = new FileOutputStream(fileDest);
        byte[] byArr = new byte[1024*8];// 空间换时间
        int b=0;
        // b:实际读到的字节数
        while ((b=fis.read(byArr)) != (-1)) {
            fos.write(byArr,0,b);// 一次写入byArr长度
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if(fos != null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I/O流包装类BufferedReader的使用

public static void main(String[] args) {
    File file = new File("F:/abc/a.txt");
    System.out.println(getText(file));
}

public static String getText(File file){
    FileInputStream fis = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    StringBuilder builder = null;
    try {
        fis = new FileInputStream(file);
        // 设置编码格式
        isr = new InputStreamReader(fis,"utf-8");
        // I/O包装类,功能增强,一次读一行
        br = new BufferedReader(isr);
        String str = null;
        builder = new StringBuilder();
        // 一次读一行
        while((str=br.readLine()) != null){
            builder.append(str);
            builder.append("\r\n");// 换行
        }
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return builder.toString();
}

I/O流包装类PrintWriter的使用

File file = new File("F:/abc/a.txt");
PrintWriter pw = null;
try {
// true:追加模式
    FileOutputStream fos = new FileOutputStream(file,true);
// utf-8:设置编码格式
    OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8:设置编码格式");
    pw = new PrintWriter(osw);// 实例化PrintWriter
    pw.println("abc");// 写入字符串数据,自带换行
    pw.print("def");
    pw.print("mon");
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}finally{
    pw.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值