File 文本类

※ java.io.File类

File类提供管理文件或目录的方法。File实例表示真实文件系统中的一个文件或者目录。

  1. 构造方法
    . File(String pathname): 参数pathname表示文件路径或者目录路径;
    . File(String parent, String child): 参数parent表示根路径,参数child表示子路径;
    . File(File parent, String child): 参数parent表示根路径,参数child表示子路径;

    只处理一个文件,使用第一个构造方法;如处理一个公共目录的若干子目录或文件,那么使用第二个或者第三个更方便。

  2. 普通方法
    . boolean createNewFile():创建一个新的文件,如果文件已经存在,则创建失败(返回false),否则创建成功(返回true)。
    . boolean delete():删除文件或者空目录;
    . boolean mkdir()/mkdirs():创建一个或者多个目录(连续创建)->如果该目录的父目录不存在话,那么还会创建所有的父目录;
    . boolean renameTo(File destination):文件的改名;
    . boolean canRead()/canWrite():判断指定的文件是否能读取或者写入数据;
    . boolean exists():判断指定的文件或者目录是否存在;
    . String[] list():返回指定目录下所有文件名或者子目录名所组成的字符串数组;
    . long lastModified():返回指定文件最后一次被修改的时间(从1970年1月1日凌晨12点到这个文件的修改时间之间所经历的毫秒数);
    . String getPath()/getAbsolutePath():返回指定文件或者目录的路径和绝对路径
    . String getCanonicalPath(): 获取该File对象所代表的文件或者目录的正规路径;
    . String getParent()/getName():返回指定文件或者目录的父目录(没有返回null)和名字。

               File f = new File(".\\test.txt"));
               System.out.println(f.getCanonicalPath());        //c:\mypath\test.txt
               System.out.println(f.getAbsolutePath());         //c:\mypath\ .\test.txt
               System.out.println(f.getPath());                 //.\test.txt
               if(!f.exists()) f.createNewFile()); 

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

package com.briup.IO;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class FileTest {
    public static void main(String[] args) {
        //File file=new File("src/com/briup/IO/test.txt");
        //File file=new File("src/com/briup/IO","test1.txt");
        File file=new File("src/com/briup");
        try {
//          String[] files=file.list();
//          for(String f:files){
//              System.out.println(f);
//          }
            String [] files=file.list(new FilenameFilter() {
                //true表示该文件保留,false表示该文件忽略
                //java.lang.String
                @Override
                public boolean accept(File dir, String name) {
                    // TODO Auto-generated method stub
                    System.out.println(dir+"----"+name);
                    return name.endsWith(".txt");
                }
            });
            for(String s:files){
                System.out.println(s);
            }
            //System.out.println(file.isDirectory());
            //创建新的文件
//          file.createNewFile();
            //删除文件
            //file.delete();
            //获取绝对路径
//          System.out.println(file.getAbsolutePath());
//          System.out.println(file.getAbsoluteFile());
//          System.out.println(file.getCanonicalPath());
//          System.out.println(file.getCanonicalFile());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值