20.01  递归概述和注意事项

递归:方法定义中调用方法本身的现象

递归注意事项:

1.要有出口,否则就是死递归

2.次数不能太多,否则就内存溢出

3.构造方法不能递归使用

20.02  递归求阶乘的代码实现及内存图解

例:

 public class Practice
{
public static void main(String[] args)
{
System.out.println(jieCheng(5));
}
public static int jieCheng(int i)
{
if(i == 1)
return 1;
else
return i * jieCheng(i - 1);
}
}

 

20.03  不死神兔问题案例

有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?

第1个月:1    第2个月:1    第3个月:2   

第4个月:3    第5个月:5    第6个月:8

...

由此可见兔子对象的数据是:1,1,2,3,5,8...

规则:A:从第三项开始,每一项是前两项之和

     B:而且说明前两项是已知的

 public class Practice
{
public static void main(String[] args)
{
//方式1:数组实现
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for(int i = 2 ; i < arr.length; i++)
{
arr[i] = arr[i - 2] + arr[i - 1];
}
System.out.println(arr[19]);
System.out.println("------");
//方式2:变量的变化实现
int a = 1;
int b = 1;
for (int i = 0; i < 18; i++)
{
int temp = a;
a = b;
b = temp + b;
}
System.out.println(b);
System.out.println("------");
//方式3
System.out.println(sum(20));
}
//方式3:递归实现
public static int sum(int i)
{
if(i == 1 || i == 2)
return 1;
else
return sum(i - 1) + sum(i - 2);
}
}

20.04  递归输出指定目录下所有的java文件的绝对路径案例

 public class Practice
{
public static void main(String[] args)
{
File f = new File("E:\\javaSE");
getAllJavaFilePaths(f);
}
public 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());
}
}
}
}
}

20.05  递归删除带内容的目录案例

 public class Practice
{
public static void main(String[] args)
{
File f = new File("D:\\demo");
deleteFolder(f);
}
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
{
file.delete();
System.out.println("删除文件:"+file.getName());
}
}
//删除文件夹
srcFolder.delete();
System.out.println("删除文件夹:"+srcFolder.getName());
}
}
}

20.06  IO流概述及分类

IO流用来处理设备之间的数据传输

Java对数据的操作是通过流的方式

Java用于操作流的对象都在IO包中

20.07  IO流基类概述

按照数据流向:输入流[读入数据]、输出流[写出数据]

按照数据类型:

字节流字节输入流[InputStream]、字节输出流[OutputStream]

字符流字符输入流[Reader]、字符输出流[Writer]

注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀

 

InputStream:此抽象类是表示字节输入流的所有类的超类。

OutputStream:此抽象类是表示输出字节流的所有类的超类。

Reader:用于读取字符流的抽象类。

Writer:写入字符流的抽象类。

20.08  FileOutputStream的构造方法

FileOutputStream的构造方法

public FileOutputStream(File file)throws FileNotFoundException

创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

public FileOutputStream(String name)throws FileNotFoundException

创建一个向具有指定名称的文件中写入数据的输出文件流。

20.09  FileOutputStream写出数据

例:

 FileOutputStream fos = new FileOutputStream("D:\\a.txt");
fos.write("hello,IO".getBytes());
//释放资源,关闭此文件输出流并释放与此流有关的所有系统资源。
fos.close();

创建字节输出流对象了做了3件事情:

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

B:创建fos对象

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

字节输出流操作步骤:

A:创建字节输出流对象

B:写数据

C:释放资源

为什么一定要close()

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

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

20.10  FileOutputStream的三个write()方法

FileOutputStream 的方法:

1.public void write(int b)throws IOException

将指定字节写入此文件输出流。实现 OutputStream 的 write 方法。

2.public void write(byte[] b)throws IOException

将 b.length 个字节从指定 byte 数组写入此文件输出流中。

3.public void write(byte[] b,int off,int len)throws IOException

将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream("D:\\a.txt");
//写一个字节
fos.write(97);
byte[] bys = {97,98,99,100,101};
//写一个字节数组
fos.write(bys);
//写一个字节数组的一部分,从1开始写3个
fos.write(bys,1,3);
//释放资源
fos.close();
}
}

文件a.txt中的内容:aabcdebcd

20.11  FileOutputStream写出数据实现换行和追加写入

public FileOutputStream(String name,boolean append)throws FileNotFoundException

创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
//true表示每次将内容写到文件的末尾
FileOutputStream fos = new FileOutputStream("D:\\a.txt",true);
for (int i = 0; i < 10; i++)
{
fos.write(("hello"+i).getBytes());
//写入换行符,不同系统下的换行符是不一样的,Windows下为\r\n
fos.write("\r\n".getBytes());
}
//释放资源
fos.close();
}
}

20.12  FileOutputStream写出数据异常处理

 public class Practice
{
public static void main(String[] args)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("D:\\a.txt");
fos.write("hello".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();
}
}
}
}
}

20.13  FileInputStream读取数据

FileInputStream的构造方法

1.public FileInputStream(File file)throws FileNotFoundException

通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

2.public FileInputStream(String name)throws FileNotFoundException

通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

 

FileInputStream的成员方法

1.public int read()throws IOException

从此输入流中读取一个数据字节。

2.public int read(byte[] b)throws IOException

从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
FileInputStream fis = new FileInputStream("D:\\a.txt");
// //读一个字节,没有数据返回-1
// int by = fis.read();
// System.out.println((char)by);
//循环读取
int ch = 0;
while((ch = fis.read()) != -1)
{
//此处如果读中文会出现乱码,因为一个汉字占用两个字节
System.out.print((char)ch);
}
fis.close();
}
}

20.14  字节流复制文本文件案例(一次读一个字节)

数据源:从哪里来  a.txt--读取数据--FileInputStream 

目的地:到哪里去  b.txt--写数据--FileOutputStream

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装数据源
FileInputStream fis = new FileInputStream("d:\\a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("d:\\b.txt"); int by = 0;
while ((by = fis.read()) != -1)
{
fos.write(by);
} // 释放资源
fos.close();
fis.close();
}
}

20.15  计算机是如何识别把两个字节拼接为中文

例:

 String s = "你好中国";
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));

运行结果:

[-60, -29, -70, -61, -42, -48, -71, -6]

上例中在GBK编码下将"你好中国"转为字节数组发现数组中的元素都是负数

在计算机中中文的存储分两个字节:

第一个字节肯定是负数。

第二个字节常见的是负数,可能有正数。但是没影响。

20.16  字节流复制图片案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
FileInputStream fis = new FileInputStream("C:\\06.jpg");
FileOutputStream fos = new FileOutputStream("D:\\006.jpg"); int by = 0;
while((by = fis.read()) != -1)
{
fos.write(by);
} fis.close();
fos.close();
}
}

20.17  FileInputStream读取数据一次一个字节数组

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
FileInputStream fis = new FileInputStream("D:\\a.txt"); //数组的长度一般是1024或者1024的整数倍
byte[] bys = new byte[1024];
int len = 0;//定义一个变量用于接收读取的字节个数
while((len = fis.read(bys)) != -1)
{
//将数组的一部分转成字符串,因为数组不一定每次都是读满的
//读多少数据就转多少数据
//不需要换行符
System.out.print(new String(bys, 0, len)); }
//释放资源
fis.close();
}
}

20.18  字节流复制文本文件案例(一次读一个字节数组)

 public class Practice
{
public static void main(String[] args) throws IOException
{
//原数据
FileInputStream fis = new FileInputStream("D:\\a.txt");
//目的数据
FileOutputStream fos = new FileOutputStream("D:\\c.txt");
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1)
{
fos.write(bys, 0, len);
}
//释放资源
fis.close();
fos.close();
}
}

20.19  字节流复制视频案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
//原数据
FileInputStream fis = new FileInputStream("C:\\test.flv");
//目的数据
FileOutputStream fos = new FileOutputStream("D:\\aaa.flv");
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1)
{
fos.write(bys, 0, len);
}
//释放资源
fis.close();
fos.close();
}
}

20.20  BufferedOutputStream写出数据

字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式),所以提供了字节缓冲区流

字节缓冲输出流:BufferedOutputStream

字节缓冲输入流:BufferedInputStream

 

BufferedOutputStream构造方法:

1.public BufferedOutputStream(OutputStream out)

创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

2.public BufferedOutputStream(OutputStream out,int size)

创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream("D:\\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//写数据
bos.write("hello".getBytes());
//关闭资源
bos.close();
}
}

20.21  BufferedInputStream读取数据

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
//方式1:读一个字节
int by = 0;
while((by = bis1.read()) != -1)
{
System.out.print((char)by);
}
System.out.println("------");
//方式2:读一个字节数组
BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis2.read(bys)) != -1)
{
System.out.println(new String(bys, 0, len));
} bis1.close();
bis2.close();
}
}

20.22  字节流四种方式复制MP4并测试效率

基本字节流一次读写一个字节        //耗时:103891毫秒

基本字节流一次读写一个字节数组   //耗时:1110毫秒

高效字节流一次读写一个字节        //耗时:1547毫秒

高效字节流一次读写一个字节数组   //耗时:953毫秒

 public class Practice
{
public static void main(String[] args) throws IOException
{
long time1 = System.currentTimeMillis();
// method1("C:\\test.flv","D:\\test1.flv");
// method2("C:\\test.flv","D:\\test2.flv");
// method3("C:\\test.flv","D:\\test3.flv");
method4("C:\\test.flv","D:\\test4.flv");
long time2 = System.currentTimeMillis();
System.out.println("耗时:"+(time2 - time1)+"毫秒");
}
//基本字节流一次读写一个字节
//耗时:103891毫秒
public static void method1(String src,String dest) throws IOException
{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest); int by = 0;
while((by = fis.read()) != -1)
{
fos.write(by);
} fis.close();
fos.close();
}
//基本字节流一次读写一个字节数组
//耗时:1110毫秒
public static void method2(String src,String dest) throws IOException
{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest); byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1)
{
fos.write(bys,0,len);
} fis.close();
fos.close();
}
//高效字节流一次读写一个字节
//耗时:1547毫秒
public static void method3(String src,String dest) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)); int by = 0;
while ((by = bis.read()) != -1)
{
bos.write(by);
} bos.close();
bis.close();
}
//高效字节流一次读写一个字节数组
//耗时:953毫秒
public static void method4(String src,String dest) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)); byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1)
{
bos.write(bys, 0, len);
} bos.close();
bis.close();
}
}

JavaSE学习总结第20天_IO流2的更多相关文章

  1. JavaSE学习总结第21天_IO流3

      21.01  转换流出现的原因及格式 由于字节流操作中文不是特别方便,所以,java就提供了转换流. 字符流 = 字节流 + 编码表 21.02  编码表概述和常见编码表 编码表:计算机只能识别二 ...

  2. JavaSE学习总结第19天_IO流1

      19.01  集合的特点和数据结构总结 HashSet.HashMap.Hashtable判断元素唯一性的方式: 通过对象的hashCode和equals方法来完成元素唯一性 如果对象的hashC ...

  3. JavaSE学习总结第22天_IO流4

    -  22.01  数据输入输出流的概述和讲解 操作基本数据类型 public class DataInputStreamextends FilterInputStream implements Da ...

  4. javaSE学习笔记(15) ---缓冲流、转换流、序列化流

    javaSE学习笔记(15) ---缓冲流.转换流.序列化流 缓冲流 昨天复习了基本的一些流,作为IO流的入门,今天我们要见识一些更强大的流.比如能够高效读写的缓冲流,能够转换编码的转换流,能够持久化 ...

  5. 毕向东_Java基础视频教程第20天_IO流(15~17)

    第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...

  6. 毕向东_Java基础视频教程第20天_IO流(11~14)

    第20天-11-IO流(Properties简述) .properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名.它们也可以存储用于国际化和本地化的字符串,这种文 ...

  7. 毕向东_Java基础视频教程第20天_IO流(7~10)

    第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...

  8. 毕向东_Java基础视频教程第20天_IO流(5~6)

    第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...

  9. 毕向东_Java基础视频教程第20天_IO流(1~4)

    第20天-01-IO流(File概述) File类: 用来将文件或者文件夹封装成对象, 方便进行操作. File对象可以作为参数, 传递给流对象的构造函数. 流对象不能操作文件夹; 流对象不能操作文件 ...

随机推荐

  1. 树的判断(poj nyoj hduoj)

    题目: http://ac.jobdu.com/problem.php?pid=1481 http://acm.nyist.net/JudgeOnline/problem.php?pid=129 ht ...

  2. jQuery 插件入门

    先看http://www.cnblogs.com/lideng/p/3605551.html http://www.cnblogs.com/xcj26/p/3345556.html (非常细致  很多 ...

  3. uva 719 Glass Beads(后缀自动机)

    [题目链接] https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=524&am ...

  4. 今天研究了下webservice 终于OK了

    今天研究了下webservice 终于OK了,所以把它写到自己的博客来,因为网上说的都很复杂     而在这里,我会很简单的说明,一看就懂     首先在进行webservice  一定要下载包    ...

  5. Swift 中类的初始化器与继承

    首先,Swift 为类定义了两种初始化器来确保类中所有的储存属性都能得到一个初始化值.这两种初始化器就是「指定初始化器」(Designated Initializer)与「便利初始化器」(Conven ...

  6. Android进程的内存管理分析

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...

  7. DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

    这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用.先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使 ...

  8. POJ 3623 Best Cow Line, Gold(字符串处理)

    题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再 ...

  9. VS C++工程类成员初始化检测脚本

    最近项目中出现由类成员未初始化而进行读写而造成的问题,于是想将项目中所有的为初始化的地方找出来,优化一下代码,维护了这么多年的程序已有百万余行且VS2015还尚未支持检查类成员初始化的方法.,于是想写 ...

  10. Random类短时间大量随机重复的问题

    先声明一下,我是在那篇文章上看到的解决方法: http://dl.download.csdn.net/down10/20141103/4b173214e41ff3207305c2470524b0f3. ...