文件字节流
FileInputstream/FileOutputStream
FileInputStream通过字节的方式读取文件,适合读取所有类型的文件(图像、视频、文本文件等)。
Java也提供了FileReader专门读取文本文件。
FileOutputStream通过字节的方式写数据到文件中,适合所有类型的文件。
Java也提供了FileWriter专门写入文本文件。
使用文件字节流分别实现文件的读取与写入操作,将两种功能综合使用就可以实现文件的复制。
注意:
1、为了减少对硬盘的读写次数,提高效率,通常设置缓存数组。
相应地,读取时使用的方法为:read(byte[] b);写入时的方法为:write(byte[ ] b, int off, int length)。
2、程序中如果遇到多个流,每个流都要单独关闭,防止其中一个流出现异常后导致其他流无法关闭的情况。
1、文件字节输入流FileInputstream
import java.io.*;
/**
* 文件字节输入流InputStream
* 1、创建源
* 2、选择流
* 3、操作:分段读取
* 4、释放资源
*/
public class IOTest03 {
public static void main(String[] args) throws IOException {
//1、创建源
File src = new File("abc.txt");
//2、选择流
InputStream is = null;
try{
is = new FileInputStream(src);
//3、操作:分段读取
byte[] flush = new byte[1024*10];//缓冲容器//字节
//乱码->中英文混合,字节数不够就会乱码
//byte[] flush = new byte[2];//缓冲容器//字节
int len = -1;//接受长度
//read读的是数据,read(byte[] b)读的是数据大小
while((len=is.read(flush))!=-1){
//字节数组->字符串(解码)
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源
try{
if(null!=is){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、文件字节输出流FileOutputStream
import java.io.*;
/**
* 文件字节输出流OutputStream
* 1、创建源
* 2、选择流
* 3、操作:写入
* 4、释放资源
*/
public class IOTest04 {
public static void main(String[] args) {
//1、创建源
File dest = new File("dest.txt");
//2、选择流
OutputStream os = null;
try{
//os = new FileOutputStream(dest);
//FileOutputStream(File file,boolean append),追加文件
os = new FileOutputStream(dest,true);//改为true可以追加文件
//os = new FileOutputStream(dest,false);//改为false可以重新写文件
//3、操作:写入
/*一
String msg = "IO is so easy";
byte[] datas = msg.getBytes();//字符串->字节数组(编码)
os.write(datas,0,datas.length);//写出所有数据*/
//二
String msg = "change";
os.write(msg.getBytes());
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源
try{
if(null!= os){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
用到一个write方法:void write(byte[ ] b),该方法不再一个字节一个字节地写入,而是直接写入一个字节数组;
另外其还有一个重载的方法:void write(byte[ ] b, int off, int length),这个方法也是写入一个字节数组,但是可以指定从字节数组
的哪个位置开始写入,写入的长度是多少。
文件拷贝Copy
import java.io.*;
/**
* 文件拷贝:文件字节输入、输出流InputStream和OutputStream
* 1、创建源
* 2、选择流
* 3、操作:写出
* 4、释放资源
* 思考:利用递归制作文件夹的拷贝
*/
public class Copy {
public static void main(String[] args) {
copy("src/cn/test/io/Copy.java","copy.txt");
}
//把copy写成了方法
public static void copy(String srcPath,String destPath) {
//1、创建源
//File src = new File("p.png");//源头
//File dest = new File("pcopy.png");//目的地
File src = new File(srcPath);//源头
File dest = new File(destPath);//目的地
//2、选择流
InputStream is = null;
OutputStream os = null;
try{
is = new FileInputStream(src);
os = new FileOutputStream(dest);
//3、操作:分段读取
byte[] flush = new byte[1024];//缓冲容器//字节
int len = -1;//接受长度
while((len=is.read(flush))!=-1){
//分段写出
os.write(flush,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源(分别关闭:先打开的后关闭)
try{
if(null!=os){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try{
if(null!=is){
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}