主頁 > 知識庫 > JSP常見的文件操作小結(jié)

JSP常見的文件操作小結(jié)

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 阿里云 團(tuán)購網(wǎng)站 銀行業(yè)務(wù) 服務(wù)器配置 Linux服務(wù)器 電子圍欄

本文實例總結(jié)了JSP常見的文件操作。分享給大家供大家參考。具體如下:

JSP中的文件操作:FILE類

String path=request.getRealPath("/");//傳遞參數(shù)"/"可以返回web應(yīng)用根目錄
String tmp_path=path+"tmp";
File f1=new File(tmp_path);//創(chuàng)建FILE類,指定路徑為tmp_path
f1.mkdir();//創(chuàng)建目錄
File f2=new File(tmp_path,"a.txt");//創(chuàng)建FILE類,指定路徑為//tmp_path+"a.txt"
f2.createNewFile();//創(chuàng)建f2指定的文件
File f3=new File(tmp_path,"b.txt");
f3.createNewFile();
File f4=new File(tmp_path,"b.txt");
f4.createNewFile();

其中:

File對象的length()方法可以計算文件的大小
isFile()方法可以判斷是否為文件
isDirectory()方法可以判斷是否為文件夾
getName()可以得到文件文件夾的名字
canRead()是否可讀
canWrite()是否可寫
isHidden()是否隱藏
lastModified()最后修改日期,返回Date類的一個對象

文件的讀取

示例1:

String path=request.getRealPath("/");
File fp=new File(path,"file1.txt");//定義一個文件
FileInputStream fistream=new FileInputStream(fp);//定義一個文件輸入流綁定一個文件
byte buf[]=new byte[10000];
int bytesum=fistream.read(buf,0,10000)//把字節(jié)文件寫入到buf數(shù)組中,返回寫入的字節(jié)數(shù)
String str_file=new String(buf,0,bytesum);
out.println(str_file);
fistream.close();

示例2:

String path=request.getRealPath("/");
File fp=new File(path,"file1.txt");
FileReader freader=new FileReader(fp):
BufferedReader bfdreader=new BufferedReader(freader);
String str_line=bfdreader.readLine();
while(str_line!=null){
  out.println(str_line);
  out.println("br>");
  str_line=bfdreader.readLine();
 }
 bfdreader.close();
 freader.close();

文件的寫入:

示例1:

String path=request.getRealPath("/");
File fp=new File(path,"file2.txt");
FileWriter fwriter=new FileWriter(fp);
request.setCharacterEncoding("GBK");//設(shè)置字符編碼
String str_file=request.getParameter("textarea");
fwriter.write(str_file);
fwriter.close();

示例2:

String path=request.getRealPath("/");
File fp=new FIle(path,"file2.txt");
FileWriter fwriter=new FIleWriter(fp);
BufferedWriter bfwriter=new BufferedWriter(fwriter);
request.setCharacterEncoding("GBK");
String str_file=request.getParameter("textarea");
bfwriter.write(str_file,0,str_file.length());
bfwriter.flush();
bfwriter.close();

希望本文所述對大家的JSP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • JSP實現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法
  • jsp中獲取當(dāng)前目錄的方法
  • JSP 從配置文件獲取參數(shù)詳解
  • JSP針對XML文件操作技巧實例分析
  • JSP導(dǎo)出Excel文件的方法
  • JSP實現(xiàn)快速上傳文件的方法
  • JSP文件下載功能的4種方法
  • jsp編程獲取當(dāng)前目錄下的文件和目錄及windows盤符的方法

標(biāo)簽:棗莊 廣元 衡水 蚌埠 衢州 大理 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP常見的文件操作小結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266