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[1024];
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中:
<[color=red]pre[/color]><bean:write name="monitorViewForm" property="mefe" /></[color=red]pre[/color]>
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[1024];
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中:
<[color=red]pre[/color]><bean:write name="monitorViewForm" property="mefe" /></[color=red]pre[/color]>
本文介绍如何在Java中读取txt文件内容,并通过JSP进行展示。
1万+

被折叠的 条评论
为什么被折叠?



