package com.hdfs;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import java.io.IOException;public class FileSystemOperationDemo1 { public static void main(String[] args) throws IOException { //getter connection Configuration conf = new Configuration(); //create core path Path corePath = new Path("/usr/local/hadoop/etc/hadoop/core-site.xml"); //create hdfspath Path hdfsSiteXmlPath = new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml"); //add resource to connection conf.addResource(corePath); conf.addResource(hdfsSiteXmlPath); Path hdfsPath = new Path("hdfs://192.168.92.132:8020/TestDir/"); FileSystem fs = hdfsPath.getFileSystem(conf); //Determine whether the directory or file is exists boolean b = fs.exists(hdfsPath); if (!b) { fs.mkdirs(hdfsPath);//create file instance } //print hostname java.lang.String hostname = conf.get("fs.default.name"); System.out.println(hostname); //copy HDFS to Localhost fs.copyToLocalFile(new Path("hdfs://192.168.92.132:8020/profile"), new Path("/")); fs.copyFromLocalFile(new Path("/etc/profile"), new Path("hdfs://192.168.92.132:8020/TestDir")); FileStatus status = fs.getFileStatus(new Path("hdfs://192.168.92.132:8020/profile")); //getter absolute path System.out.println("path:" + status.getPath()); //getter relative path System.out.println("path:" + status.getPath().toUri().getPath()); //get block size System.out.println("blockSize:" + status.getBlockSize()); //get group System.out.println("Group:" + status.getGroup()); //get Owner System.out.println("Owner:" + status.getOwner()); // getter all statuses FileStatus[] statuses = fs.listStatus(new Path("hdfs://192.168.92.132:8020/TestDir")); for (FileStatus fileStatus : statuses) { System.out.println("relative path:" + fileStatus.getPath().toUri().getPath()); } FSDataInputStream fdis = fs.open(new Path("hdfs://192.168.92.132:8020/profile")); byte[] buff = new byte[128]; int length = 0; while ((length = fdis.read(buff, 0, 128)) != -1) { System.out.println(length); System.out.println(new java.lang.String(buff,0,length)); } System.out.println(fdis.getPos()); fdis.seek(10); while ((length = fdis.read(buff, 0, 128)) != -1) { System.out.println(new java.lang.String(buff, 0, length)); } fdis.seek(0); byte[] buff2 = new byte[128]; fdis.read(buff2, 0, 128); System.out.println(new java.lang.String(buff2)); System.out.println(buff2.length); fs.close(); }}