範文齋

位置:首頁 > 行業範文 > 設計

JSP程序設計教程分享

設計3.07W

本文實例總結了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 程序設計