Skip to content

Commit ed363eb

Browse files
committed
Java第二十天
1 parent ba0882f commit ed363eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1028
-0
lines changed

day20/IO流概述及分类.bmp

2.24 MB
Binary file not shown.

day20/code/day20_DiGui/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day20/code/day20_DiGui/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day20_DiGui</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* 递归:方法定义中调用方法本身的现象
5+
*
6+
* 注意事项:
7+
* A:递归一定要有出口,否则就是死递归
8+
* B:递归的次数不能过多,否则内存溢出
9+
* C:构造方法不能递归使用
10+
*
11+
* StackOverflowError:当应用程序递归太深而发生堆栈溢出时,抛出该错误。
12+
*
13+
* 举例:
14+
* 从前有座山,山上有座庙,庙里有个老和尚,正在给小和尚讲故事,故事是:
15+
* 从前有座山,山上有座庙,庙里有个老和尚,正在给小和尚讲故事,故事是:
16+
* 从前有座山,山上有座庙,庙里有个老和尚,正在给小和尚讲故事,故事是:
17+
* 从前有座山,山上有座庙,庙里有个老和尚,正在给小和尚讲故事,故事是:
18+
* ...
19+
*
20+
* 学java -- 找工作 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣钱 --
21+
* 学java -- 找工作 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣钱 --
22+
* 学java -- 找工作 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣钱 --
23+
* ...
24+
*/
25+
public class DiGuiDemo {
26+
public void show() {
27+
show();
28+
}
29+
30+
// public DiGuiDemo() {
31+
// DiGuiDemo();
32+
// }
33+
34+
public static void main(String[] args) {
35+
DiGuiDemo dgd = new DiGuiDemo();
36+
dgd.show();
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 需求:求5的阶乘
5+
*/
6+
public class DiGuiDemo {
7+
public static void main(String[] args) {
8+
// 用循环实现阶乘
9+
int jc = 1;
10+
for (int x = 1; x <= 5; x++) {
11+
jc *= x;
12+
}
13+
System.out.println("5的阶乘是:" + jc);
14+
15+
// 用递归实现求阶乘
16+
System.out.println("5的阶乘是:"+jc(5));
17+
18+
}
19+
20+
/*定义一个方法jc(int n),
21+
*假如jc(n)表示n的阶乘,请问
22+
*n-1的阶乘如何表示呢?
23+
*jc(n-1)
24+
*
25+
*出口是1!= 1
26+
*/
27+
public static int jc(int n){
28+
if(n==1){
29+
return 1;
30+
}else {
31+
return n*jc(n-1);
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 兔子问题(斐波那契数列):有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第20个月的兔子对数为多少?
5+
* 兔子对数
6+
* 第一个月: 1
7+
* 第二个月: 1
8+
* 第三个月: 2
9+
* 第四个月: 3
10+
* 第五个月: 5
11+
* 第六个月: 8
12+
* ...
13+
*
14+
* 斐波那契数列:1,1,2,3,5,8...
15+
*
16+
* 规律:从第三项开始,每一项是前两项之和。
17+
* 出口:前两项应该是已知。第一项1,第二项1
18+
*
19+
* 相邻的两个月我们用a,b表示
20+
* 第一个相邻:a=1,b=1
21+
* 第二个相邻:a=1,b=2
22+
* 第三个相邻:a=2,b=3
23+
* 第四个相邻:a=3,b=5
24+
* ...
25+
* 每次下一个相邻的a是以前的b,b是以前的a+b
26+
*/
27+
public class DiGuiTest {
28+
public static void main(String[] args) {
29+
// 用数组做
30+
int[] arr = new int[20];
31+
arr[0] = 1;
32+
arr[1] = 1;
33+
for (int x = 2; x < arr.length; x++) {
34+
arr[x] = arr[x - 1] + arr[x - 2];
35+
}
36+
System.out.println("第20个月的兔子对数是:" + arr[19]);
37+
38+
//用变量的变化来做
39+
int a = 1;
40+
int b = 1;
41+
for(int x=0; x<18; x++){
42+
int temp = a;//以前的a
43+
a = b;
44+
b = temp + b;
45+
}
46+
System.out.println("第20个月的兔子对数是:"+b);
47+
48+
//用递归实现
49+
System.out.println("第20个月的兔子对数是:"+fib(20));
50+
}
51+
52+
/*
53+
* 定义方法fib(int n)
54+
* fib(n)表示是第n个月的兔子对数。
55+
* 请问,n-1和n-2个月的兔子对数如何表示呢?
56+
* fib(n-1),fib(n-2)
57+
*/
58+
public static int fib(int n){
59+
if(n==1 || n==2){
60+
return 1;
61+
}else {
62+
return fib(n-1)+fib(n-2);
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.itcast_03;
2+
3+
import java.io.File;
4+
5+
/*
6+
/*
7+
* 删除带内容的目录
8+
* D:\demo
9+
*
10+
* 分析:
11+
* A:封装指定目录
12+
* B:获取指定目录下的所有文件或者文件夹的File数组
13+
* C:遍历该File数组,获取得到每一个File对象
14+
* D:判断该File对象
15+
* 是文件夹:回到B(递归)
16+
* 是文件:
17+
* 直接删除
18+
* E:删除文件夹
19+
*/
20+
public class DeleteFolderDemo {
21+
public static void main(String[] args) {
22+
// 封装指定目录
23+
File srcFolder = new File("d:\\demo");
24+
// 递归方法
25+
deleteFolder(srcFolder);
26+
}
27+
28+
public static void deleteFolder(File srcFolder) {
29+
// 获取指定目录下的所有文件或者文件夹的File数组
30+
File[] fileArray = srcFolder.listFiles();
31+
32+
// 删除除了系统文件以外的内容,我们是可以实现
33+
if (fileArray != null) {
34+
// 遍历该File数组,获取得到每一个File对象
35+
for (File file : fileArray) {
36+
if (file.isDirectory()) {
37+
deleteFolder(file);
38+
} else {
39+
System.out.println(file.getName() + "---" + file.delete());
40+
}
41+
}
42+
43+
System.out
44+
.println(srcFolder.getName() + "---" + srcFolder.delete());
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.itcast_03;
2+
3+
import java.io.File;
4+
5+
/*
6+
* 递归输出目录下指定后缀名结尾的文件绝对路径 .java
7+
* D:\itcast\20150306
8+
*
9+
* 分析:
10+
* A:封装指定目录
11+
* B:获取指定目录下的所有文件或者文件夹的File数组
12+
* C:遍历该File数组,获取得到每一个File对象
13+
* D:判断该File对象
14+
* 是文件夹:回到B(递归)
15+
* 是文件:
16+
* 判断是否以指定的后缀结尾
17+
* 是:就输出该文件的绝对路径
18+
*/
19+
public class GetAllFileNameDemo {
20+
public static int count1 = 0;
21+
public static int count2 = 0;
22+
23+
public static void main(String[] args) {
24+
// 封装指定目录
25+
File srcFolder = new File("D:\\itcast\\20150306");
26+
// 递归实现
27+
getAllJavaFilePath(srcFolder);
28+
System.out.println("共学习了" + count1 + "个java文件");
29+
System.out.println("共学习了" + count2 + "个avi文件");
30+
}
31+
32+
public static void getAllJavaFilePath(File srcFolder) {
33+
// 获取指定目录下的所有文件或者文件夹的File数组
34+
File[] fileArray = srcFolder.listFiles();
35+
// 遍历该File数组,获取得到每一个File对象
36+
for (File file : fileArray) {
37+
if (file.isDirectory()) {
38+
// 是文件夹:回到B(递归)
39+
getAllJavaFilePath(file);
40+
} else {
41+
// 判断是否以指定的后缀结尾
42+
if (file.getName().endsWith(".java")) {
43+
count1++;
44+
// 就输出该文件的绝对路径
45+
System.out.println(file.getAbsolutePath());
46+
}else if(file.getName().endsWith(".avi")){
47+
count2++;
48+
System.out.println(file.getName());
49+
}
50+
}
51+
}
52+
}
53+
}

day20/code/day20_IO/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day20/code/day20_IO/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day20_IO</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7

day20/code/day20_IO/a.txt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cn.itcast_02;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
6+
/*
7+
* �ֽ������������ݲ��裺
8+
* A:�����ֽ�����������
9+
* B:���÷�������ȡ����
10+
* C:�ͷ���Դ
11+
*
12+
* �ֽ���������ȡ���������ַ�ʽ:
13+
* A:һ�ζ�ȡһ���ֽ�
14+
* B:һ�ζ�ȡһ���ֽ�����
15+
*
16+
* һ�ζ�ȡһ���ֽڣ�public int read()
17+
*/
18+
public class FileInputStreamDemo {
19+
public static void main(String[] args) throws IOException {
20+
// �����ֽ�����������
21+
// FileInputStream fis = new FileInputStream("fis.txt");
22+
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
23+
24+
// ���÷�������ȡ����
25+
// ��һ�ζ�ȡ
26+
// int by = fis.read();
27+
// System.out.println(by);
28+
// System.out.println((char) by);
29+
//
30+
// // �ڶ��ζ�ȡ
31+
// by = fis.read();
32+
// System.out.println(by);
33+
// System.out.println((char) by);
34+
//
35+
// // �����ζ�ȡ
36+
// by = fis.read();
37+
// System.out.println(by);
38+
// System.out.println((char) by);
39+
// ���Ƿ�������Ĵ�����һ���ģ����ԣ����ǿ���Ҫ��ѭ���Ľ�
40+
// �����ѭ���Ľ���?
41+
// �ص�����
42+
// ��������ж�ѭ��ʲôʱ�����
43+
// by = fis.read();
44+
// System.out.println(by);
45+
// by = fis.read();
46+
// System.out.println(by);
47+
// ͨ���������Ƿ��֣�����ж�����ֵ��-1
48+
49+
// int by = fis.read();
50+
// while (by != -1) {
51+
// System.out.print((char) by);
52+
// by = fis.read();
53+
// }
54+
55+
// ���մ���
56+
int by = 0;
57+
// ��ȡ���ݣ���ֵ���ж�
58+
while ((by = fis.read()) != -1) {
59+
System.out.print((char) by);
60+
}
61+
62+
// �ͷ���Դ
63+
fis.close();
64+
}
65+
}

0 commit comments

Comments
 (0)