以HTML爲模版,生成新的HTML

package com.yinli.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

public class GenerateHtml {
	@SuppressWarnings("static-access")
	public static void MakeHtml(String filepath, String text, String disrpath, String fileName) {

		String title = text;
		System.out.println(filepath);
		String templateContent = "";
		try {

			FileInputStream fileInputStream = new FileInputStream(filepath); // 讀取模版文件
			try {
				int length = fileInputStream.available();
				byte bytes[] = new byte[length];
				fileInputStream.read(bytes);
				fileInputStream.close();
				templateContent = new String(bytes, "utf-8");
				//這裏的“###text###”是需要替換的html中的字符,title是新的字符,如果要改多個不同的店就多次replaceAll就可以了
				templateContent = templateContent.replaceAll("###text###", title);

				String filename = fileName + ".html";
				String fileRealPath = disrpath + "/" + filename;
				FileOutputStream fileOutputStream = new FileOutputStream(fileRealPath);//向目標目錄下寫入文件
				byte tagbytes[] = templateContent.getBytes();
				fileOutputStream.write(tagbytes);
				fileOutputStream.close();

			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static ArrayList<String> getFiles(String path) {//驗證文件
		ArrayList<String> files = new ArrayList<String>();
		File file = new File(path);
		File[] tempList = file.listFiles();

		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].isFile()) {
				System.out.println("文件:" + tempList[i]);
				files.add(tempList[i].toString());
			}
			if (tempList[i].isDirectory()) {
				System.out.println("文件夾:" + tempList[i]);
			}
		}
		return files;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章