学习了IO流,
有的流是按照字节的方式读取数据,一次读取1个字节byte,等同于一次读取8个二进这种流是万能的,什么类型的文件都可以读取。
包括:文本文件,图片,声音文件,视频。这就是下面的流:
文件专属:
(一):java.io.FileInputstream(字节,是byte数组)
(二):java.io.Fileoutputstream(字节,是byte数组
注释很清晰,也防止时间长不练自己看不明白 相信看完会对你有帮助
以下
一:展示FileInputStream的读文件,以及读取到的byte数组转换成字符串输出在控制台上面。
/*
FileInputStream字节输入流 (硬盘到内存) 读
*/
public class test {
public static void main(String[] args) {
//给定一个FileInputStream类型的in 初始值为null。
//这是一个全局变量,在全局都可以访问,如果不给一个全局变量,在下面需要关闭流的时候,无法访问到
FileInputStream in=null;
try {
in=new FileInputStream("test");
//给定一个读取字节的总长度初始值为0
int readCount=0;
//给定一个初始容量为6的byte数组
byte[] bytes=new byte[6];
//通过while循环,读byte数组当中的字节,当数组当中没有字节的时候返回-1,结束循环
while ((readCount=in.read(bytes))!=-1){
//通过String方法,把byte数组转换成字符出,规定长度输出。
System.out.print(new String(bytes,0,readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
//finally语句块是在cty catch语句块当中一定会执行的,在这里面关上流
} catch (IOException e) {
e.printStackTrace();
} finally {
//假如in为空的话,就不用关闭流,不是空的话,需要关闭,
// 使用try catch 捕捉异常
if ( in!= null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(二):Fileoutputstream写入字符串,先将字符串转换成byte数组,在放入指定文件当中
public class test02 {
public static void main(String[] args) {
FileOutputStream out=null;
try {
//在此处判断是否append(追加),就是在文件的末尾写入,还是要在文件的开头写,
// 如果是开头写的话,之前文档里的内容就被删掉
out=new FileOutputStream("HelloWorld",true);
//给定一个字符串
String s="小奶糕";
//把字符串转换成byte数组
byte[] bytes=s.getBytes();
//在指定文件中写入byte数组
out.write(bytes);
//写入的话,千万不要忘了刷新流
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(三):FileInputstream和Fileoutputstream联合使用,进行对文件的copy
public class text03 {
public static void main(String[] args) {
FileInputStream in=null;
FileOutputStream out=null;
try {
//读取的文件夹
in=new FileInputStream("D:\\壁纸\\kk.jpg");
//copy到的文件夹
out=new FileOutputStream("HelloWorld");
//给定一个读取到的字节数,初始值为0
int readCount=0;
//给定一个读取一兆大小的byte数组。
byte[] bytes=new byte[1024*1024];
//读取数组,当读取不到字节的时候返回-1,跳出循环
while ((readCount=in.read(bytes))!=-1){
//读到多少字节,在指定的文件下写入多少。
out.write(bytes,0,readCount);
}
//刷新流
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//为什么关闭流的两个try catch语句不能放到一起?
//假如上面的那个流出现了异常,那么下面的流就不会关闭了,为了保证都关闭
if (in != null) {
try {
//关闭流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
//关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
最后展示一下FileInputStream里其他常用方法int available()
返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。
(返回还有多少字节没有读取)
但是,这个方法有一个
优点:不用循环,读一次就可以了。
缺点是:不适合较大的文件,因为byte[]数组不能太大
public class texts04 {
public static void main(String[] args) {
FileInputStream in=null;
try {
in=new FileInputStream("test");
//此时还没有读取数据,所以调用这个方法返回的是总字节数
int read=in.available();
System.out.println("读取到的总字节数为:"+read);//20
//既然知道了总共的字节数,就知道了要创建一个byte数组的大小了
byte[] bytes=new byte[20];
//开始读取文件当中的内容,返回读取到的字节数int。
int readCount= in.read(bytes);
//将读取到byte数组里的字节通过调用String的方法,抓换成字符串,输出
System.out.println(new String(bytes,0,readCount));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
//关闭流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
效果如下: