12.4文件操作
StringBuilder与StringBuffer都是可变的字符串
在进行文件操作时,需要知道一些关于文件的信息,File类提供了一些方法可以用来操作文件和获得文件的信息。对于目录,Java把它当做一种特殊的文件,既文件名文件。
创建新文件对象的方法:
File f1=new File(“data.txt”);
File f2=new File(“\\mydir”,”data.txt”);----\\mydir\\data.txt
File dir=new File(“\\etc”);
File f4=new File(dir,”data.txt”);
删除文件对象:
F4.delete();
使用相对路径用getParent方法返回为空
访问文件对象:
public String getName();//返回文件对象名,不包含路径名
public String getPath();//返回相对路径,包含文件名
public String getAbsolutePath();//返回绝对路径名,包含文件名
public String getParent();//返回文件对象的路径名
public File getPanterFile();//返回父文件对象
获得文件属性:
public long lastModified();//返回指定文件最后被修改的时间
public boolean exists();//测试指定文件是否存在
public long length();//返回指定文件的字节长度
目录操作:
public boolean mkdir();//创建指定目录,正常建立时返回true
public String[] list();//返回目录中的所有文件名字符串
public File[] listFiles();//返回指定目录中的所有文件对象
文件操作:
public booean renameTo(Filedest);//文件重命名
public boolean delete();//删除目录
利用File自动更新文件:
public static void update(String fname,String destdir) throws IOException{
File f1,f2,dest;
//在当前目录中创建文件对象f1.dest
f1=new File(fname);
dest=new File(destdir);
if(f1.exists()){
//dest不存在时创建目录
if(!dest.exists())
dest.mkdir();
//在目录dest中创建文件f2
f2=new File(dest.fname);
long d1=f1.lastModified();
long d2=f2.lastModified();
//f2不存在时或者存在但日期较早时
if((!f2.exists())||(f2.exists())&&(d1>d2)){
copy(f1,f2);//复制
}
showFileInfo(f1);
showFileInfo(dest);
}else{
System.out.println(f1.getName()+"file not found");
}
}
public static void copy(File f1,File f2) throws IOException{
//创建文件输入流对象
FileInputStream fis=new FileInputStream(f1);
//创建文件输出流对象
FileOutputStream fos=new FileOutputStream(f2);
int count,n=512;
byte buffer[]=new byte[n];
//从输入流读取数据
count=fis.read(buffer,0,n);
while(count!=-1){
//写出输出流数据
fos.write(buffer,0,count);
count=fis.read(buffer,0,n);
}
System.out.println("复制文件"+f2.getName()+"成功!");
//关闭输入/输出流
fis.close();
fos.close();
}
public static void showFileInfo(File f1)throws IOException{
SimpleDateFormat sdf;
sdf=new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
if(f1.isFile()){
String filepath=f1.getAbsolutePath();
Date da=new Date(f1.lastModified());
String mat=sdf.format(da);
System.out.println();
}else{
System.out.println("<目录:>\t"+f1.getAbsolutePath());
File[] files=f1.listFiles();
for(int i=0;i<files.length;i++){
showFileInfo(files[i]);
}
}
文件过滤器
Java提供两个接口-----FileFilter和FilenameFilter,用来实现对文件名字符串的过滤。FileFilterj接口用于抽象路径名的过滤器;FilternameFilter接口的实现可用于过滤器文件名。这连个接口都有accept方法,只不过参数不同。
public boolean accept(File pathname) {//验证要显示的文件是否符合过滤条件。如果符合返回为真,作为返回值放在返回数组里面
// TODO Auto-generated method stub
//如果是目录要返回假,如果没有找到.也返回假,如果也找到点了,从点后开始截,截下来的扩展名与要查找的扩展名不同,也返回假
if(pathname.isDirectory()){//是目录
return false;
}
//不是目录
String filename=pathname.getName();
int n=filename.indexOf(".");//找文件名字的.的位置
if(n==-1||n==filename.length()-1){//没有.或者在最后
return false;
}
if(filename.substring(n+1).equals(exterName)){
return true;
}
return false;
}
随机文件的操作
文件随机读写流的读取指针控制
long getFilePointer() ---得到当前的文件读取指针。
void seek(long pos) ---把指针从开始移动到pos位置。
long length() ---得到文件的长度(有多少个字节) 。
void setLength(long newLength)
读写数据的常用方法
读、写基本数据类型: readInt()、writeInt(int n)等;
读、写UTF字符串: readUTF()、writeUTF(String str);
读取文件中的一行: readLine();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f=new File("res/example.txt");
RandomAccessFile raf=new RandomAccessFile(f,"rw");//f是文件名,rw是模型
//写入
raf.write(1234);
raf.writeDouble(1.235);
raf.seek(0);//指针定位到开头
int n=raf.readInt();
double d=raf.readDouble();
System.out.println(n);
System.out.println(d);
System.out.println(raf.getFilePointer());
raf.seek(4);
double d1=raf.readDouble();
System.out.print(d1);
raf.close();
}