- 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/60533926fe0d5f25b9672abf2d1817fb.png)
//假设 /user/hadoop/input路径已存在 如没存在 在命令行输入 hadoop fs -mkdir -p /user/hadoop/input
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String localFileName = "/home/hadoop/myfile.txt";
String remoteFileName = "/user/hadoop/input/myfile.txt";
Path localFilePath = new Path(localFileName);
Path remoteFilePath = new Path(remoteFileName);
FileSystem fs = FileSystem.get(conf);
if(!fs.exists(remoteFilePath)) { 远程文件不存在
System.out.println("文件未在远程服务器存在");
fs.copyFromLocalFile(localFilePath, remoteFilePath);
System.out.println("文件已上传到服务器");
}else { //远程文件已存在
System.out.println("文件已在远程服务器存在,请选择下一步动作:1 覆盖 ;2 添加到文件末尾 ");
int c