JSP程式設計教程分享

來源:果殼範文吧 3.05W

本文例項總結了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();

熱門標籤