Java.io流的基本常用类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30160727/article/details/78142169
Java.io包下面的类主要是对文件的操作,也是我们经常会用到的类,尤其是实现文件上传和下载的功能。本文主要介绍该包下常用的类,包括File,inputStream,OutputStream,Reader和Writer。io流的操作依赖于File类,所以本文首先会介绍File的相关知识。

1.File

File对象代表一个文件实体,其有以下三个常用的构造函数:
String parentPath = "C:\\Users\\Administrator\\Desktop";
String demoPath = "C:\\Users\\Administrator\\Desktop\\demo.txt";
//一个形参,以文件路径字符串创建File对象
File file1 = new File(demoPath);
//两个形参,第一个代表父目录字符串,第二个代表子目录或者文件字符串
File file2 = new File(parentPath,"/dir1");
//两个形参,第一个代表父文件对象,第二个是子目录或者文件字符串
File file3 = new File(file2,"/dir2");
如果以上文件不存在,我们可以调用File类的mkdir()和mkdirs()方法创建文件夹,调用createNewFile()方法创建文件:
String parentPath = "C:\\Users\\Administrator\\Desktop";
String demoPath = "C:\\Users\\Administrator\\Desktop\\demo.txt";
//一个形参,以文件路径字符串创建File对象
File file1 = new File(demoPath);
if (!file1.exists()){
    //创建一个空文件
    file1.createNewFile();
}
//两个形参,第一个代表父目录字符串,第二个代表子目录或者文件字符串
File file2 = new File(parentPath,"/dir1");
if (!file2.exists()){//判断该文件是否存在
    //不存在就创建
    file2.mkdirs();
}
//两个形参,第一个代表父文件对象,第二个是子目录或者文件字符串
File file3 = new File(file2,"/dir2");
if (!file3.exists()){
    //还不存在 ,再次创建
    file3.mkdir();
}
说明:exists()方法用来判断文件是否存在,如果不存在,再去创建。
          createNewFile()创建一个空文件,不能创建文件夹。
          mkdirs()和mkdir()都是创建文件夹,区别在于前者可以创建多层不存在的文件夹,后者只能创建单层,下面的例子解释
File file2 = new File(parentPath,"/dir1");
if (!file2.exists()){
    //这里可以用mkdir()和mkdirs()
    file2.mkdir();
}
File file3 = new File(file2,"/dir2/dir3");
if (!file3.exists()){
    //这里只能用mkdirs()
    file3.mkdirs();
}
File类还有一下常用的方法:
//打印文件的绝对路径
System.out.println(file1.getAbsolutePath());
//文件名
System.out.println(file1.getName());
//得到父目录
System.out.println(file1.getParent());

2.io流

io流按照单位分成字节流和字符流,本文按照这个类别分别介绍这种流。

说明 字节流 字符流
抽象基类 InputStream,OutputStream Reader,Writer
节点流 FileInputStream,FileOutputStream FileReader,FileWriter
缓冲流 BufferedInputStream,BufferedOutputStream BuffredReader,BufferedWriter
数据流 DataInputStream,DataOutputStream  
对象流 ObjectInputStream,ObjectOutputStream  
转换流   InputStreamReader,OutputStreamReader

随机访问流 RandomAccessFile  

2.1 字节流
 字节流主要是以字节的方式读取或者写入文件。
 2.1.1 FileInputStream和FileOutputStream
           FileInputStream是从文件中读内容,每次读都放在缓冲数组中,然后返回每次读字节的长度。如果到文件尾,则返回-1.
String parentPath = "C:\\Users\\Administrator\\Desktop";
String demoPath = "C:\\Users\\Administrator\\Desktop\\demo.txt";
//一个形参,以文件路径字符串创建File对象
File file1 = new File(demoPath);
FileInputStream fis = new FileInputStream(file1);
//缓冲数组,存在每次读取的字节,一次读取1024个
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) != -1){
    System.out.println(new String(buffer,0,length));
}
fis.close();
说明:首先定义缓冲数组buffer,用来存在每次从文件中读取的内容。length是每次读取得字节数组长度,没有则是-1.调用read()方法开始从文件中读,循环一直到文件尾。FileInputStream还有三个非常有用的方法:
mak(),reset(),skip(),方便我们随机读取文件内容
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buffer = new byte[4];
int length = 0;
//1.标记下标为0的位置
bis.mark(0);
//2.读,下标从0开始
length = bis.read(buffer);
System.out.println(new String(buffer,0,length) );
//3.再次读
length = bis.read(buffer);
System.out.println(new String(buffer,0,length) );
//4.重置,下标回到上次标记的位置
bis.reset();
//5.读
length = bis.read(buffer);
System.out.println(new String(buffer,0,length) );
//6.跳过4个字节
bis.skip(4);
//7.读
length = bis.read(buffer);
System.out.println(new String(buffer,0,length) );
bis.close();
fis.close();

上面代码的步骤,可有下面的示意图展现:

说明:1.首先文件中的内容为Icanuseio!,并且调用mark()方法标记当前位置。其中,mark是标记的下标位置,pos是读取的起始位置。
2.经过第二步读的时候,mark的位置不变,pos向后移动4个位置。
3.再次读的时候,从上次pos的位置继续读取。
4.调用reset()方法将下标重置到上次mark的位置,即0,这时pos回到0位置
5.再次读就是从下标为0的位置读
6.调用skip(4)方法,将pos的值跳过4个字节,如上图红色区域。
7.再次读,从pos的位置读
FileOutputStream是向文件中写内容,我们需要把内容放在缓冲数组中.记住每次写入内容,都是把之前的内容清除后再写入。
FileOutputStream fos = new FileOutputStream(file1);
//需要写入文件的内容
byte[] content = "写入文件的内容".getBytes();
//写
fos.write(content);
fos.close();
2.1.2 BufferedInputStream和BufferedOutputStream
BufferedInputStream同样包含FileInputStream类相同的方法。BufferedOutputStream和FileOutputStream的用法相同,区别在于前者会在文件尾追加写入,而后者是清除文件内容再写入。
2.1.3 DataInputStream,DataOutputStream
     DataInputStream 主要是把8种基本数据类型和字符串写入文件,再利用DataOutputStream按照写入的顺序读出来。
//首先向文件中写入内容
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
//写boolean值
dos.writeBoolean(true);
//写byte
dos.writeByte(12);
//写字符串
dos.writeBytes("sd444");
//写字符
dos.writeChar(98);
//写浮点型数据
dos.writeDouble(1.3);
//写整型
dos.writeInt(45);
//写字符串
dos.writeUTF("哈哈");
dos.close();
fos.close();
//读出写入的内容,顺序一致
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);

boolean readBoolean = dis.readBoolean();
System.out.println(readBoolean);
byte readByte = dis.readByte();
System.out.println(readByte);
//这里bytes数组的长度应和写入的字符串的长度一致
byte[] bytes = new byte[5];
dis.read(bytes);
System.out.println(new String(bytes));
char readChar = dis.readChar();
System.out.println(readChar);
double readDouble = dis.readDouble();
System.out.println(readDouble);
int readInt = dis.readInt();
System.out.println(readInt);
String readUTF2 = dis.readUTF();
System.out.println(readUTF2);
dis.close();
fos.close();
打印输出:
true
12
sd444
b
1.3
45
哈哈


注意:1.读的顺序必须和写的顺序一致
2.通过writeBytes()方法写入字符串,读的时候缓冲数组的大小应该和字符串的长度一样。标红所示

2.1.4 ObjectInputStream,ObjectOutputStream
DataInputStream,DataOutputStream是操作基本数据类型和字符串,但是其不能读写其他对象。ObjectInputStream,ObjectOutputStream在数据流的基础上增加了对对象实例的读写操作.注意写入的对象必须可序列化
//首先向文件中写入内容
FileOutputStream fos = new FileOutputStream(file);
//创建一个user对象
User user = new User();
user.setAge(12);
user.setBirth(new Date());
user.setName("小明");
//将对象写入文件中
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(user);
oos.close();

//读取对象
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
User user1 = (User) ois.readObject();
System.out.println(user1);
ois.close();
fis.close();


2.2 字符流
2.2.1 FileReader,FileWriter
 字符流与字节流的区别在于,字符流每次读或写是以char为单位,而不是byte,说明速度更快。
//首先向文件中写入内容
FileWriter fw = new FileWriter(file);
fw.write("hello");
fw.close();

//读取对象
FileReader fr = new FileReader(file);
char[] buffer = new char[1024];
int length =0 ;
while ((length = fr.read(buffer)) != -1){
    System.out.println(new String(buffer,0,length));
}


2.2.2 BuffredReader,BufferedWriter
     这两个缓冲流的速度回更快,每次可以读取一行,但是只能对文本操作。BufferedWriter在文件尾追加内容。
//首先向文件中写入内容
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("祝大家国庆快乐!");
bw.write("祝大家国庆快乐!");
bw.close();
fw.close();

//读取
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String buffer = null;
while ((buffer = br.readLine()) != null){
    System.out.println(buffer);
}

2.2.3 InputStreamReader,OutputStreamReader

转换流主要是将字节流转成字符流,以提高操作文件的速度。而且在项目开发过程中,我们一般只能获得对象的字节流。
FileInputStream fis = new FileInputStream(file);
//字节流向字符流转换
InputStreamReader isr = new InputStreamReader(fis);
//通过转换流创建字符流对象
BufferedReader br = new BufferedReader(isr);

3.RandomAccessFile

上述的字节流和字符流都需要两个对象来操作一个文件。而随机访问流只需一个对象即可,并且可以在任何一个位置进行读写操作。该类主要用在大文件上传的场景中,该场景中大文件被分成几份上传,到后台我们需要把分散的文件组合在一起。
RandomAccessFile raf = new RandomAccessFile(file,"rw");
//不会清楚文件原来的内容,从开始覆盖
String content = "abcdefghijk";
raf.writeUTF(content);
//设置下标
raf.seek(content.length());
//从当前下标写
raf.writeUTF("123");
raf.close();
RandomAccessFile raf2 = new RandomAccessFile(file,"rw");

String readLine = raf2.readLine();
System.out.println(readLine);
raf2.close();
总结:点滴成河,记录每次的学习经历。本文主要介绍了java.io包下对文件操作的相关知识,比较常用的是FileInputStream和BufferedReader.

猜你喜欢

转载自blog.csdn.net/sinat_30160727/article/details/78142169