前提:1、搭建好hadoop伪分布式环境;2、安装好eclipse;
注:修改 /etc/hosts 添加 “本机IP hadoop01” ,
那么代码中创建hdfs文件系统的时候的URI hdfs://hadoop01:9000 相当于 hdfs://hadoop服务器ip(例如:192.168.1.1XX):9000
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
public class demo1 {
	private FileSystem fileSystem = null;
	private String location = "";
	@Before
	public void init() throws URISyntaxException, IOException {
		// 初始化文件系统,连接到hdfs的namenode,new Configuration 代表默认配置
		fileSystem = FileSystem.get(new URI("hdfs://hadoop01:9000"),
				new Configuration());
		// 下载文件的路径(文件位于电脑的绝对路径)
		location = "/data/downloads";
	}
	@Test
	public void testUpload() throws IOException {
		// Path “/” 代表 hadoop hdfs 的根路径
		String fileName = "A.tar.gz";
		InputStream in = new FileInputStream(location + fileName);
		OutputStream out = fileSystem.create(new Path("/hadoop.tar.gz"));
		IOUtils.copyBytes(in, out, 4096, true);
	}
	@Test
	public void testDownload() throws URISyntaxException, IOException {
		InputStream in = fileSystem.open(new Path("/hadoop.tar.gz"));
		String fileName = "A.tar.gz";
		OutputStream out = new FileOutputStream(location + fileName);
		IOUtils.copyBytes(in, out, 4096, true);
	}
	@Test
	public void testDel() throws IllegalArgumentException, IOException {
		// true 递归删除,用于删除文件夹
		boolean flag = fileSystem.delete(new Path("/word.text"), false);
		System.out.println(flag);
	}
}
原文:http://www.cnblogs.com/lixiongqing/p/4572916.html