一、字节输入流
InputStream: 用来实现接口化编程
FileInputStream:用来读取文件数据的流
BufferedInputStream :封装别的流用来提高效率,提前将数据封装到内存中
1、FileInputStream使用
read()//读取一个字节 read(byte[] b)//读取b的长度个字节到b当中,返回值是读到的字节长度,返回-1说明读到了末尾。继承自InputStream read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾)。继承自InputStream close()//关闭流,释放资源.继承自InputStream FileInputStream input=new FileInputStream("path"); byte[] b=new byte[1024]; int len; while((len=input.read(b))!=-1){ System.out.print(new String(b,0,len)); }
2、BufferedInputStream
read()//读取一个字节 read(byte[] b)//读取b的长度个字节到b当中,返回值是读到的字节长度,返回-1说明读到了末尾。继承自InputStream read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾)。继承自InputStream close()//关闭流,释放资源.继承自InputStream FileInputStream input=new FileInputStream("path"); BufferedInputStream buffered=new BufferedInputStream(input); byte[] b=new byte[1024]; int len; while((len=buffered.read(b))!=-1){ System.out.print(new String(b,0,len)); } buffered.close(); input.close();
二、字节输出流
OutputStream: 用来实现接口化编程
FileOutputStream:用来读取文件数据的流
BufferedOutputStream :封装别的流用来提高效率,提前将数据封装到内存中
1、FileOutputStream
write(byte[] b):将b的长度个字节数据写到输出流中。 write(byte[] b,int off,int len):从b的off位置开始,获取len个字节数据,写到输 出流中。 flush():刷新输出流,把数据马上写到输出流中。 close():关闭流,释放系统资源。 write(int b)//将b转成一个字节数据,写到输出流中 FileOutputStream write=new FileOutputStream("path"); write.write("hellow".getBytes()); write.close();
1、FileOutputStream
write(byte[] b):将b的长度个字节数据写到输出流中。 write(byte[] b,int off,int len):从b的off位置开始,获取len个字节数据,写到输 出流中。 flush():刷新输出流,把数据马上写到输出流中。 close():关闭流,释放系统资源。 write(int b)//将b转成一个字节数据,写到输出流中 FileOutputStream write=new FileOutputStream("path"); BufferedOutputStream buffered=new BufferedOutputStream(write); buffered.write("hellow".getBytes()); buffered.close(); write.close();
三、字符输入流
1、Reader //是字符输入流的抽象基类 ,它定义了以下几个函数:
read() :读取单个字符,返回结果是一个int,需要转成char;到达流的末尾时,返回-1
read(char[] cbuf):读取cbuf的长度个字符到cbuf这种,返回结果是读取的字符数,到达流的末尾时,返回-1
close() :关闭流,释放占用的系统资源。
2、BufferedReader //可以把字符输入流进行封装,将数据进行缓冲,提高读取效率
除了Reader的函数还实现了
read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
readLine() :读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null
3、InputStreamReader//可以把InputStream中的字节数据流根据字符编码方式转成字符数据流
除了Reader的函数还实现了
read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
4、FileReader//可以把FileInputStream中的字节数据转成根据字符编码方式转成字符数据流
InputStreamReader in=new InputStreamRead(new FileInputStream("path")); int cha; while((cha=in.read())!=-1){ System.out.print((char)cha); } in.close(); FileReader in=new FileReader("path"); int len; char[] c=new char[1024]; while((len=in.read(c))!=-1){ System.out.print(new String(c,0,len)); } in.close(); FileReader in=new FileReader("path"); BufferedReader reader=new BufferedReader(in); String line; while((line=reader.readLine())!=null){ System.out.print(line); } reader.close(); in.close();
四、字符输出流
1、 Writer是字符输出流的抽象基类, ,它定义了以下几个函数
write(char[] cbuf) :往输出流写入一个字符数组。
write(int c) :往输出流写入一个字符。
write(String str) :往输出流写入一串字符串。
write(String str, int off, int len) :往输出流写入字符串的一部分。
close() :关闭流,释放资源。 【这个还是抽象的,写出来是说明有这个关闭功能】
flush():刷新输出流,把数据马上写到输出流中。 【这个还是抽象的,写出来是说明有这个关闭功能】
2、OutputStreamWriter可以使我们直接往流中写字符串数据,它里面会帮我们根据字符编码方式来把字符数据转成字节数据再写给输出流,它相当于一个中介\桥梁。
3、FileWriter与OutputStreamWriter功能类似,我们可以直接往流中写字符串数据,FileWriter内部会根据字符编码方式来把字符数据转成字节数据再写给输出流。
4、BufferedWriter比FileWriter还高级一点,它利用了缓冲区来提高写的效率。它还多出了一个函数:
newLine() :写入一个换行符。
FileInputStrea input=new FileInputStream("path"); OuterStreamWrite write =new OuterStreamWrite(input); write.write("hello"); write.close(); FileWrite write=new FileWrite("path"); write.write("hello"); write.close(); FileWrite write=new FileWrite("path"); BufferedWrite buffered=new BifferedWrite(write); buffered.write("hello"); buffered.close(); write.close();