java file用法_Java中File常用用法总结

构造函数

1、public File(String pathname)

2、public File(String parent, String child)

在父目录下面创建文件,如果需要可以自己创建父目录,否则报异常,没有发现路径

3、public File(File parent, String child)

在父目录下面创建文件,如果需要可以自己创建父目录,否则报异常,没有发现路径

创建方法

1.boolean createNewFile()   不存在返回true 存在返回false

2.boolean mkdir()  创建一层目录

3.boolean mkdirs() 创建多级目录

删除方法

1.boolean delete()

2.boolean deleteOnExit() 文件使用完成后删除

关于DeleteOnExit的相关解释。

/**

* Requests that the file or directory denoted by this abstract

* pathname be deleted when the virtual machine terminates.

* Files (or directories) are deleted in the reverse order that

* they are registered. Invoking this method to delete a file or

* directory that is already registered for deletion has no effect.

* Deletion will be attempted only for normal termination of the

* virtual machine, as defined by the Java Language Specification.

*

*

Once deletion has been requested, it is not possible to cancel the

* request.  This method should therefore be used with care.

*

*

* Note: this method should not be used for file-locking, as

* the resulting protocol cannot be made to work reliably. The

* {@link java.nio.channels.FileLock FileLock}

* facility should be used instead.

*

* @throws  SecurityException

*          If a security manager exists and its {@link

*          java.lang.SecurityManager#checkDelete} method denies

*          delete access to the file

*

* @see #delete

*

* @since 1.2

*/

判断方法

1.boolean canExecute()判断文件是否可执行

2.boolean canRead()判断文件是否可读

3.boolean canWrite() 判断文件是否可写

4.boolean exists() 判断文件是否存在

5.boolean isDirectory()

6.boolean isFile()

7.boolean isHidden()

8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断

获取方法

1.String getName()

2.String getPath()

3.String getAbsolutePath()

4.String getParent()//如果没有父目录返回null

5.long lastModified()//获取最后一次修改的时间

6.long length()

7.boolean renameTo(File f)

8.File[] liseRoots()//获取机器盘符

9.String[] list()

10.String[] list(FilenameFilter filter)

列出磁盘下的文件和文件夹

文件过滤

FilenameFilter 接口实现文件过滤,实际上利用的是策略模式,必须实现以下的代码,才能实现过滤,

String[] filenames =file.list(new FilenameFilter(){ //file 过滤目录 name 文件名 匿名内部类 public boolean accept(File file,String filename){ return filename.endsWith(".mp3"); } });

我们可以去看他如何实现过滤的。

public String[] list(FilenameFilter filter) {

String names[] = list();

if ((names == null) || (filter == null)) {

return names;

}

List v = new ArrayList<>();

for (int i = 0 ; i < names.length ; i++) {

if (filter.accept(this, names[i])) {

v.add(names[i]);

}

}

return v.toArray(new String[v.size()]);

}

先获得所有的File对象,判断File对象是否为空,如果不为空,且过滤器不为空,将实现的策略Accept去判断哪些File是符合要求的,然后添加到List中,遍历完,然后将List转化为数组,返回给调用的方法。

递归删除文件夹

public static void DeleteAll(File file)

{

if(file.isFile() == true || file.list().length == 0)

{

file.delete();

}

else

{

File []files = file.listFiles();

for(File f : files)

{

DeleteAll(f);

f.delete();

}

}

}

移动文件

找出d盘下所有的 .java 文件,拷贝至 c:\jad 目录下,并将所有文件的类型由.java 修改为.jad 。

代码如下:

public class Test5 {

public static void main(String[] args){

File f1 = new File("d:\\");

moveFile(f1);

}

public static void moveFile(File dir){ File[] files=dir.listFiles(); for(File file:files){ if(file.isDirectory()) moveFile(file); else{ if(file.getName().endsWith(".java")) file.renameTo(new File("c:\\jad\\"+ file.getName().substring(0,file.getName().lastIndexOf('.'))+".jad")); } } } }


版权声明:本文为weixin_39806818原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。