一、概念
文件在程序中是以流的形式来操作的。
流:是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两个存储位置之间的传输称为流。流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
二、分类
1、按照流向划分:输入流、输出流
以内存为参照:
输入流:从文件流向内存
输出流:从内存流向文件
2、按照传输单位划分:字节流、字符流
字节流:可以用于读写二进制文件及任何类型文件。
字符流:可以用于读写文本文件。
三、字节流VS字符流的区别
字符流的由来: 因为数据编码的不同,从而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。
区别:
1、读写单位不同:字节流以字节(1 byte,1byte=8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
2、处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
3、缓存。字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件。
总结:优先选用字节流。因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
四、具体的案例讲解
案例0
功能:1、创建文件夹、文件 2、遍历文件夹下面的所有文件
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建新文件夹对象ff
File f1 = new File("D:/FF");
//如果存在
if(f1.isDirectory()){
System.out.println("已经存在该文件夹!");
//将文件夹下面的所有文件 组成数组
File lists[] = f1.listFiles();
System.out.println("ff文件夹下面有"+lists.length+"个文件");
//遍历,取出所有的文件名称
for(int i=0;i<lists.length;i++){
System.out.println("文件名 :" +lists[i].getName());
}
}else {
//如果不存在该文件夹,则输出
System.out.println("该文件夹不存在!");
f1.mkdir();
}
//在该文件夹下面创建 新的文件
File f2 = new File("d:\\ff\\psp.txt");
//如果存在在文件
if(f2.exists()){
System.out.println("已经存在该文件");
}else{
try {
f2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
案例1
功能:从数据源中读取数据到内存(FileInputStream的使用)
/**
* 功能:从数据源中读取数据到内存
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("d:\\ff\\test.txt");
FileInputStream fis=null;
try {
fis = new FileInputStream(f1);
byte[] bytes= new byte[1024];
//得到实际读取的长度
int n=0;
//循环读取
while((n=fis.read(bytes))!=-1){
String s = new String(bytes,0,n);
System.out.print(s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//最后一定要关闭文件流
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
案例2
功能:演示FileOutputStream,从内存中读取数据到数据源
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("d:\\ff\\test.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f1);
String s="我是曾可达,我是曾可达!\r\n";
String s2="听到请回答!";
byte bytes[] = new byte[1024];
fos.write(s.getBytes());
fos.write(s2.getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
案例3
功能:演示FileInputStream ,FileOutputStream ;将图片从C盘拷贝到D盘
public class Test {
public static void main(String[] args) {
//定义输入流
FileInputStream fis =null;
//定义输出流
FileOutputStream fos=null;
try {
fis = new FileInputStream("c:\\boy.jpg");
fos = new FileOutputStream("d:\\boy.jpg");
//读取
byte [] bytes = new byte[1024];
int n=0;
while((n=fis.read(bytes))!=-1){
fos.write(bytes, 0, n);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}