学者谷

位置:首页 > 行业范文 > 设计

JSP程序设计教程分享

设计3.18W

本文实例总结了JSP常见的文件操作。分享给大家供大家参考。具体如下:

JSP程序设计教程分享

JSP中的文件操作:FILE类

String path=ealPath("/");//传递参数"/"可以返回web应用根目录String tmp_path=path+"tmp";File f1=new File(tmp_path);//创建FILE类,指定路径为tmp_r();//创建目录File f2=new File(tmp_path,"");//创建FILE类,指定路径为//tmp_path+""teNewFile();//创建f2指定的`文件File f3=new File(tmp_path,"");teNewFile();File f4=new File(tmp_path,"");teNewFile();

其中:

File对象的length()方法可以计算文件的大小

isFile()方法可以判断是否为文件

isDirectory()方法可以判断是否为文件夹

getName()可以得到文件文件夹的名字

canRead()是否可读

canWrite()是否可写

isHidden()是否隐藏

lastModified()最后修改日期,返回Date类的一个对象

文件的读取

示例1:

String path=ealPath("/");File fp=new File(path,"");//定义一个文件FileInputStream fistream=new FileInputStream(fp);//定义一个文件输入流绑定一个文件byte buf[]=new byte[10000];int bytesum=(buf,0,10000)//把字节文件写入到buf数组中,返回写入的字节数String str_file=new String(buf,0,bytesum);tln(str_file);e();

示例2:

String path=ealPath("/");File fp=new File(path,"");FileReader freader=new FileReader(fp):BufferedReader bfdreader=new BufferedReader(freader);String str_line=Line();while(str_line!=null){ tln(str_line); tln("

"); str_line=Line(); } e(); e();

文件的写入:

示例1:

String path=ealPath("/");File fp=new File(path,"");FileWriter fwriter=new FileWriter(fp);haracterEncoding("GBK");//设置字符编码String str_file=arameter("textarea");e(str_file);e();

示例2:

String path=ealPath("/");File fp=new FIle(path,"");FileWriter fwriter=new FIleWriter(fp);BufferedWriter bfwriter=new BufferedWriter(fwriter);haracterEncoding("GBK");String str_file=arameter("textarea");e(str_file,0,str_th());h();e();

标签:jsp 程序设计