RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>JSP>列表

JSP中文件的建立、检查与删除!

来源: 作者: 时间:2007-10-19 点击:

%>
</body>
</html>
    <B>读取所有的文件数据</B>
<ccid_nobr>
<table width="400" border="1" cellspacing="0" cellpadding="2"
bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">
<tr>
<td bgcolor="e6e6e6" class="code" style="font-size:9pt">
<pre><ccid_code> <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,java.lang.*"%>
<html>
<head>
<title>读取所有的文件数据</title>
</head>
<body>
<%
    String path=request.getRealPath(".");
    FileReader fr=new FileReader(path + "\\ReadData.txt");
    //关键在于读取过程中,要判断所读取的字符是否已经到了文件的末尾,
    并且这个字符是不是文件中的断行符,即判断该字符值是否为13。
    int c=fr.read();//从文件中读取一个字符
    //判断是否已读到文件结尾
    while(c!=-1){
        out.print((char)c);//输出读到的数据
        c=fr.read();//从文件中继续读取数据
        if(c==13){//判断是否为断行字符
            out.print("<br>");//输出分行标签
            fr.skip(1);//略过一个字符
            //c=fr.read();//读取一个字符
        }
    }
    fr.close();
%>
</body>
</html>
  一行一行读取数据
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
    <title>文件读取</title>
</head>
<body>
<%
    String path=request.getRealPath("");//取得当前目录的路径
    FileReader fr=new FileReader(path + "
\\file\\inc\\t.txt");//建立FileReader对象,并实例化为fr
    BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象,并实例化为br
    String Line=br.readLine();//从文件读取一行字符串
    //判断读取到的字符串是否不为空
    while(Line!=null){
        out.println(Line + "<br>");//输出从文件中读取的数据
        Line=br.readLine();//从文件中继续读取一行数据
    }
    br.close();//关闭BufferedReader对象
    fr.close();//关闭文件
%>
</body>
</html>

 略过文件中的字符不读取
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
    <title>略过字节不读取</title>
</head>
<body>
<%
    String path=request.getRealPath(".");
    FileReader fr=new FileReader(path + "
\\ReadData.txt");
    fr.skip(2);//跳过2个字节
    int c=fr.read();//读取一个字节

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册