?
?
?新建一個(gè)Java項(xiàng)目,導(dǎo)入jar,新建一個(gè)測(cè)試類(lèi),編寫(xiě)代碼實(shí)現(xiàn)文件操作功能:
?
package com.bw.test;
?
import java.io.FileInputStream;
import java.io.FileOutputStream;
?
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
?
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;
/**
?*
?* @類(lèi)描述 :
?* @作者 :
?* @日期 : 2016-7-8 下午5:10:22
?*
?*/
public class test3 {
?
??? //hdfs操作入口
??? FileSystem fileSystem = null;
???
??? @Before
??? public void setUp() throws Exception {
??????? URI uri = new URI("hdfs://192.192.192.3:9000");
??????? Configuration configuration = new Configuration();
??????? fileSystem = FileSystem.get(uri, configuration,"root");
??? }
??? @Test
??? public void list() throws Exception {
??????? //遞歸方式取得指定目錄下的文件信息
??????? FileStatus[] liFileStatus = fileSystem.listStatus(new Path("/"));
??????? //輸出取得的文件權(quán)限和文件名稱(chēng)
??????? for (FileStatus fileStatus : liFileStatus) {
??????????? System.out.println(fileStatus.getPermission()+" "+fileStatus.getPath());
??????? }
??? }
??? //上傳
??? @Test
??? public void put() throws Exception {
??????? //是否覆蓋同名文件(前提是也在同一目錄下)標(biāo)志位
??????? boolean overwrite = true;
??????? //DFS文件輸出流
??????? FSDataOutputStream outputStream = fileSystem.create(new Path("/hbase-1.1.2-bin.tar.gz"),overwrite);
??????? //本地輸入流
??????? InputStream inputStream = new FileInputStream("C:\Users\YAN\Desktop\開(kāi)發(fā)指南\Hadoop\05. HBase使用指南\安裝程序\hbase-1.1.2-bin.tar.gz");
??????? //使用工具類(lèi)上傳
??????? IOUtils.copy(inputStream, outputStream,4096);
??? }
??? //下載
??? @Test
??? public void get() throws Exception {
??????? //DFS文件輸入流
??????? FSDataInputStream inputStreamOption = fileSystem.open(new Path("/hbase-1.1.2-bin.tar.gz"));
??????? //本地文件輸出流
??????? OutputStream outputStream = new FileOutputStream("E:\hbase-1.1.2-bin.tar.gz");
??????? //使用工具類(lèi)下載
??????? IOUtils.copy(inputStreamOption, outputStream,4096);
??? }
??? //刪除
??? @Test
??? public void del() throws Exception {
??????? //是否刪除
??????? boolean b = true;
??????? //DFS文輸入流
??????? fileSystem.delete(new Path("/input"),b);
??? }
?
}
?
本文摘自 :https://blog.51cto.com/u