Java生成Js文件-封装类
2021/4/9 12:55:09
本文主要是介绍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("<pie>")) {
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();
}
}
}
这篇关于Java生成Js文件-封装类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25Java创意资料:新手入门的创意学习指南
- 2024-11-25JAVA对接阿里云智能语音服务资料详解:新手入门指南
- 2024-11-25Java对接阿里云智能语音服务资料详解
- 2024-11-25Java对接阿里云智能语音服务资料详解
- 2024-11-25JAVA副业资料:新手入门及初级提升指南
- 2024-11-25Java副业资料:入门到实践的全面指南
- 2024-11-25Springboot应用的多环境打包项目实战
- 2024-11-25SpringBoot应用的生产发布项目实战入门教程
- 2024-11-25Viite多环境配置项目实战:新手入门教程
- 2024-11-25Vite多环境配置项目实战入门教程