栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习之File类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习之File类

Java学习之File类

一、定义

File类不仅指系统中的文件,也指目录,因为目录也是特殊的文件,是文件和目录路径名的抽象表示。

二、File类常用方法

类型名称
构造方法public File(String pathname);
public File(File parent, String child);
public File(String parent, String child);
文件名的处理String getName();
String getPath();
StringgetAbsolutePath();
String getParent();
String renameTo(File newName);
文件属性测试boolean exists();
boolean canWrite();
boolean canRead();
boolean isFile();
boolean isDirectory();
文件信息和文件删除long lastModified();
long length();
boolean delete();
目录操作boolean mkdir();
String list();

所有方法参考: File类

三、作用举例

1.文件备份

例如:将Write1.txt文件拷贝到当前目录的backup文件夹下

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!");
    }

2.文件复制

public void copy(File f1,File f2) throws IOException { 
                                         //创建文件输入流对象
        FileInputStream  rf = new FileInputStream(f1); //Write1.txt
                                         //创建文件输出流对象
        FileOutputStream  wf = new FileOutputStream(f2); //backup\Write1.txt
        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();                        //关闭输出流
    }

3.获取文件信息

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(); //列出当前目录下的所有File对象
            for (int i=0;i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571374.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号