读文本文件常用方法,及在JSP中换行方法

本文介绍如何在Java中读取txt文件内容,并通过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[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]>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值