`

File类中几个经常用到的方法

 
阅读更多

一、File类的一些常用方法:

 

1.createNewFile

 

public boolean createNewFile()

                      throws IOException

Atomically creates a new, empty file named by this abstract pathname if and only if a file with 

this name does not yet exist. The check for the existence of the file and the creation of the 

file if it does not exist are a single operation that is atomic with respect to all other 

filesystem activities that might affect the file.

Note: this method should not be used for file-locking, as the resulting protocol cannot be made 

to work reliably. The FileLock facility should be used instead.

 

Returns:

true if the named file does not exist and was successfully created; false if the named file 

 

already exists

Throws:

IOException - If an I/O error occurred

SecurityException - If a security manager exists and its SecurityManager.checkWrite

 

(java.lang.String) method denies write access to the file

Since:

1.2

 

 

2.delete

 

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a 

directory, then the directory must be empty in order to be deleted.

Returns:

true if and only if the file or directory is successfully deleted; false otherwise

 

 

 

3.renameTo

 

public boolean renameTo(File dest)

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename 

operation might not be able to move a file from one filesystem to another, it might not be 

atomic, and it might not succeed if a file with the destination abstract pathname already 

exists. The return value should always be checked to make sure that the rename operation was 

successful.

 

Parameters:

dest - The new abstract pathname for the named file

Returns:

true if and only if the renaming succeeded; false otherwise

这个方法可以用来移动文件夹的同时,修改文件夹的名称(同样适用于文件)

 

 

 

 

 

二、几个容易混淆的方法

 

(1)getPath()与getAbsolutePath()的区别

public static void test1(){

        File file1 = new File(".\\test1.txt");

        File file2 = new File("D:\\workspace\\test\\test1.txt");

        System.out.println("-----默认相对路径:取得路径不同------");

        System.out.println(file1.getPath());

        System.out.println(file1.getAbsolutePath());

        System.out.println("-----默认绝对路径:取得路径相同------");

        System.out.println(file2.getPath());

        System.out.println(file2.getAbsolutePath());

 

}

 

得到的结果:

-----默认相对路径:取得路径不同------

.\test1.txt

D:\workspace\test\.\test1.txt

-----默认绝对路径:取得路径相同------

D:\workspace\test\test1.txt

D:\workspace\test\test1.txt

因为getPath()得到的是构造file的时候的路径。

getAbsolutePath()得到的是全路径

如果构造的时候就是全路径那直接返回全路径

如果构造的时候试相对路径,返回当前目录的路径+构造file时候的路径

 

(2).getAbsolutePath()和getCanonicalPath()的不同

public static void test2() throws Exception{

        File file = new File("..\\src\\test1.txt");

        System.out.println(file.getAbsolutePath());

        System.out.println(file.getCanonicalPath());

    }

得到的结果

D:\workspace\test\..\src\test1.txt

D:\workspace\src\test1.txt

可以看到CanonicalPath不但是全路径,而且把..或者.这样的符号解析出来。

 

(3).

public static void test3() throws Exception{

        File file = new File("D:\\Text.txt");

        System.out.println(file.getCanonicalPath());

    }

 

确定你的系统是Windows系统。

确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:

D:\Text.txt

注意这里试大写的Text.txt

在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果

D:\text.txt

同样的代码得到不同的结果。

同时可以对比getAbsolutePath()看看,这个得到的结果是一样的。

 

原因:

window是大小写不敏感的,也就是说在windows上test.txt和Test.txt是一个文件,所以在windows上当文件不

 

存在时,得到的路径就是按照输入的路径。但当文件存在时,就会按照实际的情况来显示。这也就是建立文件

 

后和删除文件后会有不同的原因。文件夹和文件类似。

分享到:
评论

相关推荐

    总结了一些asp.net 经常需要用到的一些方法和类

    被封装在一起的类有以下几个: Compare------------------数据比较类(如比较时间大小) Cookie-------------------操作Cookie的类 DataBase-----------------数据操作的类 Encrypt------------------...

    用PHP文件上传的具体思路及实现

    也许每一个站点都可能会对上传文件有许多的限制,这些限制会包括 文件类型,文件大小,扩展名,以及上传目录的存在与否,上传文件的存在与否,目录的可写性,可读性,上传文件的改名及怎样把文件从缓存当中复制到你...

    在b/s开发中经常用到的javaScript技术整理

    在b/s开发中经常用到的javaScript技术整理 一、验证类 1、数字验证内 1.1 整数 1.2 大于0的整数 (用于传来的ID的验证) 1.3 负整数的验证 1.4 整数不能大于iMax 1.5 整数不能小于iMin 2、时间类 ...

    MFC文件操作

    相关信息:CFileDialog 用于取文件名的几个成员函数: 假如选择的文件是C:\WINDOWS\TEST.EXE 则(1)GetPathName();取文件名全称,包括完整路径。取回C:\WINDOWS\TEST.EXE (2)GetFileTitle();取文件全名:TEST.EXE (3)...

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

    PHP实现递归目录的5种方法

    这不当前手下的项目就用到了这个,于是总结了几个循环创建目录的方法。 方法一:使用glob循环 <?php //方法一:使用glob循环 function myscandir1($path, &$arr) { foreach (glob($path) as $file) { if (is...

    java面试宝典

    184、Servlet执行时一般实现哪几个方法? 44 185、getServletContext()和getServletConfig()的意思 44 186、Hashtable和HashMap 44 187、JAVA SERVLET API中forward() 与redirect()的区别? 44 189、Can a Java ...

    千方百计笔试题大全

    184、Servlet执行时一般实现哪几个方法? 44 185、getServletContext()和getServletConfig()的意思 44 186、Hashtable和HashMap 44 187、JAVA SERVLET API中forward() 与redirect()的区别? 44 189、Can a Java ...

    C语言中常用的几个头文件及库函数

    不完全统计,C语言标准库中的头文件有15个之多,所以我主要介绍常用的这四个头文件stdio.h,string.h,math.h,stdlib.h,以后用到其他的再做补充。下面上干货: 1.<stdio>:定义了输入输出函数、类型以及宏,函数几乎...

    java 面试题 总结

    创建了几个String Object? 两个 28、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。 public class ThreadTest1{ ...

    net学习笔记及其他代码应用

    创建了几个String Object? 答:两个对象,一个是“xyx”,一个是指向“xyx”的引用对象s。 38.abstract class和interface有什么区别? 答: 声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于...

    超级有影响力霸气的Java面试题大全文档

    创建了几个String Object? 两个 31、EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的?  SessionBean: Stateless Session Bean 的生命周期是由容器决定的,当客户机发出请求要建立一个...

    Linux操作系统基础教程

    Linux 操作系统基础教程 清华大学信息学院计算机系 ...从网上下载的,但是我不推荐易用这种方法得到 Linux,因为仅仅核心就有几十个 Mbit 的 数据量,而一个完整的发行版本大概都是 1Gbit 左右的数据量...

    C# winform对话框用法大全

    对话框中我们常用了以下几种: 1、文件对话框(FileDialog) 它又常用到两个:  打开文件对话框(OpenFileDialog)  保存文件对话(SaveFileDialog)...在介绍的过程中我用到了一个自己开发的类:File,主要是文件操作的……

    C_-SaveFileDialog-各类对话框用法

    C# winform对话框用法大全 收藏 对话框中我们常用了以下几种: 1、文件对话框(FileDialog) 它又常用到两个:  打开文件对话框(OpenFileDialog) ...在介绍的过程中我用到了一个自己开发的类:File,主要是文件操作的

    c#学习笔记.txt

    如前所述,我是一个狮子座男人,一度我认为学习Java会使我看起来与众不同,可是几个月以后我放弃了这个选择,我看了论坛里关于这两种语言孰优孰劣的讨论,最终选择了C#,请不要问我为何做出这样的选择,很多人认为...

    (java se 代码)Bank Account Management System 银行账户管理子系统

    要求3:另外,请为Bank类添加几个统计方法 1.统计银行所有账户余额总数 2.统计所有信用账户透支额度总数 要求4:编写测试类 写个测试类,测试以上代码能否正常工作。 要求5:覆盖toString方法 查看对象的内容。 ...

    java面试题

    答:多形:一个类中多个同名方法。继承:子类继承父类。 jsp内置对象? 答:request 用户端请求 response 回应 pageContext 网页属性 session 会话 out 输出 page 当前网页 exception 错误网页 application ...

    MySQL命令大全

    MySql的用户管理是通过User表来实现的,添加新用户常用的方法有两个,一是在User表插入相应的数据行,同时设置相应的权限;二是通过GRANT命令创建具有某种权限的用户。其中GRANT的常用用法如下: grant all on mydb...

Global site tag (gtag.js) - Google Analytics