IO流(二)File类

转载请注明出处:http://blog.youkuaiyun.com/github_39430101/article/details/77142235

File类

Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。

常用构造方法

通过将给定路径名字符串转换为抽象路径名来创建一个新的File实例

File(String pathname)
常用方法
返回类型方法用法
booleancanExecute()测试应用程序是否可以执行此抽象路径名表示的文件。
booleancreateNewFile()当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
static FilecreateTempFile(String prefix,String suffix,File directory)在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。
booleandelete()删除此抽象路径名表示的文件或目录。
booleandeleteOnExit()在虚拟机终止时,请求删除此抽象路径名表示的文件或目录
booleanexists()测试此抽象路径名表示的文件或目录是否存在。
FilegetAbsoluteFile()返回此抽象路径名的绝对路径名形式。
StringgetAbsolutePath()返回此抽象路径名的绝对路径名字符串。
StringgetName()返回由此抽象路径名表示的文件或目录的名称。
StringgetParent()返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null
StringgetPath()将此抽象路径名转换为一个路径名字符串。
booleanisDirectory()测试此抽象路径名表示的文件是否是一个目录。
booleanisFile()测试此抽象路径名表示的文件是否是一个文件。
longlength()返回此抽象路径名表示的文件的长度
String[]list()返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
longlength()返回此抽象路径名表示的文件的长度
File[]listFiles(FilenameFilter filter)返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

示例

创建文件

public class TestFile {
    public static void main(String[] args) throws IOException{
        File file = new File("D:/io/a.txt");
        if(!file.exists()){
           boolean flag = file.createNewFile();  //创建文件夹 file.mkdir()
           if(flag == true){
               System.out.println("文件创建成功");
           } else 
               System.out.println("文件创建失败");
        }
    }
}

删除文件

public class TestFile {
    public static void main(String[] args) throws IOException{
        File file = new File("D:/io/a.txt");
        if(file.exists()){
           boolean flag = file.delete();
           if(flag == true){
               System.out.println("文件删除成功");
           } else 
               System.out.println("文件删除失败");
        }
    }
}

输出一个目录中的内容

public class TestFile {
    public static void main(String[] args) throws IOException{
        File file = new File("D:/io/");
        File[] subs = file.listFiles();
        for(File a :subs){
            System.out.println(a);
        }
    }
}

输出一个目录下所有扩展名为txt的文件

public class TestFile {
    public static void main(String[] args) throws IOException{
       File dir = new File("D:/io/");
       File[] subs = dir.listFiles(new FileFilter(){ 
         public boolean accept(File file){
             return file.getName().endsWith(".txt");
         }  
       });
       for(File sub: subs){
           System.out.println(sub);
       }
    }
}

如何遍历目录中所有的文件

public class TestFile {
    public static void main(String[] args) throws IOException{
        File file = new File("D:/io/");
        fileList(file);
    }
    public static void fileList(File file){
       if(file.isFile()){
           System.out.println("文件名:"+file.getName());
       } else if(file.isDirectory()){
           System.out.println("文件夹目录是:"+file.getName());
           File[] f = file.listFiles();
           for(File s : f){
               fileList(s);
           }
       }
    }
}

RandomAccessFile
RandomAccessFile适用于由大小已知的记录组成的文件,所以我们可以使用seek()将记录从一处转移到另一处,然后读取或者修改记录。RandomAccessFile不是InputStream或者OutputStream继承层次结构中的一部分。从本质上来说,RandomAccessFile的工作方式类似于把DataInputStream和DataOutputStream组合起来使用,还添加了一些方法。其中方法getFilePointer()用于查找当前所处的文件位置。
使用RandomAccessFile类的write(byte[])方法向文件写数据

public class TestFile {
    public static void main(String[] args) throws IOException{
        RandomAccessFile raf = new RandomAccessFile("D:/io/raf.txt","rw");
        byte[] buf = "HelloWorld".getBytes();
        raf.write(buf);
        raf.close();
    }
}

使用RandomAccessFile类的read(byte[])方法从文件读取数据

public class TestFile {
    public static void main(String[] args) throws IOException{
        RandomAccessFile raf = new RandomAccessFile("D:/io/raf.txt","r");
        //创建长度为10的字节数组
        byte[] b = new byte[10];
        int len = raf.read(b);
        System.out.println("读取到了:"+len+"个字节");
        System.out.println(new String(b));
        raf.close();
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值