读写文件,用代码在讲html文件转为jsp文件
package my.testguava; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.charset.Charset; import java.util.List; import com.google.common.io.ByteProcessor; import com.google.common.io.ByteStreams; import com.google.common.io.FileWriteMode; import com.google.common.io.Files; public class Mytestguava { public static void main2(String[] args) { String filepath = "/home/rocky/Develop/maven/study_maven/maven_workSpace/worksApproval/src/main/webapp/WEB-INF/page"; File files = new File(filepath); for(File f:files.listFiles()) { System.out.println(f.getName()); } } public List<String> getCommonsjsp(File f) throws FileNotFoundException, IOException { return Files.readLines(f,Charset.defaultCharset()); } public void getAllHtml(String commonjsp,String fileDir,String targetDir) throws IOException { List<String> commonFile = getCommonsjsp(new File(commonjsp)); File dir = new File(fileDir); File[] files = new File(fileDir).listFiles(); Files futil = null; List<String> result = null; RandomAccessFile randomFile = null; BufferedWriter bw = null; FileWriter fw = null; for(File f : files) { if(f.getName().endsWith(".html")||f.getName().endsWith(".HTML")) { result = Files.readLines(f, Charset.defaultCharset()); result.addAll(0, commonFile); fw = new FileWriter(new File(targetDir)+File.separator+f.getName().substring(0, f.getName().lastIndexOf(".html"))+".jsp"); bw = new BufferedWriter(fw); for(String line :result) { bw.write(line); bw.newLine(); } bw.flush(); fw.close(); bw.close(); } } } public static void main(String[] args) throws IOException { new Mytestguava().getAllHtml("/home/rocky/Develop/job/jianzhi/xieru/webpage/testpage/common.jsp", "/home/rocky/Develop/job/jianzhi/xieru/webpage/testpage/page", "/home/rocky/Develop/job/jianzhi/xieru/webpage/testpage/dirmy2"); System.out.println(Charset.defaultCharset()); } }
下面的是commonjsp的内容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <% String nameSpace = request.getContextPath(); %> <% String projectPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + nameSpace + "/"; %> <c:set value="<%=projectPath %>" var="basePath"/>
学好计算机,走遍天下都不怕