maven做如下配置
groupId:org.apache.hadoop
artifactId:hadoop-client
version:${hadoop.version}
代碼如下:
獲取文件系統
private static FileSystem getFileSystem() throws Exception{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(conf);
return fileSystem;
}
寫文件
private static void write(String toFolder) throws Exception{
FileSystem fileSystem = getFileSystem();
fileSystem.delete(new Path(toFolder), true);
FileInputStream in = null;
FSDataOutputStream out = null;
try {
in = new FileInputStream(new File("E:\\1804211523.txt"));
out = fileSystem.create(new Path(toFolder));
IOUtils.copyBytes(in, out, 4096, false);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
}
讀取文件:
private static void read(String filePath) throws Exception{
//預處理,刪除文件加
FileSystem fileSystem = getFileSystem();
Path path = new Path(filePath);
FSDataInputStream inStream = null;
//用文件系統操作路徑
try{
inStream = fileSystem.open(path);
IOUtils.copyBytes(inStream, System.out, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream);
}
}