(1)创建目录及文件
private static void createFile(String fileName) throws IOException {
Path dir = Paths.get(fileName);
Files.createDirectories(dir.getParent());
try{
Files.createFile(dir);
System.out.println("创建文件成功!文件位置:" + fileName);
}
catch(FileAlreadyExistsException e) {
System.out.println("文件 " + fileName + " 已存在,无需创建!");
}
System.out.println();
}
(2)基于BufferedWriter写入文件
private static void writeToFile(String fileName, String content) throws IOException{
String[] lines = content.split("\r\n");
try( BufferedWriter output = new BufferedWriter(new FileWriter(fileName)); ) {
for (String line: lines) {
output.write(line);
output.newLine();
}
output.flush();
System.out.println("数据写入成功!文件位置:" + fileName);
}
System.out.println();
}
(3)基于基本IO复制文件
private static void copyByIO(String sourceFile, String targetFile) throws IOException {
try( InputStream input = new FileInputStream(sourceFile);
OutputStream output = new FileOutputStream(targetFile); ){
int size = 4;
byte[] buffer = new byte[size];
int len;
int cnt = 0;
while( (len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
cnt++;
}
System.out.println(sourceFile + " -> " + targetFile + " 复制成功!");
System.out.println("字节数组缓冲区大小为" + size + "时,读写循环次数为" + cnt + "\n");
}
}
(4)基于NIO复制文件
private static void copyByNIO(String sourceFile, String targetFile) throws IOException {
Path source = Paths.get(sourceFile);
Path target = Paths.get(targetFile);
Files.createDirectories(target.getParent());
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
System.out.println(sourceFile + " -> " + targetFile + " 复制成功!\n");
}
(5)删除文件
private static void deleteFile(String fileName) throws IOException {
Path dir = Paths.get(fileName);
Files.deleteIfExists(dir);
System.out.println(fileName + " 删除成功!\n");
}
(6)递归遍历指定目录下所有文件
private static void walkDirectories(String dir) throws IOException {
Path d = Paths.get(dir);
Files.walk(d)
.forEach(System.out::println);
}
完整代码
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
public class IOTest {
public static void main(String[] args) throws IOException {
String fileName = "C:/example/from.txt";
System.out.println("----- 创建文件 ------");
createFile(fileName);
System.out.println("----- 将字符串写入文件 -------");
String str =
"白日依山尽\r\n" +
"黄河入海流\r\n" +
"欲穷千里目\r\n" +
"更上一层楼\r\n";
writeToFile(fileName, str);
System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
String toFile = "C:/example/to.txt";
copyByIO(fileName, toFile);
System.out.println("--------- 基于NIO实现文件的复制 ----------");
String toFile2 = "C:/example/nio/to.txt";
copyByNIO(fileName, toFile2);
System.out.println("---------- 删除指定文件 -------------");
deleteFile(toFile);
System.out.println("---------- 遍历指定目录文件 -------------");
String dir = "C:/example";
walkDirectories(dir);
}
private static void createFile(String fileName) throws IOException {
Path dir = Paths.get(fileName);
Files.createDirectories(dir.getParent());
try{
Files.createFile(dir);
System.out.println("创建文件成功!文件位置:" + fileName);
}
catch(FileAlreadyExistsException e) {
System.out.println("文件 " + fileName + " 已存在,无需创建!");
}
System.out.println();
}
private static void writeToFile(String fileName, String content) throws IOException{
String[] lines = content.split("\r\n");
try( BufferedWriter output = new BufferedWriter(new FileWriter(fileName)); ) {
for (String line: lines) {
output.write(line);
output.newLine();
}
output.flush();
System.out.println("数据写入成功!文件位置:" + fileName);
}
System.out.println();
}
private static void copyByIO(String sourceFile, String targetFile) throws IOException {
try( InputStream input = new FileInputStream(sourceFile);
OutputStream output = new FileOutputStream(targetFile); ){
int size = 4;
byte[] buffer = new byte[size];
int len;
int cnt = 0;
while( (len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
cnt++;
}
System.out.println(sourceFile + " -> " + targetFile + " 复制成功!");
System.out.println("字节数组缓冲区大小为" + size + "时,读写循环次数为" + cnt + "\n");
}
}
private static void copyByNIO(String sourceFile, String targetFile) throws IOException {
Path source = Paths.get(sourceFile);
Path target = Paths.get(targetFile);
Files.createDirectories(target.getParent());
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
System.out.println(sourceFile + " -> " + targetFile + " 复制成功!\n");
}
private static void deleteFile(String fileName) throws IOException {
Path dir = Paths.get(fileName);
Files.deleteIfExists(dir);
System.out.println(fileName + " 删除成功!\n");
}
private static void walkDirectories(String dir) throws IOException {
Path d = Paths.get(dir);
Files.walk(d)
.forEach(System.out::println);
}
}