<%@ page contentType="text/html;charset=gb2312" import="java.io.*"%>
<html>
<head>
<title>显示文件页面</title></head>
<body>
<center>
<%
File fl=new File("C:\\Tomcat 5.5\\webapps\\ROOT\\JSP\\file1.txt");
byte by[]=new byte[(int)fl.length()]; int b;
if(fl.exists()){
try{
FileInputStream fis=new FileInputStream(fl);
while((b=fis.read(by,0,by.length))!=-1){
String s=new String(by,0,b); out.println(s);
}
fis.close();
}
catch (IOException e)
{
out.println("文件读取错误");
}
}
else{
out.println("文件不存在,请确认在进行读取");
}
%>
</center>
</body>
</html>
对这些标注的语句我理解的不是很透彻,也会用,请大家给我再讲一下,特别是" while((b=fis.read(by,0,by.length))!=-1){"
中的那个“0”书上说是数据存储的指定位置什么的,我不明白到底是什么意思,我改成"1”或其他数字就通不过了。唉,郁闷!劳驾你们了,小弟在此谢了!分不多了,只能给这么多了,不要在意呀,哈哈。
<html>
<head>
<title>显示文件页面</title></head>
<body>
<center>
<%
File fl=new File("C:\\Tomcat 5.5\\webapps\\ROOT\\JSP\\file1.txt");
byte by[]=new byte[(int)fl.length()]; int b;
if(fl.exists()){
try{
FileInputStream fis=new FileInputStream(fl);
while((b=fis.read(by,0,by.length))!=-1){
String s=new String(by,0,b); out.println(s);
}
fis.close();
}
catch (IOException e)
{
out.println("文件读取错误");
}
}
else{
out.println("文件不存在,请确认在进行读取");
}
%>
</center>
</body>
</html>
对这些标注的语句我理解的不是很透彻,也会用,请大家给我再讲一下,特别是" while((b=fis.read(by,0,by.length))!=-1){"
中的那个“0”书上说是数据存储的指定位置什么的,我不明白到底是什么意思,我改成"1”或其他数字就通不过了。唉,郁闷!劳驾你们了,小弟在此谢了!分不多了,只能给这么多了,不要在意呀,哈哈。
楼主【gjk11】截止到2008-06-27 16:24:55的历史汇总数据(不包括此帖):
发帖数:6 发帖分:180
结贴数:6 结贴分:180
未结数:0 未结分:0
结贴率:100.00% 结分率:100.00%
敬礼!
int off,
int len)
throws IOException从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。如果 len 不为 0,则在输入可用之前,该方法将阻塞;否则,不读取任何字节并返回 0。 覆盖:
类 InputStream 中的 read
参数:
b - 存储读取数据的缓冲区。
off - 目标数组 b 中的起始偏移量。(按你的代码,这个大于0的话会数组越界)
len - 读取的最大字节数。
返回:
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
抛出:
NullPointerException - 如果 b 为 null。
IndexOutOfBoundsException - 如果 off 为负、len 为负,或者 len 大于 b.length - off
IOException - 如果发生 I/O 错误。
另请参见:
InputStream.read()以上我也是查API的,请养成好的习惯