`
bubble
  • 浏览: 146180 次
  • 性别: Icon_minigender_1
  • 来自: 辽宁
社区版块
存档分类
最新评论

可恶的“查询吧”源码,我要阉了你!

阅读更多

nnd,一直想在自己的网站上加这个“查询吧”,不过里面的超链接都是指向根目录“/”,恶心,在网上没找到好的代码替换工具,今天心情好自己写一个吧。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReplaceUtil {

	private String[] dot;
	private File dir;

	private int di = 0;
	private int count = 1;

	public synchronized void done() {
		File[] fs = dir.listFiles();
		for (File f : fs) {
			if (f != null)
				if (f.isDirectory()) {
					dir = f;
					di = di + 1;
					done();
					di = di - 1;
				} else if (f.isFile()) {
					boolean isDot = false;
					String suf = f.getName().substring(
							f.getName().lastIndexOf("."));
					for (int i = 0; i < dot.length; i++) {
						if (suf.endsWith(dot[i]))
							isDot = true;
					}
					if (isDot) {
						try {
							System.out.println(count + "->操作文件[" + f.getPath()
									+ "]");
							replace(f, di);
							count++;
						} catch (IOException e) {
							System.out.println("操作文件[" + f.getPath() + "]时出错!");
							e.printStackTrace();
						}
					}
				}
		}
	}

	private void replace(File f, int di) throws IOException {
		StringBuffer sb = new StringBuffer();
		// read
		InputStream in = new FileInputStream(f);
		BufferedReader dr = new BufferedReader(new InputStreamReader(in));
		String line = dr.readLine();
		while (line != null) {
			// TODO:replace
			if (f.getName().endsWith("js")) {
				if (di == 0)
					line = line.replace("=\\\"\\/", "=\\\"");
				else if (di == 1)
					line = line.replace("=\\\"\\/", "=\\\"../");
				else if (di == 2)
					line = line.replace("=\\\"\\/", "=\\\"../../");
				else if (di == 3)
					line = line.replace("=\\\"\\/", "=\\\"../../../");
			} else if (f.getName().endsWith("css")) {
				if (di == 0)
					line = line.replaceAll("\\(/", "(");
				else if (di == 1)
					line = line.replaceAll("\\(/", "(../");
				else if (di == 2)
					line = line.replaceAll("\\(/", "(../../");
				else if (di == 3)
					line = line.replaceAll("\\(/", "(../../../");
			} else {
				if (di == 0)
					line = line.replaceAll("=\"/", "=\"");
				else if (di == 1)
					line = line.replaceAll("=\"/", "=\"../");
				else if (di == 2)
					line = line.replaceAll("=\"/", "=\"../../");
				else if (di == 3)
					line = line.replaceAll("=\"/", "=\"../../../");
			}
			sb.append(line + "\t\n");
			line = dr.readLine();
		}
		// write
		// String tfname=newFilePath(f.getAbsolutePath());//test
		BufferedWriter writer = new BufferedWriter(new FileWriter(f));
		writer.write(sb.toString());
		writer.flush();
	}

	/**
	 * use for testing
	 * @param path
	 * @return
	 */
	private String newFilePath(String path) {
		String sub = path.substring(0, path.lastIndexOf("."));
		String enb = path.substring(path.lastIndexOf("."));
		return sub + "_" + enb;
	}

	public String[] getDot() {
		return dot;
	}

	public void setDot(String[] dot) {
		this.dot = dot;
	}

	public File getDir() {
		return dir;
	}

	public void setDir(File dir) {
		this.dir = dir;
	}

	public static void main(String[] args) {
		String[] dot = { "htm", "html", "php", "js", "css" };
		File dir = new File("F:\\测试\\91tbnet");
		ReplaceUtil r = new ReplaceUtil();
		r.setDot(dot);
		r.setDir(dir);
		r.done();
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics