jsp文件操作之如何实现读取操作

发布时间:2021-11-22 11:46:15 作者:小新
来源:亿速云 阅读:129

这篇文章主要介绍jsp文件操作之如何实现读取操作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
  这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松读取文本文件,注意请放置一个文本文件afile.txt到web根目录的test目录下,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。
Read.jsp

<%--调用javabean>


文件内容:



<% int="" count="0;">
<% while="">
<%>
第<%>行:
<%>
    
<%>

undefined





//DelimitedDataFile.java bean文件源代码
//导入java包
import java.io.*;
import java.util.StringTokenizer;

public class DelimitedDataFile
{

private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public DelimitedDataFile()
{
     file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
    
     path = filePath;
     file = new BufferedReader(new FileReader(path));
}
     //设置文件路径
     public void setPath(String filePath)
        {
            
            path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
            System.out.println("file not found");
            }
    
        }
//得到文件路径
     public String getPath() {
        return path;
}
//关闭文件
public void fileClose() throws IOException
{
    
     file.close();
}
//读取下一行记录,若没有则返回-1
public int nextRecord()
{
    
    
     int returnInt = -1;
     try
     {
     currentRecord = file.readLine();
     }
    
     catch (IOException e)
     {
     System.out.println("readLine problem, terminating.");
     }
    
     if (currentRecord == null)
     returnInt = -1;
     else
     {
     token = new StringTokenizer(currentRecord);
     returnInt = token.countTokens();
     }
     return returnInt;
}

    //以字符串的形式返回整个记录
public String returnRecord()
{

return currentRecord;
}
}

以上是“jsp文件操作之如何实现读取操作”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. python的文件操作、读取、内容替换
  2. Python 之 文件操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jsp

上一篇:Python Web怎么学

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》