Skip to content

Commit 37b6012

Browse files
committed
Java第二十一天
1 parent ed363eb commit 37b6012

File tree

110 files changed

+2288
-0
lines changed

Some content is hidden

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

110 files changed

+2288
-0
lines changed

day21/IO流小结图.bmp

2.24 MB
Binary file not shown.

day21/code/day21_IO/.classpath

Lines changed: 6 additions & 0 deletions
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>

day21/code/day21_IO/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day21_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>
Lines changed: 11 additions & 0 deletions
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

day21/code/day21_IO/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello���
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

day21/code/day21_IO/bos.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.itcast_01;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
7+
/*
8+
* 需求:请把项目路径下的闭月羞花.bmp复制到项目路径下的林青霞.bmp中
9+
* 数据源:
10+
* 闭月羞花.bmp -- FileInputStream
11+
* 目的地:
12+
* 林青霞.bmp -- FileOutputStream
13+
*/
14+
public class CopyImageDemo {
15+
public static void main(String[] args) throws IOException {
16+
// 封装数据源
17+
FileInputStream fis = new FileInputStream("闭月羞花.bmp");
18+
// 封装目的地
19+
FileOutputStream fos = new FileOutputStream("林青霞.bmp");
20+
21+
// 方式1
22+
// int by = 0;
23+
// while ((by = fis.read()) != -1) {
24+
// fos.write(by);
25+
// }
26+
27+
// 方式2
28+
byte[] bys = new byte[1024];
29+
int len = 0;
30+
while ((len = fis.read(bys)) != -1) {
31+
fos.write(bys, 0, len);
32+
}
33+
34+
// 释放资源
35+
fos.close();
36+
fis.close();
37+
}
38+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cn.itcast_02;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
10+
/*
11+
* 由于数组的方式一次比一个字节的方式快很多,所以,java本身在设计的时候,也考虑到了。
12+
* 就设计出了内置数组的缓冲区流。
13+
* 字节缓冲输入流
14+
* BufferedInputStream
15+
* 字节缓冲输出流
16+
* BufferedOutputStream
17+
*
18+
* 通过看构造方法,我们发现,缓冲流不能直接操作文件。
19+
* 是建立在基本的操作流之上的。
20+
* 所以,这种流也被称之为高级流。
21+
*/
22+
public class BufferedDemo {
23+
public static void main(String[] args) throws IOException {
24+
// 写数据
25+
// BufferedOutputStream(OutputStream out)
26+
// OutputStream out = new FileOutputStream("bos.txt");
27+
// BufferedOutputStream bos = new BufferedOutputStream(out);
28+
// BufferedOutputStream bos = new BufferedOutputStream(
29+
// new FileOutputStream("bos.txt"));
30+
// bos.write("hello".getBytes());
31+
// bos.close();
32+
33+
// 读数据
34+
// BufferedInputStream(InputStream is)
35+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
36+
"bos.txt"));
37+
38+
// 方式1
39+
int by = 0;
40+
while ((by = bis.read()) != -1) {
41+
System.out.print((char) by);
42+
}
43+
System.out.println("-----------------------");
44+
45+
// 方式2
46+
bis = new BufferedInputStream(new FileInputStream("bos.txt"));
47+
byte[] bys = new byte[1024];
48+
int len = 0;
49+
while ((len = bis.read(bys)) != -1) {
50+
System.out.print(new String(bys, 0, len));
51+
}
52+
53+
bis.close();
54+
}
55+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package cn.itcast_03;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
9+
/*
10+
* 数据源:
11+
* d:\\哥有老婆.mp4
12+
* 目的地:
13+
* 项目路径下copy.mp4
14+
*
15+
* 四种方式:
16+
* A:基本字节流一次读写一个字节 67023毫秒
17+
* B:基本字节流一次读写一个字节数组 共耗时:102毫秒
18+
* C:高效字节流一次读写一个字节 共耗时:650毫秒
19+
* D:高效字节流一次读写一个字节数组 共耗时:36毫秒
20+
*/
21+
public class CopyMP4Demo {
22+
public static void main(String[] args) throws IOException {
23+
long start = System.currentTimeMillis();
24+
// method1();
25+
// method2();
26+
// method3();
27+
method4();
28+
long end = System.currentTimeMillis();
29+
System.out.println("共耗时:" + (end - start) + "毫秒");
30+
}
31+
32+
// 基本字节流一次读写一个字节
33+
public static void method1() throws IOException {
34+
FileInputStream fis = new FileInputStream("d:\\哥有老婆.mp4");
35+
FileOutputStream fos = new FileOutputStream("copy1.mp4");
36+
37+
int by = 0;
38+
while ((by = fis.read()) != -1) {
39+
fos.write(by);
40+
}
41+
42+
fos.close();
43+
fis.close();
44+
}
45+
46+
// 基本字节流一次读写一个字节数组
47+
public static void method2() throws IOException {
48+
FileInputStream fis = new FileInputStream("d:\\哥有老婆.mp4");
49+
FileOutputStream fos = new FileOutputStream("copy2.mp4");
50+
51+
byte[] bys = new byte[1024];
52+
int len = 0;
53+
while ((len = fis.read(bys)) != -1) {
54+
fos.write(bys, 0, len);
55+
}
56+
57+
fos.close();
58+
fis.close();
59+
}
60+
61+
// 高效字节流一次读写一个字节
62+
public static void method3() throws IOException {
63+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
64+
"d:\\哥有老婆.mp4"));
65+
BufferedOutputStream bos = new BufferedOutputStream(
66+
new FileOutputStream("copy3.mp4"));
67+
68+
int by = 0;
69+
while ((by = bis.read()) != -1) {
70+
bos.write(by);
71+
}
72+
73+
bos.close();
74+
bis.close();
75+
}
76+
77+
// 高效字节流一次读写一个字节数组
78+
public static void method4() throws IOException {
79+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
80+
"d:\\哥有老婆.mp4"));
81+
BufferedOutputStream bos = new BufferedOutputStream(
82+
new FileOutputStream("copy4.mp4"));
83+
84+
byte[] bys = new byte[1024];
85+
int len = 0;
86+
while ((len = bis.read(bys)) != -1) {
87+
bos.write(bys, 0, len);
88+
}
89+
90+
bos.close();
91+
bis.close();
92+
}
93+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.itcast_04;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
6+
/*
7+
* 字节流操作中文,不是特别的方便,所以我们要想办法改进。这个时候就引出了转换流。
8+
* 可以把字节流转换为字符流。
9+
* 字符流 = 字节流+编码表
10+
*/
11+
public class FileInputStreamDemo {
12+
public static void main(String[] args) throws IOException {
13+
FileInputStream fis = new FileInputStream("a.txt");
14+
15+
// int by = 0;
16+
// while ((by = fis.read()) != -1) {
17+
// System.out.print((char) by);
18+
// }
19+
20+
byte[] bys = new byte[1024];
21+
int len = 0;
22+
while ((len = fis.read(bys)) != -1) {
23+
System.out.print(new String(bys, 0, len));
24+
}
25+
26+
fis.close();
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.itcast_04;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* 它可以自己把两个字节拼成一个汉字
7+
* 第一个字节肯定是负数。第二个可能是正数,大部分是负数。
8+
*/
9+
public class StringDemo {
10+
public static void main(String[] args) {
11+
// hello你好
12+
// String s = "hello";
13+
// byte[] bys = s.getBytes();
14+
// // [104, 101, 108, 108, 111]
15+
// System.out.println(Arrays.toString(bys));
16+
17+
String ss = "我爱你林青霞";
18+
byte[] bys = ss.getBytes();
19+
// [-50, -46, -80, -82, -60, -29, -63, -42, -57, -32, -49, -68]
20+
System.out.println(Arrays.toString(bys));
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.itcast_05;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.util.Arrays;
5+
6+
/*
7+
* String s = "你好";
8+
*
9+
* 编码:
10+
* 把你能够看懂的变成你看不懂
11+
* 解码:
12+
* 把你看不懂变成你能看懂的
13+
*
14+
* 谍战片:潜伏,回家看看去
15+
* 今天晚上老地方见 -- 1011110000111 -- 十进制 -- 找个小本子 -- 找字符
16+
*/
17+
public class StringDemo {
18+
public static void main(String[] args) throws UnsupportedEncodingException {
19+
String s = "你好";
20+
21+
// public byte[] getBytes()
22+
// byte[] bys = s.getBytes();
23+
// [-60, -29, -70, -61]
24+
// public byte[] getBytes(String charsetName)
25+
byte[] bys = s.getBytes("GBK");
26+
// [-60, -29, -70, -61]
27+
// byte[] bys = s.getBytes("UTF-8");
28+
// [-28, -67, -96, -27, -91, -67]
29+
System.out.println(Arrays.toString(bys));
30+
31+
// String(byte[] bytes)
32+
// String ss = new String(bys);
33+
// public String(byte[] bytes, String charsetName)
34+
String ss = new String(bys, "GBK");
35+
// String ss = new String(bys, "UTF-8");
36+
System.out.println(ss);
37+
}
38+
}

day21/code/day21_IO/林青霞.bmp

2.24 MB
Binary file not shown.

day21/code/day21_IO/闭月羞花.bmp

2.24 MB
Binary file not shown.

day21/code/day21_IO2/.classpath

Lines changed: 6 additions & 0 deletions
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>

day21/code/day21_IO2/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day21_IO2</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>
Lines changed: 11 additions & 0 deletions
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

day21/code/day21_IO2/Copy.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.itcast_01;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
/*
8+
* InputStreamReader(InputStream in)
9+
* InputStreamReader(InputStream in, String charsetName)
10+
*/
11+
public class InputStreamReaderDemo {
12+
public static void main(String[] args) throws IOException {
13+
// 创建对象
14+
InputStreamReader isr = new InputStreamReader(new FileInputStream(
15+
"isr.txt"));
16+
17+
// 一次读取一个字符
18+
// int ch = 0;
19+
// while ((ch = isr.read()) != -1) {
20+
// System.out.print((char) ch);
21+
// }
22+
23+
// 一次读取一个字符数组
24+
char[] chs = new char[1024];
25+
int len = 0;
26+
while ((len = isr.read(chs)) != -1) {
27+
System.out.print(new String(chs, 0, len));
28+
}
29+
30+
// 释放资源
31+
isr.close();
32+
}
33+
}

0 commit comments

Comments
 (0)