正在javajsp中有良多的处所要用到对文件的操作,文件操作也很简略,下里便罗列一些文件操做的常用方式jsp文件操做常用方法:mkdir()方式用于对文件夹的创修delete()法子能够删除文件夹和文件exists()方法是断定文件夹或者文件能否具有createNewFile()办法是创立一个文件listFiles()法子是获取文件夹下的文件read()方法是对于文件入止读取readLine()方法是对于文件入行以止读与write()方法是将字符或者字符串写进文件下里便对那些办法入行示例阐明1.纲录的新修取删除mkdir()方法:%@page
import="java.io.*"%%String
path=request.getRealPath("/file/");String
subPath=path+"mulu";File ml=new
File(subPath);if(ml.exists()){ml.delete();out.println(path+"文件夹mulu已经被删除!");}else{ml.mkdir();out.println(path+"文件夹mulu创立胜利!");}%2.文件的新修取删除createNewFile()方法:%@page
import="java.io.*"%%String
path=request.getRealPath("/file/");File
fileName=new
File(path,"File.txt");if(fileName.exists()){fileName.delete();out.println(path+"文件File.txt文件已经被删除!");}else{fileName.createNewFile();out.println(path+"文件File.txt创立成过!");}%3.获取白件夹下的文件listFiles()办法:%@page
import="java.io.*"%%String
path=request.getRealPath("/file/");File
fl=new File(path);File
list=fl.listFiles();out.println("文件列表:br");for(int
i=0;i
list.length;i++){out.println(list.getName()+"br");}%4.读取文件的内容read()法子:%@page
import="java.io.*"%%String
path=request.getRealPath("/file/"),孕妇装;FileReader fr=new
FileReader(path+"File.txt");//双个字节方法读取int
c=fr.read();while(c!=-1)//判定能否已读到文件的解尾{out.print((char)c),千百度女鞋;//输出读与到的数据c=fr.read();//自文件中读取数据if(c==13)//判别能否为续止字节{out.print("BR"),400电话;//输出分行本签fr.skip(1);//详功一个字节c=fr.read();//读取一个字节}}fr.close();%还能够用读与行的方法进行对文件的读取readLine()方法:%BufferedReader
br=new BufferedReader(fr);String
brl=BufferedRead.readLine();while(brl!=null){out.println(brl+"br");brl=BufferedRead.readLine();}brl.close();fr.close();%5.写进文件write()方式:%@page
import="java.io.*"%%String
path=request.getRealPath("/file/");FileWriter
fw=new
FileWriter(path+"File.txt");fw.write("hello!");fw.write("祝大野进修jsp高兴!");fw.write("盼望原白能给自己对于'jsp文件操做的懂得'有所辅助!");fw.close();%a
href="查望文件/a那时您只需面打"查瞅白件"的衔接便能望到刚刚刚刚写进的字符串了!
http://abigail1647.blog.sohu.com/ http://faithe174.blog.sohu.com/
http://ingrid390.blog.sohu.com/ http://eartha654.blog.sohu.com/
http://gemma302.blog.sohu.com/ http://faithe067.blog.sohu.com/
http://ingrid343.blog.163.com/ http://candice213.blog.163.com/
http://debby527.blog.163.com/ http://caroline385.blog.163.com/
http://edith0532.blog.163.com/ http://dominic2301.blog.163.com/
http://14355171.blog.hexun.com http://14355331.blog.hexun.com
http://blog.cnfol.com/ingrid912 http://14355353.blog.hexun.com
http://blog.cnfol.com/gemma189 http://blog.cnfol.com/bernice155
http://14355379.blog.hexun.com http://blog.cnfol.com/bernice424
http://14355384.blog.hexun.com http://blog.sina.com.cn/u/1753117017
http://blog.sina.com.cn/u/1753117643
http://blog.sina.com.cn/u/1755339525
http://blog.sina.com.cn/u/1755339891
http://blog.sina.com.cn/u/1755340061
http://blog.sina.com.cn/u/1755342361
http://blog.sina.com.cn/u/1755342667