引入依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>

常用方法

writeStringToFile

写入字符串到指定文件,直接覆盖掉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File file = new File("E://backup//a.txt");
FileUtils.writeStringToFile(file,"222","UTF-8");
}
}

readFileToString

指定编码表读取内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File file = new File("E://backup//a.txt");
String str=FileUtils.readFileToString(file,"UTF-8");
System.out.println(str);
}
}

copyFile

复制一个文件的内容到另一个文件,如果目标文件不存在那么会新建它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File file = new File("E://backup//a.txt");
File file2 = new File("E://backup//a2.txt");
FileUtils.copyFile(file, file2);
}
}

copyDirectory

复制一个文件夹的内容到另一个文件夹,如果目标文件夹不存在那么会新建它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File srcDir=new File("E://backup//1");
File destDir=new File("E://backup//2");
FileUtils.copyDirectory(srcDir, destDir);
}
}

copyDirectoryToDirectory

复制一个文件夹及其内容到另一个文件夹,如果目标文件夹不存在那么会新建它(注意:本方法和上一个方法存在差异)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File srcDir=new File("E://backup//1");
File destDir=new File("E://backup//2");
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
}
}

forceDelete

强制删除文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File file=new File("E://backup//a.txt");
FileUtils.forceDelete(file);
}
}

deleteQuietly

删除文件,常用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ledao;

import org.apache.commons.io.FileUtils;

import java.io.File;

/**
* @author LeDao
* @company
* @create 2022-01-24 23:48
*/
public class MyTest {

public static void main(String[] args) {
File file = new File("E://data//a.txt");
FileUtils.deleteQuietly(file);
}
}

deleteDirectory

删除文件夹,文件夹不为空也可以删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.apache.commons.io.FileUtils;

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

/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) throws IOException {
File file=new File("E://backup//1");
FileUtils.deleteDirectory(file);
}
}