81,094
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.io.*" %>
<%@ page import="sun.misc.BASE64Decoder"%>
<%@ page import="sun.misc.BASE64Encoder"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>使用JSP读取TXT格式文件</title>
</head>
<body>
<%
String path="C:\\"; //目录分隔符必须用双斜杠
File file=new File(path,"abc.txt");
FileReader fr=new FileReader(file); //字符输入流
BufferedReader br=new BufferedReader(fr); //使文件可按行读取并具有缓冲功能
StringBuffer strB=new StringBuffer(); //strB用来存储jsp.txt文件里的内容
String str=br.readLine();
while(str!=null){
//out.println(str);
strB.append(str).append("<br>"); //将读取的内容放入strB
str=br.readLine();
}
br.close(); //关闭输入流
fr.close();
%>
<%=strB %>
<%
BASE64Decoder decoder = new BASE64Decoder();
byte[] decoderBytes = decoder.decodeBuffer(strB.toString());
write.write(decoderBytes);
%>
</body>
</html>