20 API-IO流(递归,IO流(字节流FileOutputStream,FileInputStream,字节缓冲区流BufferedOutputStream,BufferedInputStream

1:递归(理解)

(1)方法定义中调用方法本身的现象
举例:老和尚给小和尚讲故事,我们学编程
(2)递归的注意事项;
A:要有出口,否则就是死递归
B:次数不能过多,否则内存溢出
C:构造方法不能递归使用

(3)递归的案例:

递归思想分解法


A:递归求阶乘


/*
 * 需求:请用代码实现求5的阶乘。
 * 下面的知识要知道:
 * 		5! = 1*2*3*4*5
 * 		5! = 5*4!
 * 
 * 有几种方案实现呢?
 * 		A:循环实现
 * 		B:递归实现
 * 			a:做递归要写一个方法
 * 			b:出口条件
 * 			c:规律
 */
public class DiGuiDemo {
	public static void main(String[] args) {
		int jc = 1;
		for (int x = 2; x <= 5; x++) {
			jc *= x;
		}
		System.out.println("5的阶乘是:" + jc);
		
		System.out.println("5的阶乘是:"+jieCheng(5));
	}
	
	/*
	 * 做递归要写一个方法:
	 * 		返回值类型:int
	 * 		参数列表:int n
	 * 出口条件:
	 * 		if(n == 1) {return 1;}
	 * 规律:
	 * 		if(n != 1) {return n*方法名(n-1);}
	 */
	public static int jieCheng(int n){
		if(n==1){
			return 1;
		}else {
			return n*jieCheng(n-1);
		}
	}
}


B:兔子问题

/*
 * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
 * 分析:我们要想办法找规律
 * 			兔子对数
 * 第一个月: 	1
 * 第二个月:	1
 * 第三个月:	2
 * 第四个月:	3	
 * 第五个月:	5
 * 第六个月:	8
 * ...
 * 
 * 由此可见兔子对象的数据是:
 * 		1,1,2,3,5,8...
 * 规则:
 * 		A:从第三项开始,每一项是前两项之和
 * 		B:而且说明前两项是已知的
 * 
 * 如何实现这个程序呢?
 * 		A:数组实现
 * 		B:变量的变化实现
 * 		C:递归实现
 * 
 * 假如相邻的两个月的兔子对数是a,b
 * 第一个相邻的数据:a=1,b=1
 * 第二个相邻的数据:a=1,b=2
 * 第三个相邻的数据:a=2,b=3
 * 第四个相邻的数据:a=3,b=5
 * 看到了:下一次的a是以前的b,下一次是以前的a+b	
 */
public class DiGuiDemo2 {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = new int[20];
		arr[0] = 1;
		arr[1] = 1;
		// arr[2] = arr[0] + arr[1];
		// arr[3] = arr[1] + arr[2];
		// ...
		for (int x = 2; x < arr.length; x++) {
			arr[x] = arr[x - 2] + arr[x - 1];
		}
		System.out.println(arr[19]);// 6765
		System.out.println("----------------");

		int a = 1;
		int b = 1;
		for (int x = 0; x < 18; x++) {
			// 临时变量存储上一次的a
			int temp = a;
			a = b;
			b = temp + b;
		}
		System.out.println(b);
		System.out.println("----------------");

		System.out.println(fib(20));
	}

	/*
	 * 方法: 返回值类型:int 参数列表:int n 出口条件: 第一个月是1,第二个月是1 规律: 从第三个月开始,每一个月是前两个月之和
	 */
	public static int fib(int n) {
		if (n == 1 || n == 2) {
			return 1;
		} else {
			return fib(n - 1) + fib(n - 2);
		}
	}
}


C:递归输出指定目录下所有指定后缀名的文件绝对路径

import java.io.File;

/*
 * 需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。
 * 
 * 分析:
 * 		A:封装目录
 * 		B:获取该目录下所有的文件或者文件夹的File数组
 * 		C:遍历该File数组,得到每一个File对象
 * 		D:判断该File对象是否是文件夹
 * 			是:回到B
 * 			否:继续判断是否以.java结尾
 * 				是:就输出该文件的绝对路径
 * 				否:不搭理它
 */
public class FilePathDemo {
	public static void main(String[] args) {
		// 封装目录
		File srcFolder = new File("E:\\JavaSE");

		// 递归功能实现
		getAllJavaFilePaths(srcFolder);
	}

	private static void getAllJavaFilePaths(File srcFolder) {
		// 获取该目录下所有的文件或者文件夹的File数组
		File[] fileArray = srcFolder.listFiles();

		// 遍历该File数组,得到每一个File对象
		for (File file : fileArray) {
			// 判断该File对象是否是文件夹
			if (file.isDirectory()) {
				getAllJavaFilePaths(file);
			} else {
				// 继续判断是否以.java结尾
				if (file.getName().endsWith(".java")) {
					// 就输出该文件的绝对路径
					System.out.println(file.getAbsolutePath());
				}
			}
		}
	}
}


D:递归删除带内容的目录(小心使用)

import java.io.File;

/*
 * 需求:递归删除带内容的目录
 * 
 * 目录我已经给定:demo
 * 
 * 分析:
 * 		A:封装目录
 * 		B:获下的所有文件或者文件夹的File数组
 * 		C:遍历该Fil取该目录e数组,得到每一个File对象
 * 		D:判断该File对象是否是文件夹
 * 			是:回到B
 * 			否:就删除
 */
public class FileDeleteDemo {
	public static void main(String[] args) {
		// 封装目录
		File srcFolder = new File("demo");
		// 递归实现
		deleteFolder(srcFolder);
	}

	private static void deleteFolder(File srcFolder) {
		// 获取该目录下的所有文件或者文件夹的File数组
		File[] fileArray = srcFolder.listFiles();

		if (fileArray != null) {
			// 遍历该File数组,得到每一个File对象
			for (File file : fileArray) {
				// 判断该File对象是否是文件夹
				if (file.isDirectory()) {
					deleteFolder(file);
				} else {
					System.out.println(file.getName() + "---" + file.delete());
				}
			}

			System.out
					.println(srcFolder.getName() + "---" + srcFolder.delete());
		}
	}
}




2:IO流(掌握)

       (1)IO用于在设备间进行数据传输的操作 

       (2)分类:


       (2)分类:

              A:流向

                     输入流    读取数据

                     输出流    写出数据

              B:数据类型

                     字节流   

                                   字节输入流

                                   字节输出流

                     字符流

                                   字符输入流

                                   字符输出流

              注意:

                     a:如果我们没有明确说明按照什么分,默认按照数据类型分。

                     b:除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,否则建议使用字节流。


(3)FileOutputStream写出数据

              A:操作步骤

                     a:创建字节输出流对象

                     b:调用write()方法

                     c:释放资源

                    

              B:代码体现:

			FileOutputStream fos = new FileOutputStream("fos.txt");
			
			fos.write("hello".getBytes());
			
			fos.close();


C:要注意的问题

a:创建字节输出流对象做了几件事情?

* A:调用系统功能去创建文件

* B:创建fos对象

* C:把fos对象指向这个文件

b:为什么要close()?

* A:让流对象变成垃圾,这样就可以被垃圾回收器回收了

* B:通知系统去释放跟该文件相关的资源

c:如何实现数据的换行?

写入换行符号

*windows:\r\n

* linux:\n

* Mac:\r

d:如何实现数据的追加写入?

用构造方法带第二个参数是true的情况即可


输出流异常处理

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入异常处理的字节输出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 分开做异常处理
		// FileOutputStream fos = null;
		// try {
		// fos = new FileOutputStream("fos4.txt");
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.write("java".getBytes());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 一起做异常处理
		// try {
		// FileOutputStream fos = new FileOutputStream("fos4.txt");
		// fos.write("java".getBytes());
		// fos.close();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 改进版
		// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 为了保证close()一定会执行,就放到这里了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


(4)FileInputStream读取数据

              A:操作步骤

                     a:创建字节输入流对象

                     b:调用read()方法

                     c:释放资源

                    

              B:代码体现:


			FileInputStream fis = new FileInputStream("fos.txt");
			
			//方式1
			int by = 0;
			while((by=fis.read())!=-1) {
				System.out.print((char)by);
			}
			
			//方式2
			byte[] bys = new byte[1024];
			int len = 0;
			while((len=fis.read(bys))!=-1) {
				System.out.print(new String(bys,0,len));
			}
			
			fis.close();


两种方式图解


(5)案例:2种实现

A:复制文本文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 复制文本文件。
 * 
 * 数据源:从哪里来
 * a.txt	--	读取数据	--	FileInputStream	
 * 
 * 目的地:到哪里去
 * b.txt	--	写数据		--	FileOutputStream
 * 
 * java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
 * 
 * 这一次复制中文没有出现任何问题,为什么呢?
 * 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。
 * 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。
 * 它会自己做转换。
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("a.txt");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("b.txt");

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 释放资源(先关谁都行)
		fos.close();
		fis.close();
	}
}


B:复制图片
C:复制视频
(6)字节缓冲区流

A:BufferedOutputStream

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));

B:BufferedInputStream

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));

(7)案例:4种实现
A:复制文本文件
B:复制图片

C:复制视频

* 计算机是如何识别什么时候该把两个字节转换为一个中文呢?
 * 在计算机中中文的存储分两个字节:
 * 第一个字节肯定是负数。
 * 第二个字节常见的是负数,可能有正数。但是没影响。

复制四种实现

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
 * 
 * 字节流四种方式复制文件:
 * 基本字节流一次读写一个字节:	共耗时:117235毫秒
 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒
 * 高效字节流一次读写一个字节: 共耗时:1141毫秒
 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		// method1("e:\\哥有老婆.mp4", "copy1.mp4");
		// method2("e:\\哥有老婆.mp4", "copy2.mp4");
		// method3("e:\\哥有老婆.mp4", "copy3.mp4");
		method4("e:\\哥有老婆.mp4", "copy4.mp4");
		long end = System.currentTimeMillis();
		System.out.println("共耗时:" + (end - start) + "毫秒");
	}

	// 高效字节流一次读写一个字节数组:
	public static void method4(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		bis.close();
	}

	// 高效字节流一次读写一个字节:
	public static void method3(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		int by = 0;
		while ((by = bis.read()) != -1) {
			bos.write(by);

		}

		bos.close();
		bis.close();
	}

	// 基本字节流一次读写一个字节数组
	public static void method2(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		fos.close();
		fis.close();
	}

	// 基本字节流一次读写一个字节
	public static void method1(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fos.close();
		fis.close();
	}
}




3:自学字符流
IO流分类
字节流:
InputStream
FileInputStream
BufferedInputStream
OutputStream
FileOutputStream
BufferedOutputStream

字符流:
Reader
FileReader
BufferedReader
Writer
FileWriter
BufferedWriter
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值