Home  >  Article  >  Java  >  Application example tutorial of FileUtils class

Application example tutorial of FileUtils class

零下一度
零下一度Original
2017-07-03 10:09:412835browse

FileUtils class application

1. Write a file;

2. Read from the file;

3. Create a folder, including folders ;

4. Copy files and folders;

5. Delete files and folders;

6. Get files from URL address;

7. List files and folders by file filter and extension;

8. Compare file contents;

9. File last modification time;

10. Calculate checksum.

1. Method of copying files or folders:

Example:

 1 public class CopyFileorDirectory {
 2     public static void main(String[] args) throws Exception {
 3         File file1 =new File("path1");
 4         File file2 =new File("path2");
 5         File file3 =new File("path3");
 6         File file4 =new File("path4");
 7         File file5 =new File("path5");
 8         //将文件复制到指定文件夹中,保存文件日期的时间。
 9         // 该方法将指定源文件的内容复制到指定目标目录中相同名称的文件中。
10         // 如果不存在,则创建目标目录。如果目标文件存在,则该方法将覆盖它。
11         FileUtils.copyFileToDirectory(file1,file2);//文件不重命
12         //将文件复制到一个新的地方(重命名文件)并保存文件日期的时间。
13         FileUtils.copyFile(file1,file3);
14 
15         //复制文件夹到指定目录下,如果指定目录不存在则创建
16         FileUtils.copyDirectoryToDirectory(file2,file4);
17 
18         //复制文件夹到指定目录下并重命名
19         FileUtils.copyDirectory(file4,file5);
20 
21         //该方法将指定的源目录结构复制到指定的目标目录中。
22         FileUtils.copyDirectory(file4,file5, DirectoryFileFilter.DIRECTORY);
23 
24         // 复制文件夹下第一级内容中指定后缀文件
25         IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
26         IOFileFilter txtFiles = FileFilterUtils.and(FileFileFilter.FILE, txtSuffixFilter);
27         FileUtils.copyDirectory(file4,file5, txtFiles);
28 
29         // 复制文件目录结构及文件夹下第一级目录内指定后缀文件
30         FileFilter filter = FileFilterUtils.or(DirectoryFileFilter.DIRECTORY, txtFiles);
31         FileUtils.copyDirectory(file4,file5, filter,false);//preserveFileDate参数默认为true。
32 
33         //将字节从URL源复制到文件目的地。如果它们还不存在,则将创建到目的地的目录。如果已经存在,文件将被覆盖。
34         URL source = new URL("http://imgsrc.baidu.com/baike/pic/ewe.jpg");
35         FileUtils.copyURLToFile(source,file5,1000,1000);
36 
37         // 等待NFS传播文件创建,并强制执行超时。该方法重复测试File.exists(),直到它返回true,或直到秒内指定的最大时间。
38         File file = new File("/abc/");
39         boolean d = FileUtils.waitFor(file,100);
40         System.out.println(d);
41     }
42 }

2. Method of deleting files or files

 1 public class FileorDirectoryDelete {
 2     public static void main(String[] args) throws Exception{
 3         File file = new File("path1");
 4         File directory = new File("path2");
 5         //递归删除一个目录(包括内容)。
 6         FileUtils.deleteDirectory(directory);
 7 
 8         //删除一个文件,不会抛出异常。如果文件是一个目录,删除它和所有子目录。
 9         FileUtils.deleteQuietly(file);
10 
11         //清理内容而不删除它。
12         FileUtils.cleanDirectory(directory);
13 
14         //删除一个文件,会抛出异常
15         //如果file是文件夹,就删除文件夹及文件夹里面所有的内容。如果file是文件,就删除。
16         //如果某个文件/文件夹由于某些原因无法被删除,会抛出异常
17         FileUtils.forceDelete(file);
18     }
19 }

3. Create directory

 1 public class CreatDirectory {
 2     public static void main(String[] args) throws Exception {
 3         File file = new File("path");
 4         //创建一个文件夹,如果由于某些原因导致不能创建,则抛出异常
 5         //一次可以创建单级或者多级目录
 6         FileUtils.forceMkdir(new File("/Users/wuguibin/Downloads/folder"));
 7         //为指定文件创建文件的父级目录
 8         FileUtils.forceMkdirParent(file);
 9     }
10 }

4. Move files or folders

//移动文件夹,并重新命名
FileUtils.moveDirectory(new File("/Users/Downloads/file1"),
       new File("/Users/Downloads/file2/file3"));

//移动文件夹,并给定是否重命名
FileUtils.moveDirectoryToDirectory(new File("/Users/Downloads/file1"),
       new File("/Users/Downloads/file2/"),false);
//移动文件到指定文件夹中,并重新命名
FileUtils.moveFile(file1,new File("/Users/Downloads/海葡萄.jpen"));
//移动文件到指定文件夹中,并给定是否创建文件夹
FileUtils.moveFileToDirectory(new File("/Users/Downloads/海葡萄.jpeg"),
        new File("/Users/Downloads/file2"),false);

5 , Determine whether the files are the same or contain relationships, get the size of the file or folder

//确定父目录是否包含指定子元素(一个文件或目录)。即directory是否包含file2,在比较之前,文件是标准化的。
boolean a = FileUtils.directoryContains(directory,file2);
//比较两个文件的内容,以确定它们是否相同。
boolean b = FileUtils.contentEquals(file1, file2)

//Get the size of the specified file or folder, it may overflow and become a negative value
long l = FileUtils .sizeOf(file1);
System.out.println(l+"KB");
//Get the size of the specified file or folder without overflowing
BigInteger bi= FileUtils.sizeOfAsBigInteger(file1);
System.out.println(bi+"kb");

//Recursively calculate the size of a directory (the sum of the lengths of all files).
//Note that sizeOfDirectory() does not detect overflow, and if overflow occurs, the return value may be negative. The sizeOfDirectoryAsBigInteger() method does not overflow.
FileUtils.sizeOfDirectory(file1);
FileUtils.sizeOfDirectoryAsBigInteger(file1);

6. Compare the old and new files

  //比较指定文件是否比参考文件创建或修改后时间晚
  boolean b = FileUtils.isFileNewer(file1,file2));
 
  //如果指定的文件比指定的日期更新。
  SimpleDateFormat date = new SimpleDateFormat("yyyy/MM/dd");
  String date1 = "2017/06/20";
  boolean c = FileUtils.isFileNewer(file1,date.parse(date1));
  boolean d = FileUtils.isFileNewer(file1,23243);
        
   //指定文件创建或修改后的时间是否比参考文件或日期早
   FileUtils.isFileOlder(file1,232434);
   FileUtils.isFileOlder(file1,System.currentTimeMillis());

7. Write Enter the file

 //把集合里面的内容写入文件,以指定字符串结束写入
 //void writeLines(File file,Collection<?> lines,String lineEnding,boolean append)
 ArrayList<String> list = new ArrayList<>();
 String str1 = "Java";
 String str2 = "JSP";
 list.add(str1);
 list.add(str2);
 FileUtils.writeLines(file8,"GBK",list,"java",true);

 //把字符串写入文件
 //参数1:需要写入的文件,如果文件不存在,将自动创建。  参数2:需要写入的内容
 //参数3:编码格式     参数4:是否为追加模式( ture: 追加模式,把字符串追加到原内容后面)
 String data1 = "认真";
 FileUtils.writeStringToFile(file,data1, "UTF-8", true);

 //把字节数组写入文件
 byte [] buf = {13,123,34};
 System.out.println(new String(buf));
 FileUtils.writeByteArrayToFile(file13,buf,0,buf.length,true);

8. Read the file and obtain the input and output stream

 //将文件的内容读入一个字符串中。
 String str =  FileUtils.readFileToString(file,"UTF-16" ); 
 FileUtils.readFileToByteArray(file);
 //把文件读取到字节数组里面
 byte[] readFileToByteArray(final File file)

 //把文件读取成字符串 ;Charset encoding:编码格式
 String readFileToString(final File file, final Charset encoding)

 //把文件读取成字符串集合 ;Charset encoding:编码格式 List<String> list4 =FileUtils.readLines(
       new File("/Users/Shared/笔记/java.txt"),"UTF-8");
 Iterator it = list4.iterator();
 while (it.hasNext()){
      Object obj=it.next();
      System.out.println(obj);
  }


 //获取输入流
FileUtils.openInputStream(file);
 //获取输出流
FileUtils.openOutputStream(file);

The above is the detailed content of Application example tutorial of FileUtils class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn