// 创建目录 public static void mkdir(String dirPath) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.mkdirs(new Path(dirPath));
fileSystem.close();
}
// 创建文件 public static void createFile(String filePath) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.create(new Path(filePath));
fileSystem.close();
}
// 删除目录或者文件 public static void deleteFile(String filePath) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.deleteOnExit(new Path(filePath));
fileSystem.close();
}
// 遍历文件 public static void getListFile(String filePath) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
FileStatus[] fileStatus = fileSystem.listStatus(new Path(filePath));
for (FileStatus file_Status : fileStatus) {
System.out.println(file_Status.getPath().toString());
}
fileSystem.close();
}
// 上传文件 public static void upLoadFile(String src, String dest) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.copyFromLocalFile(new Path(src), new Path(dest));
fileSystem.close();
}
// 下载文件 public static void downloadFile(String src, String dest) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.copyToLocalFile(new Path(src), new Path(dest));
fileSystem.close();
}
// 写文件 public static void writeFile(String filePath) throws IOException {
Configuration configuration = HadoopConfig.getConfiguration();
FileSystem fileSystem = FileSystem.get(configuration);
Path path = new Path(filePath);
FSDataOutputStream out = fileSystem.create(path);
out.writeUTF("我不是学霸,哈哈哈");
fileSystem.close();
}