读文本文件常用方法,及在JSP中换行方法
JAVA中: package com.action;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;
import com.connSql.DAOException; import com.form.MonitorViewForm;
public class ReadTxtAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws DAOException {
MonitorViewForm lf=(MonitorViewForm)form;
System.out.println("ReadTxtAction");
String findForward="";
/**
* 读取txt数据
*/
String url = "D:/mbfe.log";
lf.setMefe(this.ReadDate(url));
findForward="sucess";
return mapping.findForward(findForward);
}
/**
* 读取txt数据
*/
public String ReadDate(String url) {
try {
FileReader read = new FileReader(new File(url));
StringBuffer sb = new StringBuffer();
char ch = new char;
int d = read.read(ch);
while(d!=-1){
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
}
System.out.print(sb.toString());
read.close();
return sb.toString().trim();
} catch (FileNotFoundException e) {
//e.printStackTrace();
return null;
} catch (IOException e) {
//e.printStackTrace();
return null;
}
}
}
JSP中: <pre><bean:write name="monitorViewForm" property="mefe" /></pre>