Java IO流-Reader和Writer
主要用于文本的读取和写入,一般使用的实现类是FileReader和FileWriter;
我们给出一些实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileReader;
import
java.io.Reader;
public
class
Demo1 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Reader reader=
new
FileReader(file);
char
c[]=
new
char
[
1024
];
// 字符数组
int
len=reader.read(c);
reader.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(c,
0
,len));
}
}
|
直接读取;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileReader;
import
java.io.Reader;
public
class
Demo2 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Reader reader=
new
FileReader(file);
char
c[]=
new
char
[
1024
];
// 字符数组
int
temp=
0
;
int
len=
0
;
while
((temp=reader.read())!=-
1
){
c[len++]=(
char
)temp;
}
reader.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(c,
0
,len));
}
}
|
一个一个字符读取;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.Writer;
public
class
Demo3 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Writer out=
new
FileWriter(file);
String str=
"我爱中华"
;
out.write(str);
// 将字符串写入输出流
out.close();
// 关闭输出流
}
}
|
写入文件;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.Writer;
public
class
Demo4 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Writer out=
new
FileWriter(file,
true
);
String str=
"我爱中华2"
;
out.write(str);
// 将字符串写入输出流
out.close();
// 关闭输出流
}
}
|
追加写入;
Java IO流-BufferedInputStream和BufferedOutputStream
带缓冲的输入和输出流;
这里缓冲的概念,就是在A,B之间建立内存缓冲区,读取得快,就先放缓冲区,然后再从缓冲区写入指定目标,和没有缓冲比,效率快很多。
我们给个实例比对下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package
com.java1234.chap10.sec03;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
public
class
Demo6 {
/**
* 缓冲
* @throws Exception
*/
public
static
void
bufferStream()
throws
Exception{
// 定义了一个带缓冲的字节输入流
BufferedInputStream bufferedInputStream=
new
BufferedInputStream(
new
FileInputStream(
"C://《一头扎进J2SE》V2.0视频笔录2.doc"
));
// 定义了一个带缓冲的字节输出流
BufferedOutputStream bufferedOutputStream=
new
BufferedOutputStream(
new
FileOutputStream(
"C://复制的《一头扎进J2SE》V2.0视频笔录2.doc"
));
int
b=
0
;
long
startTime=System.currentTimeMillis();
// 开始时间
while
((b=bufferedInputStream.read())!=-
1
){
bufferedOutputStream.write(b);
}
bufferedInputStream.close();
bufferedOutputStream.close();
long
endTime=System.currentTimeMillis();
// 结束时间
System.out.println(
"缓冲花费的时间是:"
+(endTime-startTime));
}
/**
* 非缓冲
* @throws Exception
*/
public
static
void
stream()
throws
Exception{
InputStream inputStream=
new
FileInputStream(
"C://《一头扎进J2SE》V2.0视频笔录.doc"
); // 定义一个输入流
OutputStream outputStream=
new
FileOutputStream(
"C://复制的《一头扎进J2SE》V2.0视频笔录.doc"
);
int
b=
0
;
long
startTime=System.currentTimeMillis();
// 开始时间
while
((b=inputStream.read())!=-
1
){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long
endTime=System.currentTimeMillis();
// 结束时间
System.out.println(
"非缓冲花费的时间是:"
+(endTime-startTime));
}
public
static
void
main(String[] args)
throws
Exception {
stream();
bufferStream();
}
}
|
把文件从A地址复制到B地址,运行输出:
非缓冲花费的时间是:2368
缓冲花费的时间是:31
我们明显发现 带缓冲的效率高;
Java IO流-InputStream和OutputStream
InputStream是输入流 OutputStream是输出流;
InputStream输入流可以把文件从硬盘读取到内存;
OutputStream输出流可以把文件从内存写入到硬盘;
我们实际使用的都是InputStream和OutputStream的子类;
比如文件操作方面用的是FileInputStream和FileOutputStream;
我们给下实例,视频教程里会详细讲解:
准备工作,我们在C盘建一个txt文件 测试文件.txt
随便加点内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.java1234.chap10.sec03;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.InputStream;
public
class
Demo1 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
InputStream inputStream=
new
FileInputStream(file);
// 实例化FileInputStream
byte
b[]=
new
byte
[
1024
];
int
len=inputStream.read(b);
inputStream.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(b,
0
,len));
}
}
|
把文件从硬盘读取到内存,并且输出:
运行结果:
读取的内容是:我是人 www.java1234.com
上面那个是定义了固定字节数组 一批读取的,我们现在改进下,获取文件长度,然后定义指定字节数组的长度;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
com.java1234.chap10.sec03;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.InputStream;
public
class
Demo2 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
InputStream inputStream=
new
FileInputStream(file);
// 实例化FileInputStream
int
fileLength=(
int
)file.length();
byte
b[]=
new
byte
[fileLength];
inputStream.read(b);
inputStream.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(b));
}
}
|
运行结果也一样;
我们再来一种方式 一个字节一个字节读取;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
com.java1234.chap10.sec03;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.InputStream;
public
class
Demo3 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
InputStream inputStream=
new
FileInputStream(file);
// 实例化FileInputStream
int
fileLength=(
int
)file.length();
byte
b[]=
new
byte
[fileLength];
int
temp=
0
;
int
len=
0
;
while
((temp=inputStream.read())!=-
1
){
// 一个字节一个字节读取,放到b字节数组里
b[len++]=(
byte
)temp;
}
inputStream.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(b));
}
}
|
运行结果还是一样的;
下面是讲下输出流;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.java1234.chap10.sec03;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.OutputStream;
public
class
Demo4 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
OutputStream out=
new
FileOutputStream(file);
String str=
"你好,我好,大家好,Java好"
;
byte
b[]=str.getBytes();
out.write(b);
// 将b字节数组写入到输出流
out.close();
// 关闭输出流
}
}
|
我们把指定文件写入到文件;
上面那种是直接覆盖的,我们再来一个追加的;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.java1234.chap10.sec03;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.OutputStream;
public
class
Demo5 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
OutputStream out=
new
FileOutputStream(file,
true
);
String str=
"你好,我好,大家好,Java好"
;
byte
b[]=str.getBytes();
out.write(b);
// 将b字节数组写入到输出流
out.close();
// 关闭输出流
}
}
|
BufferedInputStream和BufferedOutputStream
带缓冲的输入和输出流;
这里缓冲的概念,就是在A,B之间建立内存缓冲区,读取得快,就先放缓冲区,然后再从缓冲区写入指定目标,和没有缓冲比,效率快很多。
我们给个实例比对下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package
com.java1234.chap10.sec03;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
public
class
Demo6 {
/**
* 缓冲
* @throws Exception
*/
public
static
void
bufferStream()
throws
Exception{
// 定义了一个带缓冲的字节输入流
BufferedInputStream bufferedInputStream=
new
BufferedInputStream(
new
FileInputStream(
"C://《一头扎进J2SE》V2.0视频笔录2.doc"
));
// 定义了一个带缓冲的字节输出流
BufferedOutputStream bufferedOutputStream=
new
BufferedOutputStream(
new
FileOutputStream(
"C://复制的《一头扎进J2SE》V2.0视频笔录2.doc"
));
int
b=
0
;
long
startTime=System.currentTimeMillis();
// 开始时间
while
((b=bufferedInputStream.read())!=-
1
){
bufferedOutputStream.write(b);
}
bufferedInputStream.close();
bufferedOutputStream.close();
long
endTime=System.currentTimeMillis();
// 结束时间
System.out.println(
"缓冲花费的时间是:"
+(endTime-startTime));
}
/**
* 非缓冲
* @throws Exception
*/
public
static
void
stream()
throws
Exception{
InputStream inputStream=
new
FileInputStream(
"C://《一头扎进J2SE》V2.0视频笔录.doc"
); // 定义一个输入流
OutputStream outputStream=
new
FileOutputStream(
"C://复制的《一头扎进J2SE》V2.0视频笔录.doc"
);
int
b=
0
;
long
startTime=System.currentTimeMillis();
// 开始时间
while
((b=inputStream.read())!=-
1
){
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long
endTime=System.currentTimeMillis();
// 结束时间
System.out.println(
"非缓冲花费的时间是:"
+(endTime-startTime));
}
public
static
void
main(String[] args)
throws
Exception {
stream();
bufferStream();
}
}
|
把文件从A地址复制到B地址,运行输出:
非缓冲花费的时间是:2368
缓冲花费的时间是:31
我们明显发现 带缓冲的效率高
Java IO流-Reader和Writer
主要用于文本的读取和写入,一般使用的实现类是FileReader和FileWriter;
我们给出一些实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileReader;
import
java.io.Reader;
public
class
Demo1 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Reader reader=
new
FileReader(file);
char
c[]=
new
char
[
1024
];
// 字符数组
int
len=reader.read(c);
reader.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(c,
0
,len));
}
}
|
直接读取;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileReader;
import
java.io.Reader;
public
class
Demo2 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Reader reader=
new
FileReader(file);
char
c[]=
new
char
[
1024
];
// 字符数组
int
temp=
0
;
int
len=
0
;
while
((temp=reader.read())!=-
1
){
c[len++]=(
char
)temp;
}
reader.close();
// 关闭输入流
System.out.println(
"读取的内容是:"
+
new
String(c,
0
,len));
}
}
|
一个一个字符读取;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.Writer;
public
class
Demo3 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Writer out=
new
FileWriter(file);
String str=
"我爱中华"
;
out.write(str);
// 将字符串写入输出流
out.close();
// 关闭输出流
}
}
|
写入文件;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.java1234.chap10.sec04;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.Writer;
public
class
Demo4 {
public
static
void
main(String[] args)
throws
Exception {
File file=
new
File(
"C://测试文件.txt"
);
Writer out=
new
FileWriter(file,
true
);
String str=
"我爱中华2"
;
out.write(str);
// 将字符串写入输出流
out.close();
// 关闭输出流
}
}
|
追加写入;