📅  最后修改于: 2020-11-18 08:15:03             🧑  作者: Mango
提供操作文件的方法,例如移动,打开,检查存在性,读取文件等。这些方法使用File Object。
以下是org.apache.commons.io.FileUtils类的声明-
public class FileUtils
extends Object
这是我们需要解析的输入文件-
Welcome to TutorialsPoint. Simply Easy Learning.
IOTester.java
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
public class IOTester {
public static void main(String[] args) {
try {
//Using FileUtils
usingFileUtils();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public static void usingFileUtils() throws IOException {
//get the file object
File file = FileUtils.getFile("input.txt");
//get the temp directory
File tmpDir = FileUtils.getTempDirectory();
System.out.println(tmpDir.getName());
//copy file to temp directory
FileUtils.copyFileToDirectory(file, tmpDir);
//create a new file
File newTempFile = FileUtils.getFile(tmpDir, file.getName());
//get the content
String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset());
//print the content
System.out.println(data);
}
}
它将打印以下结果。
Temp
Welcome to TutorialsPoint. Simply Easy Learning.