文章目录
- 一、文件操作
- 1. 删除 Linux 上的文件夹:
- 2. 遍历目录下的文件:
- 3. 文件重命名:
- 4. hdfs 文件重命名:
- 5. hdfs 文件上传:
- 6. hdfs 遍历目录下的文件:
一、文件操作
1. 删除 Linux 上的文件夹:
import java.io.File;
// 删除临时目录
File directory = new File("/mnt/xiaoqiang");
// 获取目录下子文件
File[] filestmp = directory.listFiles();
// 遍历该目录下的文件对象
for (File f : filestmp) {
if (f.getName().endsWith(".parquet") || f.getName().endsWith(".crc")) {
// 删除文件
f.delete();
}
}
directory.delete();
System.out.println("/mnt/xiaoqiang" + " 临时目录已删除");
2. 遍历目录下的文件:
方法一:使用递归
import java.io.File;
public class FileTraversal {
public static void main(String[] args) {
File folder = new File("目标目录的路径");
traverseFolder(folder);
}
public static void traverseFolder(File folder) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
traverseFolder(file);
} else {
System.out.println(file.getAbsolutePath());
}
}
}
} else {
System.out.println(folder.getAbsolutePath());
}
}
}
方法二:使用栈
import java.io.File;
import java.util.Stack;
public class FileTraversal {
public static void main(String[] args) {
File folder = new File("目标目录的路径");
traverseFolder(folder);
}
public static void traverseFolder(File folder) {
Stack<File> stack = new Stack<>();
stack.push(folder);
while (!stack.isEmpty()) {
File currentFile = stack.pop();
if (currentFile.isDirectory()) {
File[] files = currentFile.listFiles();
if (files != null) {
for (File file : files) {
stack.push(file);
}
}
} else {
System.out.println(currentFile.getAbsolutePath());
}
}
}
}
方法三:使用队列
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
public class FileTraversal {
public static void main(String[] args) {
File folder = new File("目标目录的路径");
traverseFolder(folder);
}
public static void traverseFolder(File folder) {
Queue<File> queue = new LinkedList<>();
queue.offer(folder);
while (!queue.isEmpty()) {
File currentFile = queue.poll();
if (currentFile.isDirectory()) {
File[] files = currentFile.listFiles();
if (files != null) {
for (File file : files) {
queue.offer(file);
}
}
} else {
System.out.println(currentFile.getAbsolutePath());
}
}
}
}
上述是几种不同的方法来遍历目录下的文件,包括递归、栈和队列。通过递归的方法,可以在遇到子文件夹时自动进入子文件夹继续遍历;而栈和队列的方法通过维护一个待处理的文件集合,不断处理集合中的文件,可以实现相同的效果。这些方法都可以正确地遍历目录下的文件。
3. 文件重命名:
import java.io.File;
public class RenameFileExample {
public static void main(String[] args) {
// 创建File对象指向旧文件
File oldFile = new File("oldFileName.txt");
// 创建File对象指向新文件名
File newFile = new File("newFileName.txt");
// 使用renameTo方法进行重命名
boolean renamed = oldFile.renameTo(newFile);
// 输出结果
if (renamed) {
System.out.println("文件重命名成功!");
} else {
System.out.println("文件重命名失败,确保文件未被打开且路径正确。");
}
}
}
请确保在调用 renameTo
方法之前,旧文件存在,并且没有其他进程正在使用该文件。同时,确保应用程序有足够的权限去更改文件名,并且目标路径存在。如果重命名操作失败,renameTo
方法会返回false。
4. hdfs 文件重命名:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
public class RenameHdfsFile {
public static void main(String[] args) throws Exception {
// 假设 HDFS 的 URI 是 "hdfs://namenode:8020",需要根据实际情况进行修改
String hdfsUri = "hdfs://namenode:8020";
String oldFileName = "/path/to/oldfile.txt";
String newFileName = "/path/to/newfile.txt";
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(URI.create(hdfsUri), conf);
// 使用FileSystem的rename方法重命名文件
boolean renamed = hdfs.rename(new Path(oldFileName), new Path(newFileName));
if (renamed) {
System.out.println("文件重命名成功!");
} else {
System.out.println("文件重命名失败!");
}
hdfs.close();
}
}
确保在运行此代码之前,已经设置好Hadoop的相关配置,并且有权限对HDFS上的文件进行操作。此外,需要有一个编译Java的环境,并且确保已经添加了Hadoop的相关依赖库。
5. hdfs 文件上传:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.net.URI;
public class HdfsUpload {
public static void main(String[] args) throws Exception {
// 检查并设置参数
if (args.length != 2) {
System.err.println("Usage: <source> <destination>");
System.exit(1);
}
String localFile = args[0];
String hdfsFile = args[1];
// 配置HDFS
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:8020"); // HDFS的URI
// 创建FileSystem实例
FileSystem fileSystem = FileSystem.get(conf);
// 上传文件
fileSystem.copyFromLocalFile(new Path(localFile), new Path(hdfsFile));
System.out.println("File uploaded successfully");
}
}
6. hdfs 遍历目录下的文件:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
public class HdfsTraversal {
public static void main(String[] args) throws IOException {
// HDFS路径
String hdfsPath = "hdfs://namenode:8020/your/hdfs/path";
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(URI.create(hdfsPath), conf);
// 调用递归遍历方法
traverseHdfs(hdfs, new Path(hdfsPath));
hdfs.close();
}
private static void traverseHdfs(FileSystem hdfs, Path path) throws IOException {
FileStatus[] files = hdfs.listStatus(path);
for (FileStatus file : files) {
if (file.isDirectory()) {
traverseHdfs(hdfs, file.getPath());
// 如果需要处理子目录,可以在这里添加代码
} else {
// 这里处理文件
System.out.println(file.getPath());
}
}
}
}