过去,很有意识积累了不少工具类。

今年,为进一步提高工作效率,积极学习了ApacheCommons Jodd Guava Hutool等众多开源项目的代码。

这几个月一直在抽空整理自己的 工具类库。

 

发现,自己的很多工具类,包括其他人的一些工具类,都没有必要再用了,因为其它开源框架,做得更好了。

更重要的是,人家写的,也比较符合自己的风格和习惯。

 

关注自己的业务场景,实用工具类时的 需求, 入参和出参,细节就交给这些靠谱工具类库了。

 

如果自己非要再写工具代码,那应该是 别人没有提供的,或者不太符合自己需要的,或者 比较符合自己特色  组织习惯 命名习惯的。

 

下面列几个过去的工具代码,今天将把他们全部删除。

今后,用别人封装好的。

自己的工具类库,应该 精简、与众不同。

重复造轮子没意思。

 

“专注业务,退出其它”这是今后的目标,暂时不细说了。

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 * 
 * 输入输出流工具类。
 * 
 * @author LeiWen 2013-3-16 上午9:52:08
 */
public class IOStreamUtils {
	/**
	 * 从输入流中读取文本内容。
	 * 
	 * @param inputStream
	 *            输入流
	 * @param encoding
	 *            输入流中内容的编码
	 * @param close
	 *            读取内容之后,是否关闭输入流。close为true表示关闭,false表示不关闭。
	 * @return 输入流中的文本内容
	 */
	public static String readText(InputStream inputStream, String encoding,
			boolean close) {
		String text = null;
		if (close) {
			text = readTextAndClose(inputStream, encoding);
		} else {
			text = readText(inputStream, encoding);
		}

		return text;
	}

	/**
	 * 从输入流中读取文本内容,读取内容之后,不关闭输入流。
	 * 
	 * @param inputStream
	 *            输入流
	 * @param encoding
	 *            输入流中内容的编码
	 * @return 输入流中的文本内容
	 */
	public static String readText(InputStream inputStream, String encoding) {
		InputStreamReader inputStreamReader = null;
		StringBuffer sb = new StringBuffer("");
		try {
			inputStreamReader = new InputStreamReader(inputStream, encoding);

			BufferedReader reader = new BufferedReader(inputStreamReader);

			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	/**
	 * 从输入流中读取文本内容,读取内容之后,关闭输入流。
	 * 
	 * @param inputStream
	 *            输入流
	 * @param encoding
	 *            输入流中内容的编码
	 * @return 输入流中的文本内容
	 */
	public static String readTextAndClose(InputStream inputStream,
			String encoding) {
		String text = null;

		text = readText(inputStream, encoding);
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}

	/**
	 * 向输出流中写文本内容。
	 * 
	 * @param outputStream
	 *            输出流
	 * @param content
	 *            文本内容
	 * @param close
	 *            读取内容之后,是否关闭输出流。close为true表示关闭,false表示不关闭。
	 */
	public static void writeText(OutputStream outputStream, String content,
			boolean close) {
		if (close) {
			writeTextAndClose(outputStream, content);
		} else {
			writeText(outputStream, content);
		}
	}

	/**
	 * 向输出流中写文本内容,不关闭输出流。
	 * 
	 * @param outputStream
	 *            输出流
	 * @param content
	 *            文本内容
	 */
	public static void writeText(OutputStream outputStream, String content) {
		OutputStreamWriter outputStreamWriter = null;
		outputStreamWriter = new OutputStreamWriter(outputStream);
		try {
			outputStreamWriter.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 向输出流中写文本内容,写完之后关闭输出流。
	 * 
	 * @param outputStream
	 *            输出流
	 * @param content
	 *            文本内容
	 * @param close
	 *            读取内容之后,是否关闭输出流。close为true表示关闭,false表示不关闭。
	 */
	public static void writeTextAndClose(OutputStream outputStream,
			String content) {
		writeText(outputStream, content);

		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.jiutianniao.common.kit.io.FileKit;

/**
 * 文件读写工具类。
 * 
 * @author LeiWen 2013-3-16 上午9:51:26
 */
public class FileUtils {

	public static String readTextFromFile(String fileName) {
		return readTextFromFile(fileName, "GBK");
	}

	public static String readTextFromFile(String fileName, String charset) {
	
		
		String content = null;
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(fileName);
			content = IOStreamUtils.readText(inputStream, charset, true);
		} catch (IOException e) {
			System.out.print("IOError" + e.getMessage() + "\n");

		}
	
		return content;
	}

	/**
	 * @deprecated 将文本内容写入到文本文件中
	 * 
	 * @param path
	 *            文件的路径
	 * @param content
	 *            文本内容
	 */
	public static void writeTextToFile22(String path, String content) {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(path);
			IOStreamUtils.writeText(outputStream, content, true);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 将文本内容写入到文本文件中
	 * 
	 * @param path
	 *            文件的路径
	 * @param content
	 *            文本内容
	 */
	public static void writeTextToFile(String path, String content) {
		File file = new File(path);

		try {
			FileWriter writer = new FileWriter(file);
			writer.write(content);
			// 如果没有调用flush(),也没有关闭输出流,不会将字符串写入文件中
			writer.close();// 关闭流

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String path = "C://FileTest.java";
		writeTextToFile(path, "fans");
		try {
			FileKit.writeString(path, "fans2");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static byte[] getBytesFromFile(File file) throws IOException {
		InputStream is = new FileInputStream(file);
		long length = file.length();
		if (length > Integer.MAX_VALUE) {
			// File is too large
		}
		byte[] bytes = new byte[(int) length];
	
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}
		if (offset < bytes.length) {
			throw new IOException("Could not completely read file "
					+ file.getName());
		}
		is.close();
		return bytes;
	}

	public static void deleteTmpFile(File file) {
		if (file != null) {
			file.delete();
			System.out.println("删除临时pdf文件:" + file.getAbsolutePath());
		}
	}

	public static String getRandomFileName() {
		Date date = new Date();
		String timestamp = date.getTime() + "";
		return timestamp;
	}

	/**
	 * 对流进行压缩
	 */
	public static byte[] compressBytes(ByteArrayOutputStream outStream)
			throws IOException {
		byte[] input = outStream.toByteArray();
		int cachesize = 1024;
		Deflater compresser = new Deflater();
		compresser.reset();
		compresser.setInput(input);
		compresser.finish();
		byte output[] = new byte[0];
		ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
		try {
			byte[] buf = new byte[cachesize];
			int got;
			while (!compresser.finished()) {
				got = compresser.deflate(buf);
				o.write(buf, 0, got);
			}
			output = o.toByteArray();
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return output;
	}

	public static File saveAS(byte[] bytes, String savePath) throws IOException {
		File file = new File(savePath);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(bytes);
		fos.close();
		return file;
	}

	public static byte[] getContent(String filePath) throws IOException {
		FileInputStream in = new FileInputStream(filePath);
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
		byte[] temp = new byte[1024];
		int size = 0;
		while ((size = in.read(temp)) != -1) {
			out.write(temp, 0, size);
		}
		in.close();
		byte[] bytes = out.toByteArray();
		return bytes;
	}

	public static boolean isLegalImage(String extention) {
		return ".jpg".equals(extention) || ".gif".equals(extention)
				|| ".png".equals(extention);
	}

	public static int pdfToSwf(String sourcePath, String filePath)
			throws IOException, InterruptedException {
		// 源文件不存在则返回
		File source = new File(sourcePath);
		if (!source.exists())
			return 0;
		// 调用pdf2swf命令进行转换
		// String command = "D:\\swftools\\pdf2swf.exe" + " -o \"" + destPath +
		// fileName
		// +"\"  <SPAN style='COLOR: #ff0000'>-s languagedir=D:\\xpdf\\xpdf-chinese-simplified</SPAN> -s flashversion=9 \""
		// + sourcePath + "\"";
		String command = "C:/Program Files/SWFTools/pdf2swf.exe" + " -o \""
				+ filePath + "\" -s flashversion=9 \"" + sourcePath + "\"";
		Process process = Runtime.getRuntime().exec(command); // 调用外部程序
		final InputStream is1 = process.getInputStream();
		new Thread(new Runnable() {
			public void run() {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						is1));
				try {
					while (br.readLine() != null)
						;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
		InputStream is2 = process.getErrorStream();
		BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
		StringBuilder buf = new StringBuilder(); // 保存输出结果流
		String line = null;
		while ((line = br2.readLine()) != null)
			buf.append(line); // 循环等待ffmpeg进程结束
		System.out.println("输出结果为:" + buf);
	
		// BufferedReader bufferedReader = new BufferedReader(new
		// InputStreamReader(pro.getInputStream()));
		while (br2.readLine() != null) {
			;
		}
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			throw e;
		}
		return process.exitValue();
	}
	
	 /**
     * 压缩文件
     * 
     * @param files
     *            需要压缩的文件,文件路径为绝对路径
     * @param zipFilePath
     *            目标压缩文件的绝对路径
     */
    public static void zipFile(List<String> files, String zipFilePath) {
        // 声明压缩流对象
        ZipOutputStream zipOut = null;
        try {
            zipOut = new ZipOutputStream(new FileOutputStream(new File(
                    zipFilePath)));
            for (String file : files) {
                File file2 = new File(file);
                // 设置ZipEntry对象
                zipOut.putNextEntry(new ZipEntry(file2.getName()));
               // zipOut.setComment("www.fansunion.cn");
                zipOut.write(FileUtils.getBytesFromFile(file2));

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}