liudaoru 发表于 2013-2-7 16:46:53

文件管理器

前段时间实现的一个文件管理器:

package test.bwl;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;/** * 文件发布工具类 ** @authorbwl * @version1.0 */public class FilePublish {/** * 十进制到十六进制的转换 */public static final char[] DEC2HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e','f' };/** * 文件存储的根路径 */public static final String BASE_DIR = "F://temp//";/** * 是否是测试模式 */public static final boolean DEBUG = false;public static void main(String[] args) {try {FilePublish.getInstance().publishFile();} catch (IOException e) {e.printStackTrace();}}/** * 提供单例对象的静态内部类 */private static class SingletonHolder {public static FilePublish instance = new FilePublish();}/** * 获取对象实例 * @return */public static FilePublish getInstance() {return SingletonHolder.instance;}/** * 将html文件发布到文件系统 * @throws IOException */public void publishFile() throws IOException {System.out.println("Begin publish files...");//读取模板内容File module = new File(".//WebRoot/temple.htm");BufferedReader br = new BufferedReader(new FileReader(module));String line;StringBuffer sb = new StringBuffer();while ((line = br.readLine()) != null) {sb.append(line);}String template = sb.toString(); //模板数据//文件发布long begin = System.currentTimeMillis(); //记录上次的时间//long cur = 0L; //记录本次时间PrintWriter pw;int temp; //临时变量for (int i = 0; i < 1; i++) {temp = (int) (Math.random() * Integer.MAX_VALUE);File f = new File(BASE_DIR + id2dir(temp) + temp + ".html");if (!f.exists()) {if (!f.getParentFile().isFile()) {f.getParentFile().mkdirs();}f.createNewFile();}pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));//pw.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");//pw.write("<html><head>");//pw.write("<title>test</title>");//pw.write("</head><body>");String tempStr = template;tempStr = tempStr.replaceFirst("<<content>>","秋季,天气转凉,是易患感冒的时节。因患感冒到北京友谊医院就诊的患者有所增加。友谊医院呼吸科专家提醒,由于早晚温差大,感冒指数较高,大家要适当增加衣物,同时采取预防感冒的有效方法。 ");pw.write(tempStr);//pw.write("</body></html>");pw.flush();pw.close();}System.out.println(System.currentTimeMillis() - begin);}/** * 删除所有发布文件的方法 */public void deleteFiles() {doDelete(BASE_DIR);}/** * 执行刪除操作 */public void doDelete(String path) {File root = new File(path);if (!root.exists()) {return;}File[] children = root.listFiles();File temp;for (int i = 0; i < children.length; i++) {temp = children;if (temp.exists()) {if (temp.isDirectory()) {doDelete(temp.getAbsolutePath());}temp.delete();}}}/** * 根据id确定文件的路径 ** @param id * @return */public String id2dir(int id) {int depth = 3; //文件夹的层数,最多四层char[] path = new char;depth = depth > 4 ? 4 : (depth < 0 ? 1 : depth);id = id >>> ((4 - depth) * 8);for (int i = 0; i < depth; i++) {path[(depth - i) * 4 - 1] = '/';path[(depth - i) * 4 - 2] = '/';path[(depth - i) * 4 - 3] = DEC2HEX;id = id >>> 4;path[(depth - i) * 4 - 4] = DEC2HEX;id = id >>> 4;}return new String(path);}/** * 向模板填充数据 ** @param template * @param content * @return * @throws UnsupportedEncodingException*/public String fillTemplate(String template, String content) throws UnsupportedEncodingException {//long start = System.nanoTime();////填充内容//fillTemplate(template, "########################");//System.out.println(System.nanoTime() - start);//////start = System.nanoTime();//template.replaceAll("<<content>>", "########################");//System.out.println(System.nanoTime() - start);//原始字符串if (DEBUG) {System.out.println(template);}byte[] cc = template.getBytes(), dd = new byte, bb = new byte;byte first = ' ', second;int index = 0, bbIndex = 0, ddIndex = 0;StringBuffer sb = new StringBuffer();while (index < cc.length) {second = first;first = cc;dd = first;if (first != '<') {continue;}if (second == '<') {sb.append(new String(dd).substring(0, ddIndex - 2));ddIndex = 0;while (cc != '>') {bb = cc;}index += 2;String t = (new String(bb)).substring(0, bbIndex);if (t.equals("content")) {sb.append(content);}}}sb.append((new String(dd)).substring(0, ddIndex));if (DEBUG) {System.out.println(sb.toString());}return sb.toString();}} 
页: [1]
查看完整版本: 文件管理器