1. 递归的概述和注意事项
-
递归 : 方法定义中调用方法本身的现象
-
A. 递归要写一个方法
-
B. 递归要有出口条件
-
C. 要有规律
-
注意事项 :
要有出口,否则就是死递归 次数不能过多,否则内存溢出 构造方法不能递归使用
2. 递归解决问题的思想及图解

3. 递归求阶乘的代码实现及内存图解
public class DiGuiDemo {
public static void main(String[] args) {
System.out.println(jieCheng(6));
}
public static int jieCheng(int n) { // 递归要写一个方法
if (n == 1) {
return 1; // 出口条件
} else {
return n * jieCheng(n - 1); // 规律
}
}
}

4. 不死神兔问题案例
public class DiGuiDemo2 {
public static void main(String[] args) {
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for (int x = 2; x < arr.length; x++) {
arr[x] = arr[x-2] + arr[x-1];
}
System.out.println(arr[19]);
int a = 1;
int b = 1;
for (int x = 0; x < 18; x++) {
int temp = a;
a = b;
b = temp + b;
}
System.out.println(b);
System.out.println(fib(20));
}
public static int fib(int n) {
if (n == 1 || n ==2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
5. 递归删除带内容的目录案例
import java.io.File;
public class FileDelteDemo {
public static void main(String[] args) {
File srcFolder = new File("dust6");
deleteFolder(srcFolder);
}
private static void deleteFolder(File srcFolder) {
File[] fileArray = srcFolder.listFiles();
for (File file : fileArray) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
System.out.println(file.getName() + "---" + file.delete());
}
}
System.out.println(srcFolder.getName() + "---" + srcFolder.delete());
}
}
6. IO流概述及其分类
-
IO用于在设备间进行数据传输的操作
-
分类 :
A:流向 输入流 读取数据 输出流 写出数据 B:数据类型 字节流 字节输入流 字节输出流 字符流 字符输入流 字符输出流 注意: a:如果我们没有明确说明按照什么分,默认按照数据类型分。 b:除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,否则建议使用字节流。
7. FileOutputStream的构造方法
-
public FileOutputStream(File file) throws FileNotFoundException
-
public FileOutputStream(String name) throws FileNotFoundException
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class FileOutputStreamDemo {
public static void main(String[] args) throws FileNotFoundException {
// public FileOutputStream(File file) throws FileNotFoundException
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);
// public FileOutputStream(String name) throws FileNotFoundException
// FileOutputStream fos = new FileOutputStream("fos.txt");
}
}
8. 为什么要close() ?
- A. 让对象变成垃圾,这样就可以被垃圾回收站回收了
- B. 通知系统去释放跟该文件相关的资源
- C. close() 之后便无法调用对象
9. FileOutputStream写出数据
- a. 创建字节输出流对象
- b. 调用write()方法
- c. 释放资源
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// public FileOutputStream(File file) throws FileNotFoundException
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);
// public FileOutputStream(String name) throws FileNotFoundException
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write("hello,IO".getBytes());
fos.close();
}
}
10. FileOutputStream的三个write()方法
- public void write(byte[] b) throws IOException : 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
- public void write(byte[] b, int off, int len) throws IOException : 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
- public void write(int b) throws IOException : 将指定字节写入此文件输出流。实现 OutputStream 的 write 方法。先将int 数据 转换为ASCII码表中对应的字节。
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutPutStreamDemo2 {
public static void main(String[] args) throws IOException {
FileOutputStream fos2 = new FileOutputStream("fos2.txt");
// fos2.write(97);
// fos2.write(57);
// fos2.write(55);
byte[] bys = {122,57,53,50,55};
fos2.write(bys);
fos2.write(bys, 1, 4);
fos2.close();
}
}
11. FileOutputStream写出数据实现换行和追加写入
- fos3.write("\n".getBytes());
- 如何实现数据的追加写入?: public FileOutputStream(File file, boolean append) throws FileNotFoundException 令此构造方法第二个参数是true即可。
- 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// FileOutputStream fos3 = new FileOutputStream("fos3.txt");
FileOutputStream fos3 = new FileOutputStream("fos3.txt",true);
for (int x = 0; x < 10; x++) {
fos3.write(("hello" + x).getBytes());
fos3.write("\n".getBytes());
}
fos3.close();
}
}
12. FileInputStream读取数据
-
public int read() throws IOException : 从此输入流中一次读取一个数据字节。
-
public int read(byte[] b) throws IOException : 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回值是实际读取的字节个数。
-
操作步骤 :
a:创建字节输入流对象 b:调用read()方法 c:释放资源
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
// FileInputStream fis = new FileInputStream("fis.txt");
FileInputStream fis66 = new FileInputStream("FileOutputStreamDemo.java");
// int by = fis.read();
// while (by != -1) {
// System.out.print((char)by);
// by = fis.read();
// }
int by = 0;
while ((by = fis66.read()) != -1) {
System.out.print((char)by);
}
fis66.close();
}
}
13. 字节流复制文本文件案例1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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();
}
}
14. 字节流复制文本文件案例2
package zywaf.yty.practice_03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis66 = new FileInputStream("/Users/yangtengyu/desktop/liuyi/a.txt");
FileOutputStream fos66 = new FileOutputStream("/Users/yangtengyu/desktop/CSDN/Java基础/b.txt");
int by = 0;
while ((by=fis66.read()) != -1) {
fos66.write(by);
}
fos66.close();
fis66.close();
}
}
15. 字节流复制图片案例
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyImageDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/yangtengyu/desktop/金陵雷神/YTY_Java简历.jpg");
FileOutputStream fos = new FileOutputStream("非常突然找工作.jpg");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
16. 字节流复制动图案例1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyGifDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/yangtengyu/downloads/nba/壁纸/德国战车/thread_233843510580140_20190318224302_s_2720885_w_320_h_180_37595.gif");
FileOutputStream fos = new FileOutputStream("完美的投篮.gif");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
17. FileInputStream读取数据一次一个字节数组
- public int read(byte[] b) throws IOException : 返回值是实际读取的字节个数。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// FileInputStream fis = new FileInputStream("fis.txt");
FileInputStream fis66 = new FileInputStream("FileOutputStreamDemo.java");
byte[] bys = new byte[1024];
int length = 0;
while ((length = fis66.read(bys)) != -1) {
System.out.print(new String(bys, 0, length));
}
fis66.close();
}
}
18. FileInputStream读取数据的两种方式比较图解

19. 字节流复制文本文件案例3(用字节数组)
package zywaf.yty.practice_03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo3 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/yangtengyu/desktop/金陵雷神/发微博/热血高校.txt");
FileOutputStream fos = new FileOutputStream("/Users/yangtengyu/desktop/liuyi/泷谷源治.txt");
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
20. 字节流复制视频案例2(用byte[] b)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyGifDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/yangtengyu/downloads/nba/壁纸/德国战车/诺式-金鸡独立.gif");
FileOutputStream fos = new FileOutputStream("一人一城留美名.gif");
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
}
21. 高效字节流字节缓冲区流(高效字节流BufferedOutputStream、高效字节流BufferedInputStream)
- 注意:虽然我们有两种方式可以读取,但是这两种方式针对同一个对象在一个代码中只能使用一个。
22. 字节流四种方式复制MP4并测试效率
- 结论1:字节数组比字节块
- 结论2:带Buffered的比不带Buffered的块
package zywaf.yty.practice_06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:测试4种方法复制gif的时间
*
* 字节流复制的4种方式:
*
* 1.基本字节流一次读写一个字节: 耗时28648毫秒
* 2.基本字节流一次读写一个字节数组: 耗时36毫秒
* 3.高效字节流一次读写一个字节: 耗时159毫秒
* 4.高效字节流一次读写一个字节数组: 耗时16毫秒
*
*
*/
public class FuckingCopyTest {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method4("/Users/yangtengyu/downloads/nba/壁纸/BbsImg_56850596906382_1557564674_s_3757821_o_w_400_h_225_9158.gif","copy69.gif");
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "毫秒");
}
// 1.基本字节流一次读写一个字节
public static void method1(String srcString, String descString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(descString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
// 2.基本字节流一次读写一个字节数组
public static void method2(String srcString, String descString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(descString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 3.高效字节流一次读写一个字节:
public static void method3(String srcString, String descString) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(descString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 4.高效字节流一次读写一个字节数组:
public static void method4(String srcString, String descString) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(descString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
265

被折叠的 条评论
为什么被折叠?



