import org.apache.hadoop.conf.*; import org.apache.hadoop.fs.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; /*** * 注意:文件创建时,可能会报Permission denied错误,其中一个解决办法是 给要写入的文件所在目录增加其他人写的权限, 在Linix下输入命令 $ hdfs dfs -chmod o+w /user/ud16 * @author * */ public class CreateFile { public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { Configuration conf=new Configuration(); //注意修改为自己的IP URI uri=new URI("hdfs://192.168.1.100:9000"); //get()的第三个参数"hadoop"为Linux的用户名,注意修改 FileSystem fs=FileSystem.get(uri,conf,"hadoop"); //定义一个要创建文件的路径 Path dfs=new Path("/user/hadoop/myfile1"); FSDataOutputStream newFile=fs.create(dfs,true); //写入的内容 newFile.writeBytes("20160216048 raodi"); } }