工作中的几个程序例子

批量解压 改名

#!/bin/bash
cd /root/test
for i in $(ls *.zip | awk -F. '{print $1}')
do
Zname=`unzip -l $i.zip | grep -o '\<Simple.*log$'` 
echo $i $Zname
unzip $i.zip &&	mv Simple*.log $i.log
done

exit 0

入参是一堆日志文件
1、循环日志文件列表
2、循环单个文件的行 判断是否包含关键字luyou LUYOU_SDK_COCOS2DX_SUCCESS
3、生成新文件(命名与原文件名相同) 写入包含关键字的那些行
出参也是一堆文件 只包含关键行 
根据文件夹 得到文件夹下全部文件

public class hang01 {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		int i;
		File file1 = new File("C:\\Users\\user\\Desktop\\log");

		String[] fileList = file1.list();
		System.out.println("====当前路径下所有文件和路径如下====");
		System.out.println(fileList.length);
		for (i = 0; i < fileList.length; i++) {
			System.out.println(fileList[i]);

			File file2 = new File("C:\\Users\\user\\Desktop\\log\\" + fileList[i]);
			BufferedReader br = new BufferedReader(new FileReader(file2));
			FileOutputStream fos = new FileOutputStream(
					"C:\\Users\\user\\Desktop\\lo\\"+fileList[i]);
			PrintStream ps = new PrintStream(fos);
			String s = null;

			while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行

				boolean a = s.contains("luyou");
				if (a) {
					System.out.println(s);
					ps.println(s);
				}

			}
			br.close();
			ps.close();

		}
		System.out.println(fileList.length);
	}

}
网络参考

http://blog.csdn.net/hantangsongming/article/details/19966833
http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
http://blog.csdn.net/jnhoodlum/article/details/7735421
http://bbs.csdn.net/topics/370213517

批解压 批改名

public class jieya {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		int i;
		File file1 = new File("C:\\Users\\user\\Desktop\\log");

		String[] fileList = file1.list();
		System.out.println("====当前路径下所有文件和路径如下====");
		System.out.println(fileList.length);
		
		for (i = 0; i < fileList.length; i++) {
			System.out.println(fileList[i]);
			String tmp = getSuffixName(fileList[i]) + ".log";
			unzip("C:\\Users\\user\\Desktop\\log\\" + fileList[i], "C:\\Users\\user\\Desktop\\lo\\", tmp);
			
		}

	}

	
	static void createDir(String path) {
		File dir = new File(path);
		if (dir.exists() == false)
			dir.mkdir();
	}
	
	static String getSuffixName(String name) {
		return name.substring(0, name.lastIndexOf("."));
	}
	
	public static void unzip(String zipFilePath, String unzipDirectory, String filename)
			throws Exception {
		
		// 创建文件对象
		File file = new File(zipFilePath);
		// 创建zip文件对象
		ZipFile zipFile = new ZipFile(file);
		// 创建本zip文件解压目录
		File unzipFile = new File(unzipDirectory + "/" + getSuffixName(file.getName()));
		
		if (unzipFile.exists())
		unzipFile.delete();
		//unzipFile.mkdir();
		// 得到zip文件条目枚举对象
		Enumeration zipEnum = zipFile.getEntries();
		// 定义输入输出流对象
		InputStream input = null;
		OutputStream output = null;
		// 定义对象
		ZipEntry entry = null;
		// 循环读取条目
		while (zipEnum.hasMoreElements()) {
			// 得到当前条目
			entry = (ZipEntry) zipEnum.nextElement();
			String entryName = new String(entry.getName());
			// 用/分隔条目名称
			String names[] = entryName.split("\\/");
			int length = names.length;
			String path = unzipFile.getAbsolutePath();
			for (int v = 0; v < length; v++) {
				if (v < length - 1) { // 最后一个目录之前的目录
					path += "/" + names[v] + "/";
					createDir(path);
				} else { // 最后一个
					if (entryName.endsWith("/")) // 为目录,则创建文件夹
						createDir(unzipFile.getAbsolutePath() + "/" + entryName);
						
					else { // 为文件,则输出到文件
						input = zipFile.getInputStream(entry);
						output = new FileOutputStream(new File(
								unzipDirectory + "/" + filename));
						byte[] buffer = new byte[1024 * 8];
						int readLen = 0;
						while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
							output.write(buffer, 0, readLen);
						// 关闭流
						input.close();
						output.flush();
						output.close();
					}
				}
			}
		}
	}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值