先来看一下流的概念:
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。
字节流与字符流
在java.io包中操作文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用outputstream完成,输入使的是inputstream,在字符流中输出主要是使用writer类完成,输入流主要使用reader类完成。(这四个都是抽象类)
操作流程
在java中io操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:
使用file类打开一个文件
通过字节流或字符流的子类,指定输出的位置
进行读/写操作
关闭输入/输出
io操作属于资源操作,一定要记得关闭
字节流
字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是outputstream、inputstream
字节输出流:outputstream
outputstream是整个io包中字节输出流的最大父类,此类的定义如下:
public abstract class outputstream extends object implements closeable,flushable
从以上的定义可以发现,此类是一个抽象类,如果想要使用此类的话,则首先必须通过子类实例化对象,那么如果现在要操作的是一个文件,则可以使用:fileoutputstream类。通过向上转型之后,可以为outputstream实例化
closeable表示可以关闭的操作,因为程序运行到最后肯定要关闭
flushable:表示刷新,清空内存中的数据
fileoutputstream类的构造方法如下:
public fileoutputstream(file file)throws filenotfoundexception
写数据:
1 import java.io.file;
2 import java.io.fileoutputstream;
3 import java.io.ioexception;
4 import java.io.outputstream;
5
6 public class test11 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9outputstream out=new fileoutputstream(f);//如果文件不存在会自动创建
10string str="hello world";
11byte[] b=str.getbytes();
12out.write(b);//因为是字节流,所以要转化成字节数组进行输出
13out.close();
14}
15 }
也可以一个字节一个字节进行输出,如下:
1 import java.io.file;
2 import java.io.fileoutputstream;
3 import java.io.ioexception;
4 import java.io.outputstream;
5
6 public class test11 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9outputstream out=new fileoutputstream(f);//如果文件不存在会自动创建
10string str="hello world";
11byte[] b=str.getbytes();
12for(int i=0;i13out.write(b[i]);
14}
15out.close();
16}
17 }
以上输出只会进行覆盖,如果要追加的话,请看fileoutputstream类的另一个构造方法:
public fileoutputstream(file file,boolean append)throws filenotfoundexception
在构造方法中,如果将append的值设置为true,则表示在文件的末尾追加内容。
1 import java.io.file;
2 import java.io.fileoutputstream;
3 import java.io.ioexception;
4 import java.io.outputstream;
5
6 public class test11 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9outputstream out=new fileoutputstream(f,true);//追加内容
10string str="\r\nhello world";
11byte[] b=str.getbytes();
12for(int i=0;i13out.write(b[i]);
14}
15out.close();
16}
17 }
文件中换行为:\r\n
字节输入流:inputstream
既然程序可以向文件中写入内容,则就可以通过inputstream从文件中把内容读取进来,首先来看inputstream类的定义:
public abstract class inputstream extends object implements closeable
与outputstream类一样,inputstream本身也是一个抽象类,必须依靠其子类,如果现在是从文件中读取,就用fileinputstream来实现。
观察fileinputstream类的构造方法:
public fileinputstream(file file)throws filenotfoundexception
读文件:
1 import java.io.file;
2 import java.io.fileinputstream;
3 import java.io.ioexception;
4 import java.io.inputstream;
5
6 public class test12 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9inputstream in=new fileinputstream(f);
10byte[] b=new byte[1024];
11int len=in.read(b);
12in.close();
13system.out.println(new string(b,0,len));
14}
15 }
但以上方法是有问题的,用不用开辟这么大的一个字节数组,明显是浪费嘛,我们可以根据文件的大小来定义字节数组的大小,file类中的方法:public long length()
1 import java.io.file;
2 import java.io.fileinputstream;
3 import java.io.ioexception;
4 import java.io.inputstream;
5
6 public class test13 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9inputstream in=new fileinputstream(f);
10byte[] b=new byte[(int) f.length()];
11in.read(b);
12in.close();
13system.out.println(new string(b));
14}
15 }
我们换种方式,一个字节一个字节读入~
1 import java.io.file;
2 import java.io.fileinputstream;
3 import java.io.ioexception;
4 import java.io.inputstream;
5
6 public class test14 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9inputstream in=new fileinputstream(f);
10byte[] b=new byte[(int) f.length()];
11for(int i=0;i12b[i]=(byte) in.read();
13}
14in.close();
15system.out.println(new string(b));
16}
17 }
但以上情况只适合知道输入文件的大小,不知道的话用如下方法:
1 import java.io.file;
2 import java.io.fileinputstream;
3 import java.io.ioexception;
4 import java.io.inputstream;
5
6 public class test15 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9inputstream in=new fileinputstream(f);
10byte[] b=new byte[1024];
11int temp=0;
12int len=0;
13while((temp=in.read())!=-1){//-1为文件读完的标志
14b[len]=(byte) temp;
15len++;
16}
17in.close();
18system.out.println(new string(b,0,len));
19}
20 }
字符流
在程序中一个字符等于两个字节,那么java提供了reader、writer两个专门操作字符流的类。
字符输出流:writer
writer本身是一个字符流的输出类,此类的定义如下:
public abstract class writer extends object implements appendable,closeable,flushable
此类本身也是一个抽象类,如果要使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用filewriter的子类。
filewriter类的构造方法定义如下:
public filewriter(file file)throws ioexception
字符流的操作比字节流操作好在一点,就是可以直接输出字符串了,不用再像之前那样进行转换操作了。
写文件:
1 import java.io.file;
2 import java.io.filewriter;
3 import java.io.ioexception;
4 import java.io.writer;
5
6 public class test16 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9writer out=new filewriter(f);
10string str="hello world";
11out.write(str);
12out.close();
13}
14 }
在默认情况下再次输出会覆盖,追加的方法也是在构造函数上加上追加标记
1 import java.io.file;
2 import java.io.filewriter;
3 import java.io.ioexception;
4 import java.io.writer;
5
6 public class test17 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9writer out=new filewriter(f,true);//追加
10string str="\r\nhello world";
11out.write(str);
12out.close();
13}
14 }
字符输入流:reader
reader是使用字符的方式从文件中取出数据,reader类的定义如下:
public abstract class reader extends objects implements readable,closeable
reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用filereader子类。
filereader的构造方法定义如下:
public filereader(file file)throws filenotfoundexception
以字符数组的形式读取出数据:
1 import java.io.file;
2 import java.io.filereader;
3 import java.io.ioexception;
4 import java.io.reader;
5
6 public class test18 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9reader input=new filereader(f);
10char[] c=new char[1024];
11int len=input.read(c);
12input.close();
13system.out.println(new string(c,0,len));
14}
15 }
也可以用循环方式,判断是否读到底:
1 import java.io.file;
2 import java.io.filereader;
3 import java.io.ioexception;
4 import java.io.reader;
5
6 public class test19 {
7public static void main(string[] args) throws ioexception {
8file f = new file("d:" + file.separator+"test.txt");
9reader input=new filereader(f);
10char[] c=new char[1024];
11int temp=0;
12int len=0;
13while((temp=input.read())!=-1){
14c[len]=(char) temp;
15len++;
16}
17input.close();
18system.out.println(new string(c,0,len));
19}
20 }
字节流与字符流的区别
字节流和字符流使用是非常相似的,那么除了操作代码的不同之外,还有哪些不同呢?
字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的
字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close方法的话,则不会输出任何内容,说明字符流用的是缓冲区,并且可以使用flush方法强制进行刷新缓冲区,这时才能在不close的情况下输出内容
那开发中究竟用字节流好还是用字符流好呢?
在所有的硬盘上保存文件或进行传输的时候都是以字节的方法进行的,包括图片也是按字节完成,而字符是只有在内存中才会形成的,所以使用字节的操作是最多的。
如果要java程序实现一个拷贝功能,应该选用字节流进行操作(可能拷贝的是图片),并且采用边读边写的方式(节省内存)。
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/