package action;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servletAction extends HttpServlet{
public static final String ACTIONPATH = "eclipse.txt";
public void service(HttpServletRequest request,HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
try {
// E:\apachetomcat7\webapps\a\
//该方法是获取项目部署的物理路径
String path = request.getRealPath("\\");
//根据获取的物理路径创建一个文件字节输入流对象
FileInputStream fis = new FileInputStream(path + ACTIONPATH);
//将字节输入流转换成字符输入流
InputStreamReader isr=new InputStreamReader(fis);
//创建一个字符缓冲输入流
BufferedReader br=new BufferedReader(isr);
//使用response响应对象获取一个输出流
PrintWriter out = response.getWriter();
//创建一个char类型数组,数组大小是50
char[] c=new char[100];
int num=0;
//将遍历读取到的字符存入到char类型数组中
while(br.read(c)!=-1){
//利用输出流将数组输出
out.println(c);
num++;
}
out.close();
br.close();
isr.close();
fis.close();
System.out.println("文件已经输出到页面,输出的次数:"+num+"次");
} catch (IOException e) {
e.printStackTrace();
}
}
}