outputStream.write(bys,1,3);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
InputStream的子类FileInputStream
FileInputStream的构造方法
FileInputStream(File file)
FileInputStream(String name)
推荐第二种构造方法:
FileInputStream inputStream = new FileInputStream(“a.txt”);
把刚才写的数据现在读取到控制台:
public int read()
public int read(byte[] b)
第一个read是读一个字节,第二个read是读一个字节数组。
//读一个字节
int by = 0;
while
需要文中资料的朋友,可以加我\/信获取:vip1024b 备注Java
((by=inputStream.read())!=-1){
System.out.println((char)by);
}
读到没数据了就返回-1,这个用来判断是否读完。
//读一个字节数组,一般是1024大小
int len = 0 ;
byte[] bys = new byte[1024];
while ((len = inputStream.read(bys)) != -1) {
System.out.println(new String(bys,0,len));
}
两个read的返回值略有不同,read()返回读取的字节,读到末尾返回-1,read(byte[] b)返回的是读到的字节个数,读到的字节放在了bytes字节数组里,读到末尾没数据了返回-1。
两种读取方式图解:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Yv0oyKLy-1603813798619)(http://p5kllyq5h.bkt.clouddn.com/174401.jpg)]
同样的用完了流,也要及时的关闭,以防占用内存。
inputStream.close();
完整示例:
建议以字节数组的方式读取数据。
package io2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
-
Create by stefan
-
Date on 2018-05-27 23:00
-
Convertion over Configuration!
*/
public class input2 {
public static void main(String args[]){
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(“a.txt”);
// byte[] bys = new byte[4];
// int len = inputStream.read(bys);
// System.out.println(new String(bys)); //bcd
// System.out.println(len); //3
// System.out.println(inputStream.read(bys)); //-1
int len = 0 ;
byte[] bys = new byte[1024];
while ((len = inputStream.read(bys)) != -1) {
System.out.println(new String(bys,0,len));
}
/**
- public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
*/
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节流复制文件
利用输入流读取一个文件里的字节,再利用输出流将读取到的字节写出到另一个文件中(不存在会自动创建)
package io2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
/**
-
Create by stefan
-
Date on 2018-05-27 23:19
-
Convertion over Configuration!
*/
public class copy {
public static void main(String args[]) throws IOException {
FileInputStream inputStream = new FileInputStream(“E:\huge1.jpg”);
FileOutputStream outputStream = new FileOutputStream(“E:\古月.jpg”);
byte[] bytes = new byte[1024];
int len = 0;
while ((len=inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0,len);
}
inputStream.close();
outputStream.close();
}
}
注:复制文本、图片、mp3、视频等的方式一样。
字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果。
java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流。
字节缓冲输出流
BufferedOutputStream
字节缓冲输入流
BufferedInputStream
BufferedOutputStream
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“a.txt”,true));
bos.write(“hello world”.getBytes());
bos.close();
BufferedInputStream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“a.txt”));
byte[] bytes = new byte[1024];
int len = 0;
while ((len=bis.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
bis.close();
注:
-
成员方法与字节流基本一样,字节缓冲流的作用就是提高输入输出的效率。
-
构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
-
为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
复制文件的升级:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“E:\modern-java.pdf”));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“F:\汤包\慕课大巴\modern-java.pdf”));
int len = 0;
byte[] bytes =new byte[1024];
while ((len=bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bis.close();
bos.close();
测试:四种复制文件的效率高低
package io2;
import java.io.*;
/**
-
测试复制的时间
-
Create by stefan
-
Date on 2018-05-28 10:28
-
Convertion over Configuration!
*/
public class copy2 {
//一个字节一个字节的复制,耗时22697毫秒
public static void fun() throws IOException {
FileInputStream fis = new FileInputStream(“F:\汤包\慕课大巴\modern-java.pdf”);
FileOutputStream fos = new FileOutputStream(“E:\modern-java.pdf”);
int by = 0;
while ((by=fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
//1024字节数组复制 耗时63毫秒
public static void fun1() throws IOException {
FileInputStream fis = new FileInputStream(“F:\汤包\慕课大巴\modern-java.pdf”);
FileOutputStream fos = new FileOutputStream(“E:\modern-java.pdf”);
int len = 0;
byte[] bytes =new byte[1024];
while ((len=fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
// 一个字节一个字节复制,但是用了缓冲流 耗时64毫秒
public static void fun2() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“E:\modern-java.pdf”));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“F:\汤包\慕课大巴\modern-java.pdf”));
int by = 0;
while ((by=bis.read()) != -1) {
bos.write(by);
}
最后
面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?
掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。
public static void fun2() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“E:\modern-java.pdf”));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“F:\汤包\慕课大巴\modern-java.pdf”));
int by = 0;
while ((by=bis.read()) != -1) {
bos.write(by);
}
最后
面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?
掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。
[外链图片转存中…(img-BRLc3Ikq-1716310224089)]
[外链图片转存中…(img-GSiHO8yt-1716310224090)]