js事件在Java生生成,Java生成Js文件-封裝類

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import com.gzdec.common.config.AppConfig;

import com.gzdec.common.web.exception.DefaultException;

public class WriteJS {

static File cgiPath = null;

static String root = null;

static {

try {

root = AppConfig.getProperty("rootPath")

+ AppConfig.getProperty("cgiFilePath");

cgiPath = createDir(AppConfig.getProperty("rootPath"), AppConfig

.getProperty("cgiFilePath"));

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 根據傳過來的路徑,文件名稱,文件內容生成JS文件

*

* @param path

* @param name

* @param html

* @throws IOException

* @throws DefaultException

*/

public static void writeJS(String path, String name, String html)

throws IOException, DefaultException {

/* 創建目錄 */

File filePath = createDir(root, path.trim());

/* 創建文件 */

File file = getFile(filePath, name.trim() + ".js");

/* 寫文件 */

writeFile(file, html);

}

public static void writeXML(String path, String fileName, String xml)

throws IOException, DefaultException {

/* 創建目錄 */

File filePath = createDir(root, path.trim());

/* 創建文件 */

File file = getFile(filePath, fileName.trim() + ".xml");

/* 寫文件 */

writeFileXML(file, xml);

}

public static void writeHTML(String path, String fileName, String xml)

throws IOException, DefaultException {

/* 創建目錄 */

String htmlRoot = "";

try {

htmlRoot = AppConfig.getProperty("rootPath")

+ AppConfig.getProperty("cgiFilePath");

} catch (Exception e) {

e.printStackTrace();

}

File filePath = createDir(htmlRoot, path.trim());

/* 創建文件 */

File file = getFile(filePath, fileName.trim() + ".html");

/* 寫文件 */

writeFileXML(file, xml);

}

/**

* 創建目錄,目錄必須以"/"間隔

*

* @param rootPath

* @param path

* @throws IOException

* @throws DefaultException

*/

public static File createDir(String rootPath, String path)

throws IOException, DefaultException {

File file = new File(rootPath);

if (!file.exists()) {

throw new DefaultException("根目錄不存在");

}

String dirs[] = path.split("/");

for (int i = 0; i < dirs.length; i++) {

if (dirs[i] != null && !"".equals(dirs[i].trim())) {

file = getDir(file, dirs[i]);

}

}

return file;

}

/**

* 創建目錄

*

* @param parentPath

* @param dirName

* @return

* @throws IOException

*/

public static File getDir(File parentPath, String dirName)

throws IOException {

File dir = new File(parentPath, dirName);

if (!dir.exists()) {

dir.mkdir();

}

return dir;

}

/**

* 創建文件

*

* @param dirPath

* @param fileName

* @return

* @throws IOException

*/

public static File getFile(File dirPath, String fileName)

throws IOException {

File file = new File(dirPath, fileName);

if (!file.exists()) {

file.createNewFile();

}

return file;

}

/**

* 寫文件

*

* @param file

* @param html

* @throws IOException

* @throws DefaultException

*/

public static void writeFile(File file, String html) throws IOException,

DefaultException {

if (html != null && !"".equals(html.trim())) {

/* 參數有效性檢測 */

if (file == null || !file.isFile()) {

throw new DefaultException(

"public static void appendFile(File tar) parameters error!");

}

/* 判斷文件是否可寫 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()

+ " write prohibited! ");

}

String js = "var html =\"" + html + "\";document.write(html);";

/* 寫文件 */

/*

* FileWriter fw = new FileWriter(file); fw.write(js); fw.close();

*/

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "GBK");

out.write(js.toString());

out.flush();

out.close();

} else {

/* 參數有效性檢測 */

if (file == null || !file.isFile()) {

throw new DefaultException(

"public static void appendFile(File tar) parameters error!");

}

/* 判斷文件是否可寫 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()

+ " write prohibited! ");

}

String js = "var html =\"\";document.write(html);";

/* 寫文件 */

/*

* FileWriter fw = new FileWriter(file); fw.write(js); fw.close();

*/

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "GBK");

out.write(js.toString());

out.flush();

out.close();

}

}

/**

* 寫文件

*

* @param file

* @param html

* @throws IOException

* @throws DefaultException

*/

public static void writeFileXML(File file, String xml) throws IOException,

DefaultException {

if (xml != null && !"".equals(xml.trim())) {

/* 參數有效性檢測 */

if (file == null || !file.isFile()) {

throw new DefaultException(

"public static void appendFile(File tar) parameters error!");

}

/* 判斷文件是否可寫 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()

+ " write prohibited! ");

}

/* 寫文件 */

/*

* FileWriter fw = new FileWriter(file); fw.write(js); fw.close();

*/

// 根據文件名生成對應格式

/*   if (xml.contains("")) {

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "UTF-8");

out.write(xml);

out.flush();

out.close();

} else {

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "GBK");

out.write(xml);

out.flush();

out.close();

}*/

//本項目xml必須為UTF-8格式

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "GBK");

out.write(xml);

out.flush();

out.close();

} else {

/* 參數有效性檢測 */

if (file == null || !file.isFile()) {

throw new DefaultException(

"public static void appendFile(File tar) parameters error!");

}

/* 判斷文件是否可寫 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()

+ " write prohibited! ");

}

/* 寫文件 */

/*

* FileWriter fw = new FileWriter(file); fw.write(js); fw.close();

*/

OutputStreamWriter out = new OutputStreamWriter(

new FileOutputStream(file), "GBK");

out.write(xml);

out.flush();

out.close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值