static代码块设置全局变量和eclipse java配好HDFS类对HDFS的操作

看到static就想到分享,听到这句话的时候稍微有了感觉
static 静态随着类的创建同时进入内存,他的消失也是和类同时。这样就会大大的减少运行时间,同时他还有static直接【类.】引用的特性,减轻了程序员的负担
而static代码块也是在类加载的时候加载,同时在实例化的构造方法的时候只执行一次,意思就是说我们每次在调用这个类的时候我们就只有第一次执行一次这个静态代码块,等下次调用的时候JVM就不需要将内存再分配一次,这样大大减少了内存的消耗,提高程序的韧度。(注,同时在静态全局变量的时候我们可以在类中引用。【类中可以new但是不可使用调用方法,所以作用比较大】)
谈到静态代码块就不得不提初始化语句块也就 {} 它的加载在main之后在类之前,且实例化的时候只执行一次。

将String 字符串转换成Path路径(这是JDK 1.7的新特性)
String p=new String("");
Path p=new Path§;


/**
 * 
 *HDFS的工具类,用来实现对HDFS的操作
 *	1.文件的上传、下载、移动(重命名)、删除
 *	2.目录的增、删、改、查
 *	3.将文件从文件上传到HDFS上
 *		inputPath 要上传文件的路径()
 *		outPath   目标路径(HDFS路径)
 */
public class HDFutils {
	
	private static Configuration conf=null;
	private static FileSystem fs=null;
	//对于静态变量的实例化,通常在静态代码块中实现
	//代码块{}
	static {
		conf=new Configuration();
		URI uri;
		conf.set("dfs.replication","1");
		try {
			uri = new URI("hdfs://hadoop:9000");
			fs=FileSystem.get(uri,conf,"root");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//Alt + / 快捷提示
	//Ctrl+shift+o 快速导包
	//Ctrl +1 快捷提示
	/**
	 * 
	 * @param inputPath 要上传文件的路径()
	 * @param outputPath 目标路径(HDFS路径)
	 */
	public static void put(String inputPath,String outputPath) {
			InputStream in=null;
			OutputStream out=null;
			try {
				in=new FileInputStream(inputPath);
				out=fs.create(new Path(outputPath));
				 IOUtils.copy(in, out);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO Auto-generated catch block
			}finally {
				try {
					in.close();
					out.close();

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}	
	}
	//创建目录
	public static void mkdir(String path) {
		
		try {
			fs.mkdirs(new Path(path));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	 //改
	public static void rename(String exsitedpath,String nowPath) {
		try {
			fs.rename(new Path(exsitedpath),new Path(nowPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//删除
@SuppressWarnings("deprecation")
	public static void delete(String path) {
		try {
			fs.delete(new Path(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//查

	public static void check(String path) {
		try {
			fs.listStatus(new Path(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

没有设置全局变量的HDFS上传下载方法详细注释

public static void get() throws Exception {
// //1、获取HDFS配置信息
// Configuration conf=new Configuration();
// //2.获取HDFS系统对象
// //FileSystem表示hadoop的文件系统
// URI uri=new URI(“hdfs://192.168.56.100:9000”);
// //fs表示hdfs文件系统
// FileSystem fs=FileSystem.get(uri,conf);
// //输出流,将数据写入到指定文件
// OutputStream out=new FileOutputStream(“a.text”);
// //输入流,读取数据到内存
//
// InputStream in=fs.open(new Path("/hadoop-2.7.3.tar.gz"));
// //通过输入流输出流拷贝的方式,进行数据的写入
// IOUtils.copy(in, out);
// //关闭资源
//
// in.close();
// out.close();
// fs.close();
//
// }
// public static void put() throws Exception{
// //1.创建配置信息
// Configuration conf=new Configuration();
//
// //指定上传文件的副本数量
// conf.set(“dfs.replication”, “1”);
// //2.获取HDFS文件系统对象
// URI uri=new URI(“hdfs://192.168.56.100:9000”);
// FileSystem fs=FileSystem.get(uri,conf);
// //3、输入流和输出流
// InputStream in=new FileInputStream(“a.text”);
// OutputStream out=fs.create(new Path("/hadoop.text"));
// IOUtils.copy(in, out);
// in.close();
// out.close();
// fs.close();
// }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_Octopus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值