Java I/O文件读写基本类相关知识和概念,对于程序设计者来说,创建一个好的输入/输出系统是一项艰难的任务,其中挑战来源于所有的可能性,不仅存在各种源端与接收端(文件,控制台,网络链接等),而且还需要以各种不同的方式与它们通信(顺序,随机存取,缓冲,二进制,按字符,按行,按字等)。
Java I/O主要包括如下几个层次:
1. File(文件特征与管理):用于文件或者目录的描述信息,例如生成新目录,修改文件名,删除文件,判断文件所在路径等。
2. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的父类。定义了所有输入流都具有的共同特征。
3. OutputStream(二进制格式操作):抽象类。基于字节的输出操作。是所有输出流的父类。定义了所有输出流都具有的共同特征。
Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。
4. Reader(文件格式操作):抽象类,基于字符的输入操作。
5. Writer(文件格式操作):抽象类,基于字符的输出操作。
6. RandomAccessFile(随机文件操作):它的功能丰富,可以从文件的任意位置进行存取(输入输出)操作。
1. File
它是独立于系统平台的,利用其构造函数创建出相应的File 对象;再调用其中的方法实现对文件的各个属性方面的操作。
构造函数:
File( String path)
File(String path, String FileName)
File(File dir, String name)
用途:File类提供了一种与机器无关的方式来描述一个文件对象的属性,通过类File所提供的方法,可以得到文件或目录的描述信息,这主要包括名称、所在路经、可读性、可写性、文件的长度等,还可以生成新的目录、改变文件名、删除文件、列出一个目录中所有的文件等。
public static void main(String[] args) throws IOException {
File f = new File("dir");
f.createNewFile();// 创建一个.txt这个文件
f.mkdir();// 创建一个名为.txt的目录
/*
* 使用绝对路径
*
* File f=new File("D:\\dir\\src\\A.java");
*
* f.createNewFile();
*/
/*
* 跨平台使用
*
* 根据不同操作系统获得对应的分隔符 File fDir=new File(File.separator);
*
* String strFile="dir"+File.separator+"src"+File.separator +"A.java";
*
* File f=new File(fDir,strFile);
*
* f.createNewFile();
*
* f.delete();//删除文件或目录
*
* //f.deleteOnExit();
*/
/*
* 在缺省的临时文件目录下创建临时文件
*
* for(int i=0;i<5;i++)
*
* {
*
* File f=File.createTempFile("winTemp",".tmp");
*
* f.deleteOnExit();//退出时删除
*
*
*
* }
*/
/*
* 列出指定目录下所有子目录及文件的名称
*/
File fDir = new File(File.separator);
String strFile = "dir" + File.separator + "src";
File f = new File(fDir, strFile);
String[] names = f.list();
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
// 有过滤器的情况FilenameFilter是个接口
File dir = new File(File.separator);
String filepath = "dir" + File.separator + "src";
/**
* dir
* 上级抽象路径,如果dir为null,那么程序将自动调用单个参数的File构造方法,同时将filepath路径应用到File但构造参数
* 如果dir为//,则此路径为本文件所在磁盘根目录
*/
File f = new File(dir, filepath);
if (f.exists()) {
} else {
f.mkdirs();
}
String[] names = f.list(new FilenameFilter() { // 实现了FilenameFilter接口的匿名类,实现accept方法过滤文件
@Override
public boolean accept(File dir, String name) {
System.out.println(name.indexOf(".java"));
return name.indexOf(".java") != -1;
}
});
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
2. InputStream/OutputStream(抽象基类)
(1) 它们主要提供文件内容操作的基本功能函数read()、 write()、close()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。
(2)文件操作的一般方法:
根据所要操作的类型生成对应输入输出文件类的对象;
调用此类的成员函数实现文件数据的读写;
关闭此文件流对象。
(3)文件操作的应用要点:
异常的捕获:由于包java.io中几乎所有的类都声明有I/O异常,因此程序应该对这些异常加以处理。
流结束的判断:方法read()的返回值为-1时;readLine()的返回值为null时。
上边两个抽象基类实现类有FileInputStream/FileOutputStream(本地文件读写类):它们用于本地文件的二进制格式顺序读写。
java.io.FileInputStream是InputStream的子类。从开头File名称上就可以知道,FileInputStream与从指定的文件中读取数据至目的地有关。而java.io.FileOutputStream是OutputStream的子类,顾名思义,FileOutputStream主要与从来源地写入数据至指定的文件中有关。
当建立一个FileInputStream或FileOutputStream的实例时,必须指定文件位置及文件名称,实例被建立时文件的流就会开启;而不使用流时,必须关闭文件流,以释放与流相依的系统资源,完成文件读/写的动作。
FileInputStream可以使用read()方法一次读入一个字节,并以int类型返回,或者是使用read()方法时读入至一个byte数组,byte数组的元素有多少个,就读入多少个字节。在将整个文件读取完成或写入完毕的过程中,这么一个byte数组通常被当作缓冲区,因为这么一个byte数组通常扮演承接数据的中间角色。
PipedInputStream/PipedOutputStream(管道输入输出)
(1)它用于实现管道的二进制格式的输入输出(将一个线程的输出结果直接连接到另一个线程的输入端口,实现两者数据直接传送)。
(2) PipedReader/PipedWriter类:它用于实现管道的字符格式的输入输出。
(3)要求:操作时需要将两个端口相互连结。
(4)实现原理:
(5) 管道的连接:
方法一:是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象
方法二:是利用双方类中的任一个成员函数 connect()相连接
(6)实例讲解: Sender.java, Receiver.java, PipedIO.java (使用PipedWriter,PipedReader)
Sender.java
Receiver.java
PipedIO.java
(7)实例讲解: Sender1.java, Receiver1.java, PipedIO1.java (使用
PipedInputStream,PipedOutputStream)
Sender1.java
Receiver1.java
PipedIO1.java
RandomAccessFile(随机文件读写类):
(1)RandomAccessFile类:它直接继承于Object类而非InputStream/OutputStream类,从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。
(2)由于RandomAccessFile类实现了DataOutput与DataInput接口,因而利用它可以读写Java中的不同类型的基本类型数据(比如采用readLong()方法读取长整数,而利用readInt()方法可以读出整数值等)。
RandomFileRW.java
InputStream.java
OutputStream.java
FilterInputStream.java
FilterOutputStream.java