---
title: "读文本文件常用方法，及在JSP中换行方法"
author: "psy_yd"
date: "2010-06-28T23:44:37Z"
tags: []
categories: ["编程语言"]
slug: "jsp"
draft: false
lastmod: "2010-06-28T23:44:37Z"
id: 701273
url: "https://psy-yd.iteye.com/blog/701273"
---

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中：
<<span style="color:red">pre</span>><bean:write  name="monitorViewForm" property="mefe" /></<span style="color:red">pre</span>>