您好,欢迎访问一九零五行业门户网

关于java中I/O流的字节流与字符流的详细介绍

一、绪论
如果要进行文件内容的操作那么必须依靠数据流完成,而数据流分为两种:
字节流:inputstream(字节输入流)、outputstream(字节输出流);
字符流:reader(字符输入流)、writer(字符输出流);
二、区别
字节流是原生的操作,而字符流是经过处理后的操作。
在进行网络数据传输、磁盘数据保存所保存所支持的数据类型只有:字节。而所有磁盘中的数据必须先读取到内存后才能进行操作,而内存中会帮助我们把字节变为字符。字符更加适合处理中文。如果处理中文使用字符流,其他的任何数据都使用字节流。
相关学习视频推荐:java学习视频
三、字节输出流:(outputstream)
outputstream类定义有三个重要的输出操作方法:
1. 将给定的字节数组内容全部输出:
public void write(byte b[]) throws ioexception
2. 将部分字节数组内容输出:(重点)
public void write(byte b[], int off, int len) throws ioexception
3. 输出单个字节:
public abstract void write(int b) throws ioexception
outputstream是一个抽象类,按照抽象类的基本原则来讲,如果想要取得outputstream类的实例化对象那么一定需要子类,如果要进行文件的操作,可以使用fileoutputstream类来处理,这个类的构造方法如下:
1. 接收file类(覆盖):
public fileoutputstream(file file) throws filenotfoundexception
2. 接收file类(追加):
public fileoutputstream(file file, boolean append)
//第一步:定义要输出的文件的file类对象 file file = new file("e:"+file.separator+"hello"+file.separator+"my.txt"); //输出信息的时候文件可以不存在,但是目录必须存在 if(!file.getparentfile().exists()) {//父路径不存在 file.getparentfile().mkdirs();//创建父路径 } //第二步:利用outputstream的子类为父类进行实例化 outputstream output = new fileoutputstream(file); //第三步:输出文字信息 string msg = "富则达济天下,穷则独善其身";//字符串 //为了方便输出需要将字符串变为字节数组 byte data[] = msg.getbytes();//变为字节数组 output.write(data);//输出数据 output.close();//关闭流
输出文件的部分内容
output.write(data,0,10);//输出部分数据
使用循环方式进行单个字节的信息输出
for(int x = 0;x < data.length; x++) { output.write(data[x]);//单个字节输出数据 }
但是使用单个字节输出会将之前的内容都被覆盖了。所以需要进行数据的追加操作
outputstream output = new fileoutputstream(file,true);//此处为追加操作
四、字节输入流:(inputstream)
inputstream类中定义有三个数据的读取操作方法:
1.读取单个字节:
public abstract int read() throws ioexception;
每次执行此方法将读取当个字节数据,如果已经读取完成了,那么最后返回-1。
2.读取数据到字节数组中:
public int read(byte b[]) throws ioexception.
最常用方法,每次讲数据读取到数组之中,那么会返回一个读取长度的数据,如果没有数据则返回的长度为-1,
可是要考虑两种情况:
要读取的内容大于开辟的数组内容:长度就是整个数组的长度。
要读取的内容小于开辟数组的内容,长度就是全部最后的内容长度,数组装不满。
3.读取部分内容到字节数组中:
public int read(byte b[], int off,int len) throws ioexception
每次读取内容到部分字节数组,只允许读取满限制的数组的字节个数。此方法依然会返回读取的长度。
inputstream是一个抽象类,所以要进行文件的读取使用fileinputstream子类,子类定义的构造方法如下:
构造方法:public fileinputstream(file file) throws filenotfoundexception.
//第一步:定义要输出的文件的file类对象 file file = new file("e:"+file.separator+"hello"+file.separator+"my.txt"); //第二步:实例化inputstream inputstream input = new fileinputstream(file); //实现数据的读取操作 byte data[] = new byte[1024]; int len = input.read(data);//将数据读取到数组之中 system.out.println("读取的内容【" +new string(data,0,len)+"】"); //第四步关闭输入流 input.close();
补充:datainputstream 和 dataoutputstream
datainputstream类继承了inputstream。也就是说datainputstream是inputstream的子类。但它们同是实现了datainput接口。
dataoutputstream类继承了outputstream。也就是说dataoutputstream是outputstream的子类。但它们同是实现了dataoutput接口。
五、字符输出流:(writer)
writer是一个抽象类,要进行文件字符流操作可以使用filewriter类处理,其构造方法为:
public filewriter(file file)
//第一步:定义要输出的文件的file类对象 file file = new file("e:"+file.separator+"hello"+file.separator+"my.txt");//你的路径 if(!file.getparentfile().exists()) { file.getparentfile().mkdirs(); } writer out = new filewriter(file); string str = "一定要好好学习,天天向上。。。"; out.write(str); out.close();
虽然writer类提供有字符数组的输出操作能力,但是从本质上来讲使用writer类就意味着要执行字符串的直接输出。字符流最适合操作中文,但并不意味着字节流就无法操作中文。
六、字符输入流:(reader)
reader是一个抽象类,要进行文件字符流操作可以使用filereader类处理,其构造方法为:
public filereader (file file)
//第一步:定义要输出的文件的file类对象 file file = new file("e:"+file.separator+"hello"+file.separator+"my.txt");//你的路径 if(file.exists()) { reader in = new filereader(file); char data[] = new char[1024]; int len = in.read(data);//向字符数组保存数据,返回长度。 system.out.println(new string(data,0,len)); in.close();
七、补充
为了提高字符流读写的效率,引入了缓冲机制,java提供了缓存流类:bufferedinputstream、bufferedoutputstream 类和bufferedreader、 bufferedwriter类
//使用buffer进行文件读写bufferedinputstream bufferedinputstream = new bufferedinputstream(new fileinputstream(new file("d:\\bigbang - if you (live).mp3")));file newfile = new file("d:\\copymusic\\bigbang - if you (live).mp3");newfile.createnewfile();bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(new fileoutputstream(newfile));byte[] bytes= new byte[1024];int length = 0;while ((length=bufferedinputstream.read(bytes))!=-1){ bufferedoutputstream.write(bytes,0,length);}
相关文章教程推荐:java开发入门
以上就是关于java中i/o流的字节流与字符流的详细介绍的详细内容。
其它类似信息

推荐信息