javaIO流讲解博客 3.docx
《javaIO流讲解博客 3.docx》由会员分享,可在线阅读,更多相关《javaIO流讲解博客 3.docx(23页珍藏版)》请在冰豆网上搜索。
![javaIO流讲解博客 3.docx](https://file1.bdocx.com/fileroot1/2023-1/26/f51e63c9-7a91-4b0c-b2c1-5888d2444f86/f51e63c9-7a91-4b0c-b2c1-5888d2444f861.gif)
javaIO流讲解博客3
所谓IO,也就是Input与Output的缩写。
在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写
其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后)
对于文件内容的操作主要分为两大类
分别是:
字符流
字节流
其中,字符流有两个抽象类:
Writer Reader
其对应子类FileWriter和FileReader可实现文件的读写操作
BufferedWriter和BufferedReader能够提供缓冲区功能,用以提高效率
同样,字节流也有两个抽象类:
InputStream OutputStream
其对应子类有FileInputStream和FileOutputStream实现文件读写
BufferedInputStream和BufferedOutputStream提供缓冲区功能
俺当初学IO的时候犯了不少迷糊,网上有些代码也无法通过编译,甚至风格都很大不同,所以新手请注意:
1.本文代码较长,不该省略的都没省略,主要是因为作为一个新手需要养成良好的代码编写习惯
2.本文在linux下编译,类似于File.pathSeparator和File.separator这种表示方法是出于跨平台性和健壮性考虑
3.代码中有些操作有多种执行方式,我采用了方式1...方式2...的表述,只需轻轻解开注释便可编译
4.代码中并没有在主方法上抛出异常,而是分别捕捉,造成代码过长,如果仅是测试,或者不想有好的编程习惯,那你就随便抛吧……
5.功能类似的地方就没有重复写注释了,如果新手看不懂下面的代码,那肯定是上面的没有理解清楚
字符流
实例1:
字符流的写入
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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Demo{
public static void main(String[]args){
//创建要操作的文件路径和名称
//其中,File.separator表示系统相关的分隔符,Linux下为:
/ Windows下为:
\\
Stringpath=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "work" +File.separator+ "demo.txt";
//由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用
FileWriterw= null;
try {
//以path为路径创建一个新的FileWriter对象
//如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
w= new FileWriter(path);
//将字符串写入到流中,\r\n表示换行想有好的
w.write("Nerxiousisagoodboy\r\n");
//如果想马上看到写入效果,则需要调用w.flush()方法
w.flush();
} catch (IOExceptione){
e.printStackTrace();
} finally {
//如果前面发生异常,那么是无法产生w对象的
//因此要做出判断,以免发生空指针异常
if(w!
= null){
try {
//关闭流资源,需要再次捕捉异常
w.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
}
}
}
编译之后,在目录下面生成文件,并写入字符串
实例2:
字符流的读取
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Demo2{
public static void main(String[]args){
Stringpath=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "work" +File.separator+ "demo.txt";
FileReaderr= null;
try {
r= new FileReader(path);
//方式一:
读取单个字符的方式
//每读取一次,向下移动一个字符单位
int temp1=r.read();
System.out.println((char)temp1);
int temp2=r.read();
System.out.println((char)temp2);
//方式二:
循环读取
//read()方法读到文件末尾会返回-1
/*
while(true){
inttemp=r.read();
if(temp==-1){
break;
}
System.out.print((char)temp);
}
*/
//方式三:
循环读取的简化操作
//单个字符读取,当temp不等于-1的时候打印字符
/*inttemp=0;
while((temp=r.read())!
=-1){
System.out.print((char)temp);
}
*/
//方式四:
读入到字符数组
/*
char[]buf=newchar[1024];
inttemp=r.read(buf);
//将数组转化为字符串打印,后面参数的意思是
//如果字符数组未满,转化成字符串打印后尾部也许会出现其他字符
//因此,读取的字符有多少个,就转化多少为字符串
System.out.println(newString(buf,0,temp));
*/
//方式五:
读入到字符数组的优化
//由于有时候文件太大,无法确定需要定义的数组大小
//因此一般定义数组长度为1024,采用循环的方式读入
/*
char[]buf=newchar[1024];
inttemp=0;
while((temp=r.read(buf))!
=-1){
System.out.print(newString(buf,0,temp));
}
*/
} catch (IOExceptione){
e.printStackTrace();
} finally {
if(r!
= null){
try {
r.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
}
}
}
编译之后的效果:
实例3:
文本文件的复制
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
55
56
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo{
public static void main(String[]args){
Stringdoc=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "work" +File.separator+ "demo.txt";
Stringcopy=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "life" +File.separator+ "lrc.txt";
FileReaderr= null;
FileWriterw= null;
try {
r= new FileReader(doc);
w= new FileWriter(copy);
//方式一:
单个字符写入
int temp= 0;
while((temp=r.read())!
=-1){
w.write(temp);
}
//方式二:
字符数组方式写入
/*
char[]buf=newchar[1024];
inttemp=0;
while((temp=r.read(buf))!
=-1){
w.write(newString(buf,0,temp));
}
*/
} catch (IOExceptione){
e.printStackTrace();
} finally {
//分别判断是否空指针引用,然后关闭流
if(r!
= null){
try {
r.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
if(w!
= null){
try {
w.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
}
}
}
编译之后,产生life目录下的lrc.txt文件,复制成功
实例4:
利用字符流的缓冲区来进行文本文件的复制
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
55
56
57
58
59
60
61
62
63
64
65
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo{
public static void main(String[]args){
Stringdoc=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "work" +File.separator+ "demo.txt";
Stringcopy=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "life" +File.separator+ "lrc.txt";
FileReaderr= null;
FileWriterw= null;
//创建缓冲区的引用
BufferedReaderbr= null;
BufferedWriterbw= null;
try {
r= new FileReader(doc);
w= new FileWriter(copy);
//创建缓冲区对象
//将需要提高效率的FileReader和FileWriter对象放入其构造函数内
//当然,也可以使用匿名对象的方式br=newBufferedReader(newFileReader(doc));
br= new BufferedReader(r);
bw= new BufferedWriter(w);
Stringline= null;
//读取行,直到返回null
//readLine()方法只返回换行符之前的数据
while((line=br.readLine())!
= null){
//使用BufferWriter对象的写入方法
bw.write(line);
//写完文件内容之后换行
//newLine()方法依据平台而定
//windows下的换行是\r\n
//Linux下则是\n
bw.newLine();
}
} catch (IOExceptione){
e.printStackTrace();
} finally {
//此处不再需要捕捉FileReader和FileWriter对象的异常
//关闭缓冲区就是关闭缓冲区中的流对象
if(br!
= null){
try {
r.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
if(bw!
= null){
try {
bw.close();
} catch (IOExceptione){
e.printStackTrace();
}
}
}
}
}
字节流
实例5:
字节流的写入
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
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo{
public static void main(String[]args){
Stringpath=File.separator+ "home" +File.separator+ "siu" +
File.separator+ "work" +File.separator+ "demo.txt";
FileOutputStreamo= null;
try {
o= new FileOutputStream(path);
Stringstr= "Nerxiousisagoodboy\r\n";
byte[]buf=str.getBytes();
//也可以直接使用o.write("String".getBytes());
//因为字符串就是一个对象,能直接调用方法
o.write(buf);
} catch (IOExceptione){
e.printStackTrace();
} finally {
if(o!
= null){
try {
o.close();