读取文件:
try{
if (wxz!=null){
if(wxz.equals("C")) wxz = "c:\\";
if(wxz.equals("D")) wxz = "d:\\";
if(wxz.equals("E")) wxz = "e:\\";
if(wxz.equals("F")) wxz = "f:\\";
File dir=new File(wxz);
if (dir.getParent()!=null){
out.println("<img src='images/dfloder.gif'><a href='filelist.jsp?dir="+dir.getParent()+"'>..</a><br>");
}
String temp[]=dir.list();
for(int i=0;i<temp.length;i++){
File ft=new File(temp[i]);String wxz1=wxz+"\\"+ft;
//out.println("wxz1="+wxz1);
File ppp=new File(wxz1);
if (ppp.isDirectory()) {
out.println("<img src='images/dfloder.gif'><a href='filelist.jsp?dir="+wxz1+"'>"+ft+"</a><br>");
}else{
//out.println("<img src='images/dfile.gif'><a href='"+wxz1+ft+"'>"+ft+"</a><br>");
out.println("<img src='images/dfile.gif'><a href='readFile.jsp?path="+wxz1+"' target='_blank'>"+ft+"</a><br>");}}
}}//try
catch(Exception e)
{
out.println("没有找到文件!");
}

解决方案 »

  1.   

    因为jsp最终要编译成servlet类,所以对文件的操作和在一般的application中没什么区别,
    只是路径需要注意
      

  2.   


    要在c:\建立writefile.txt.追加数据到文件。WriteAppend.jsp
    <%@page contentType="text/html;charset=gb2312"%>                                   
    <html>                                                                             
    <head>                                                                             
    <title>追加数据</title>                                                            
    </head>                                                                            
    <body>                                                                             
    <%--创建javabean并设置属性 --%>                                                    
    <jsp:useBean id="writer" class="com.WriteAppend" scope="request">                  
    <jsp:setProperty name="writer" property="path" value="c:\\writefile.txt" />        
     <jsp:setProperty name="writer" property="something" value="初始化something属性" />
     </jsp:useBean>                                                                    
     <h3>追加数据</h3>                                                                 
     <p>                                                                               
     <%--设置要追加的字符串 --%>                                                       
     <% writer.setSomething("这是追加的内容"); %>                                      
     <%--读取上面设置的字符串 --%>                                                     
     <% out.print(writer.getSomething()); %>                                           
     <%--调用writer的writeSomething方法追加文件并返回成功或者出错信息 --%>             
     <% out.print(writer.writeSomething()); %>                                         
     </p>                                                                              
     </body>                                                                           
     </html>
      

  3.   

    3。删除文件<%@ page contentType="text/html;charset=gb2312"%>
    <%
    String filePath="c:/支出证明单.xls";
    filePath=filePath.toString();
    java.io.File myDelFile=new java.io.File(filePath);
    myDelFile.delete();
    %> 4。文件拷贝<%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <%
    int bytesum=0;
    int byteread=0; 
    file://读到流中
    InputStream inStream=new FileInputStream("c:/aaa.doc");
    FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[]  buffer =new  byte[1444];
    int length;
    while ((byteread=inStream.read(buffer))!=-1)
     {
       out.println("<DT><B>"+byteread+"</B></DT>");
       bytesum+=byteread;
       System.out.println(bytesum);
       fs.write(buffer,0,byteread);
     } 
    inStream.close(); 
    %>
      

  4.   

    1。新建目录<%@ page contentType="text/html;charset=gb2312"%>
    <%
    String filePath="c:/aaa/";
    filePath=filePath.toString();//中文转换
    java.io.File myFilePath=new java.io.File(filePath);
    if(!myFilePath.exists())
    myFilePath.mkdir();
    %>
      

  5.   

    http://www.csdn.net/develop/read_article.asp?id=19064
      

  6.   

    谢谢 渴望高飞的人
    我照你的程序,写好了CLASSES但是无论放到哪里,都无法被程序找到。总是说,类没有被找到。没办法我只有把代码写到jsp里,还好成功了。谢谢你的帮助。