读写文件,文件内容操作
来源:互联网 发布:hadoop云计算实战 编辑:程序博客网 时间:2024/05/14 20:54
// 复制文件
public void copyFile(File sourceFile, File targetFile, String storeId) {
try {
try {
// 读文件 ,
String content = readFile(sourceFile);
// 替换index_layout.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
StringBuffer html = new StringBuffer();
if (sourceFile.getName().contains("layout")) {
content = replaceSiteUrl(content);
// 在index_layout.vm中添加bizId的隐藏域
html.append(content);
html.append(
"<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId).append("\">");
} else {
// 替换index.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
content = replaceSiteUrl(content);
// 在index.vm中添加bizId的隐藏域
html = new StringBuffer();
html.append(
content.substring(0, content.indexOf("</body>")))
.append("<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId)
.append("\">")
.append(content.substring(content
.indexOf("</body>")));
}
writeFile(targetFile, html.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
}
}
//读文件
public static String readFile(File file) throws Exception {
StringBuffer content = new StringBuffer();
FileInputStream fis = null;
InputStreamReader reader = null;
try {
fis = new FileInputStream(file);
reader = new InputStreamReader(fis, CODE);
// 将输入流写入输出流
char[] buffer = new char[2048];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
content.append(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
// log.error("读取文件【"+file.getPath()+"】出错", e);
throw new Exception("读取文件【" + file.getPath() + "】出错");
} finally {
try {
if (fis != null) {
fis.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
写文件
public static void writeFile(File file, String content) throws Exception {
BufferedWriter bufferedWriter = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
// log.error("写入文件【"+file.getPath()+"】出错", e);
throw new Exception("写入文件【" + file.getPath() + "】出错");
} finally {
try {
if (fos != null) {
fos.close();
}
if (osw != null) {
osw.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 替换vm中css和js引用的@xxx.xxx.xxx@为真实的域名
private String replaceSiteUrl(String content) {
// \\s 空白符号、\\S 非空白符号
String regexp = "@(.*)@";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String siteUrl = CustomizedPropertyPlaceholderConfigurer
.getInstance().getContextProperty(matcher.group(1));
content = content.replace(matcher.group(0), siteUrl);
}
return content;
}
// 复制文件
public void copyFile(File sourceFile, File targetFile, String storeId) {
try {
try {
// 读文件 ,
String content = readFile(sourceFile);
// 替换index_layout.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
StringBuffer html = new StringBuffer();
if (sourceFile.getName().contains("layout")) {
content = replaceSiteUrl(content);
// 在index_layout.vm中添加bizId的隐藏域
html.append(content);
html.append(
"<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId).append("\">");
} else {
// 替换index.vm中,css和js引用的@xxx.xxx.xxx@为真实的域名
content = replaceSiteUrl(content);
// 在index.vm中添加bizId的隐藏域
html = new StringBuffer();
html.append(
content.substring(0, content.indexOf("</body>")))
.append("<input type=\"hidden\" name=\"bizId\" id=\"bizId\" value=\"")
.append(storeId)
.append("\">")
.append(content.substring(content
.indexOf("</body>")));
}
writeFile(targetFile, html.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
}
}
//读文件
public static String readFile(File file) throws Exception {
StringBuffer content = new StringBuffer();
FileInputStream fis = null;
InputStreamReader reader = null;
try {
fis = new FileInputStream(file);
reader = new InputStreamReader(fis, CODE);
// 将输入流写入输出流
char[] buffer = new char[2048];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
content.append(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
// log.error("读取文件【"+file.getPath()+"】出错", e);
throw new Exception("读取文件【" + file.getPath() + "】出错");
} finally {
try {
if (fis != null) {
fis.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
写文件
public static void writeFile(File file, String content) throws Exception {
BufferedWriter bufferedWriter = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
// log.error("写入文件【"+file.getPath()+"】出错", e);
throw new Exception("写入文件【" + file.getPath() + "】出错");
} finally {
try {
if (fos != null) {
fos.close();
}
if (osw != null) {
osw.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 替换vm中css和js引用的@xxx.xxx.xxx@为真实的域名
private String replaceSiteUrl(String content) {
// \\s 空白符号、\\S 非空白符号
String regexp = "@(.*)@";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String siteUrl = CustomizedPropertyPlaceholderConfigurer
.getInstance().getContextProperty(matcher.group(1));
content = content.replace(matcher.group(0), siteUrl);
}
return content;
}
0 0
- 读写文件,文件内容操作
- Node.js读写中文内容文件操作
- Node.js读写中文内容文件操作
- Node.js读写中文内容文件操作
- Node.js读写中文内容文件操作
- Python文件操作,open读写文件,追加文本内容
- Python文件操作,open读写文件,追加文本内容
- 读写文件内容
- C文件操作、读写文件 C++文件读写、文件操作
- python 操作文件----文件读写
- Python文件操作读写文件
- Python文件操作读写文件
- Python文件操作读写文件
- Python文件操作读写文件
- Python文件操作读写文件
- Python文件操作读写文件
- Python文件操作读写文件
- Java文件操作---读写文件
- 谈谈对接芝麻信用踩的坑
- JAVA 常用设计模式
- ORACLE数据库日常维护知识总结
- 十大热门大数据技术
- CSS属性(2)
- 读写文件,文件内容操作
- python&DBA遍历文件夹下ip名称SQL 执行该IP下SQL
- C++中使用new最好要显示调用初始化函数
- 态代码 状态信息 含义100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分。(HTTP 1.1新)101 Switching
- JAVA中的关键字
- 玩转Netty – 从Netty3升级到Netty4
- uva 1638 Pole Arrangement
- echart的使用记录
- hibernate一级缓存详解