如何自建工具类

需求:

  • 在工作中,我们总是会写重复的代码,我们可以将重复的代码封装成工具类来使用
  • 工具类一般用Util结尾
  • 工具类的方法一般都是静态方法
  • 譬如 我这里写了一个文件拷贝的工具类
package com.fs.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
	/**
	 * @param source 被复制的文件路径
	 * @param target 复制文件存放的路径
	 * @throws IOException 
	 */
	public static void copy(String source, String target) throws IOException {
		File sourceFile = new File(source);
		File targetFile = new File(target);
		
		byte[] bs = new byte[1024];
		InputStream in = new FileInputStream(sourceFile);
		OutputStream out = new FileOutputStream(targetFile, true);
		
		while (in.read(bs) != - 1) {
			out.write(bs);
		}
		
		in.close();
		out.close();
	}
}

如何将工具类导出为jar包

  • 选中工具类 --> file --> Export --> Java --> JAR file --> next -->选择要导出的文件 --> 指定导出的jar存放路径 --> finish

如何将jar包导入项目

  • 在src下面新建一个包,包名一般为lib
  • 将jar包复制进lib中
  • 选中jar包,右键–> build path --> add to build path
  • 至此,就可以在新项目中使用自己造的轮子。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值