File类
File类声明如下:
public class File ectends Object implements Serializable,Comparable
构造方法:
public File(String pathname)
public File(File patent,String chile)
public File(String patent,String child)
文件名的处理
String getName( ); //得到一个文件的名称(不包括路径)
String getPath( ); //得到一个文件的路径名
String getAbsolutePath( );//得到一个文件的绝对路径名
String getParent( ); //得到一个文件的上一级目录名
String renameTo(File newName); //将当前文件名更名为给定文件的完整路径
文件属性测试
boolean exists( ); //测试当前File对象所指示的文件是否存在
boolean canWrite( );//测试当前文件是否可写
boolean canRead( );//测试当前文件是否可读
boolean isFile( ); //测试当前文件是否是文件(不是目录)
boolean isDirectory( ); //测试当前文件是否是目录
普通文件信息和工具
long lastModified( );//得到文件最近一次修改的时间
long length( ); //得到文件的长度,以字节为单位
boolean delete( ); //删除当前文件
目录操作
boolean mkdir( ); //根据当前对象生成一个由该对象指定的路径
String list( ); //列出当前目录下的文件
例 8.4 自动更新文件。
本例使用File类对象对指定文件进行自动更新的操作。程序如下:
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class UpdateFile
{
public static void main(String args[]) throws IOException
{
String fname = "Write1.txt"; //待复制的文件名
String childdir = "backup"; //子目录名
new UpdateFile().update(fname,childdir);
}
public void update(String fname,String childdir) throws IOException
{
File f1,f2,child;
f1 = new File(fname); //当前目录中创建文件对象f1
child = new File(childdir); //当前目录中创建文件对象child
if (f1.exists())
{
if (!child.exists()) //child不存在时创建子目录
child.mkdir();
f2 = new File(child,fname); //在子目录child中创建文件f2
if (!f2.exists() || //f2不存在时或存在但日期较早时
f2.exists()&&(f1.lastModified() > f2.lastModified()))
copy(f1,f2); //复制
getinfo(f1);
getinfo(child);
}
else
System.out.println(f1.getName()+" file not found!");
}
public void copy(File f1,File f2) throws IOException
{ //创建文件输入流对象
FileInputStream rf = new FileInputStream(f1);
FileOutputStream wf = new FileOutputStream(f2);
//创建文件输出流对象
int count,n=512;
byte buffer[] = new byte[n];
count = rf.read(buffer,0,n); //读取输入流
while (count != -1)
{
wf.write(buffer,0,count); //写入输出流
count = rf.read(buffer,0,n);
}
System.out.println("CopyFile "+f2.getName()+" !");
rf.close(); //关闭输入流
wf.close(); //关闭输出流
}
public static void getinfo(File f1) throws IOException
{
SimpleDateFormat sdf;
sdf= new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
if (f1.isFile())
System.out.println("\t"+f1.getAbsolutePath()+"\t"+
f1.length()+"\t"+sdf.format(new Date(f1.lastModified())));
else
{
System.out.println("
\t"+f1.getAbsolutePath());
File[] files = f1.listFiles();
for (int i=0;i
getinfo(files[i]);
}
}
}
f1.lastModified()返回一个表示日期的长整型,值为从1970年1月1日零时开始计算的毫秒数,并以此长整型构造一个日期对象,再按指定格式输出日期。程序运行结果如下: D:\myjava\Write1.txt 6 2002年12月11日02时18分
D:\myjava\backup D:\myjava\backup\Write1.txt 6 2002年12月31日05时13分
文件过滤器
类FilterInputStream和FilterOutputStream分别对其他输入/输出流进行特殊处理,它们在读/写数据的同时可以对数据进行特殊处理。另外还提供了同步机制,使得某一时刻只有一个线程可以访问一个输入/输出流
类FilterInputStream和FilterOutputStream分别重写了父类InputStream和OutputStream的所有方法,同时,它们的子类也应该重写它们的方法以满足特定的需要
• 要使用过滤流,首先必须把它连接到某个输入/输出流上,通常在构造方法的参数中指定所要连接的流:
– FilterInputStream(InputStream in);
– FilterOutputStream(OutputStream out);
这两个类是抽象类,构造方法也是保护方法
类BufferedInputStream和BufferedOutputStream实现了带缓冲的过滤流,它提供了缓冲机制,把任意的I/O流“捆绑”到缓冲流上,可以提高读写效率
• 在初始化时,除了要指定所连接的I/O流之外,还可以指定缓冲区的大小。缺省大小的缓冲区适合于通常的情形;最优的缓冲区大小常依赖于主机操作系统、可使用的内存空间以及机器的配置等;一般缓冲区的大小为内存页或磁盘块等地整数倍,如8912字节或更小。
– BufferedInputStream(InputStream in[, int size])
– BufferedOutputStream(OutputStream out[, int size])
例 8.5 列出当前目录中带过滤器的文件名清单。
本例实现FilenameFilter接口中的accept方法,在当前目录中列出带过滤器的文件名。
程序如下:
import java.io.*;
public class DirFilter implements FilenameFilter
{
private String prefix="",suffix=""; //文件名的前缀、后缀
public DirFilter(String filterstr)
{
filterstr = filterstr.toLowerCase();
int i = filterstr.indexOf('*');
int j = filterstr.indexOf('.');
if (i>0)
prefix = filterstr.substring(0,i);
if (j>0)
suffix = filterstr.substring(j+1);
public static void main(String args[])
{ //创建带通配符的文件名过滤器对象
FilenameFilter filter = new DirFilter("w*abc.txt");
File f1 = new File("");
File curdir = new File(f1.getAbsolutePath(),""); //当前目录
System.out.println(curdir.getAbsolutePath());
String[] str = curdir.list(filter); //列出带过滤器的文件名清单
for (int i=0;i
System.out.println("\t"+str[i]);
}
public boolean accept(File dir, String filename)
{
boolean yes = true;
try
{
filename = filename.toLowerCase();
yes = (filename.startsWith(prefix)) &
(filename.endsWith(suffix));
}
catch(NullPointerException e)
{
}
return yes;
}
}
程序运行时,列出当前目录中符合过滤条件“w*.txt“的文件名清单。结果如下:
D:\myjava
Write1.txt
Write2.txt
文件对话框
随机文件操作
于InputStream 和OutputStream 来说,它们的实例都是顺序访问流,也就是说,只能对文件进行顺序地读/写。随机访问文件则允许对文件内容进行随机读/写。在java中,类RandomAccessFile 提供了随机访问文件的方法。类RandomAccessFile的声明为:
public class RandomAccessFile extends Object implements DataInput, DataOutput
File:以文件路径名的形式代表一个文件
FileDescriptor:代表一个打开文件的文件描述
FileFilter & FilenameFilter:用于列出满足条件的文件
File.list(FilenameFilter fnf)
File.listFiles(FileFilter ff)
FileDialog.setFilenameFilter(FilenameFilter fnf)
• FileInputStream & FileReader:顺序读文件
• FileOutputStream & FileWriter:顺序写文件
• RandomAccessFile:提供对文件的随机访问支持
类RandomAccessFile则允许对文件内容同时完成读和写操作,它直接继承Object,并且同时实现了接口DataInput和DataOutput,提供了支持随机文件操作的方法
DataInput和DataOutput中的方法
• readInt(), writeDouble()…
int skipBytes(int n):将指针乡下移动若干字节
length():返回文件长度
long getFilePointer():返回指针当前位置
void seek(long pos):将指针调到所需位置
void setLength(long newLength):设定文件长度
构造方法:
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
mode 的取值
– “r” 只读. 任何写操作都将抛出IOException。
– “rw” 读写. 文件不存在时会创建该文件,文件存在时,原文件内容不变,通过写操作改变文件内容。
– “rws” 同步读写. 等同于读写,但是任何协操作的内容都被直接写入物理文件,包括文件内容和文件属性。
– “rwd” 数据同步读写. 等同于读写,但任何内容写操武汉白癜风医院作都直接写到物理文件,对文件属性内容的修改不是这样。
例 8.6 随机文件操作。
本例对一个二进制整数文件实现访问操作当以可读写方式“rw“打开一个文件”prinmes.bin“时,如果文件不存在,将创建一个新文件。先将2作为最小素数写入文件,再依次测试100以内的奇数,将每次产生一个素数写入文件尾。
程序如下:
import java.io.*;
public class PrimesFile
{
RandomAccessFile raf;
public static void main(String args[]) throws IOException
{
(new PrimesFile()). createprime(100);
}
public void createprime(int max) throws IOException
{
raf=new RandomAccessFile("primes.bin","rw");//创建文件对象
raf.seek(0); //文件指针为0
raf.writeInt(2); //写入整型
int k=3;
while (k<=max)
{
if (isPrime(k))
raf.writeInt(k);
k = k+2;
}
output(max);
raf.close(); //关闭文件
}
public boolean isPrime(int k) throws IOException
{
int i=0,j;
boolean yes = true;
try
{
raf.seek(0);
int count = (int)(raf.length()/4); //返回文件字节长度
while ((i<=count) && yes)
{
if (k % raf.readInt()==0) //读取整型
yes = false;
else
i++;
raf.seek(i*4); //移动文件指针
}
} catch(EOFException e) { } //捕获到达文件尾异常
return yes;
}
public void output(int max) throws IOException
{
try
{
raf.seek(0);
System.out.println("[2.."+max+"]中有 "+
(raf.length()/4)+" 个素数:");
for (int i=0;i<(int)(raf.length()/4);i++)
{
raf.seek(i*4);
System.out.print(raf.readInt()+" ");
if ((i+1)%10==0) System.out.println();
}
} catch(EOFException e) { }
System.out.println();
}
}
程序运行时创建文件“primes.bin“,并将素数写入其中,结果如下:
[2..100]中有 25 个素数:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97