如何用jsp打开一个名为ww.txt的文件,并在文件内写入“你好,欢迎来如jsp”内容,小弟初学,在现等

解决方案 »

  1.   

    java程序里怎么写jsp里就怎么写,完全一样的最好的方法是写个后台的读取文件的类,jsp里import一下直接用,
      

  2.   

    我给你写代码 你参考一下 别忘了鼓励哦!!
    if (  order.getContract_attached_file_URL() != null )
       order_attached_file_url   =    get_Url ;
    else
       order_attached_file_url   = "" ;

    if ( order_attached_file_url != null && ! order_attached_file_url.trim().equals( "" ) ) {
    int pos =  order_attached_file_url.lastIndexOf(".") ;
    if ( pos > -1  )
    tmp_ext  = order_attached_file_url.substring( pos )  ;
    else
    tmp_ext = ".txt" ;
    }

    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    Calendar now = Calendar.getInstance(); //get CurrDate
    tmp_fn = "Order" +  order_id + "_" +  formatter.format(now.getTime() ) ;
    tmp_fn =  tmp_fn  + tmp_ext ;
    tmpLink =  "/ecatic/temp/" + tmp_fn  ;
    tmpRealPathFile = request.getRealPath( tmpLink ) ;
    resultAttachFile = ordersheet.get_orderHeader().getOrderAttach( conn, tmpRealPathFile ,get_Url ,order_id) ; //In the server, file name is : tmpRealPathFile
    // open the temp file, and put data to out
    File file = new File( tmpRealPathFile  );
    response.setContentType("application/octet-stream");
    // response.setContentType("application/vnd.ms-excel") ; response.setHeader("Content-disposition" , "attachment; filename=" + file.getName() );
    // response.setHeader("content-disposition","attachment;filename=\""+ FileName +".xls\""); BufferedInputStream bis = null;
    BufferedOutputStream bos = null; bis = new BufferedInputStream(new FileInputStream(file));
    bos = new BufferedOutputStream(response.getOutputStream());
    bos.flush();
    byte[] buff = new byte[2048];
    int bytesRead;
    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff,0,bytesRead);
    }
    bos.close() ;
    bis.close() ; file.delete() ;
      

  3.   

    //给你一个现成的吧private static void write2File(String content, String fileName)
        {
         if (content == null || fileName == null || fileName.trim().length()==0)
         {
         throw new IllegalArgumentException("- [error]: 文件内容和文件名一个都不能少!");
         }
        
         BufferedReader reader = null;
         BufferedWriter writer = null;

    try
    {
    String filePath = "c:/" + fileName;
    System.out.println(filePath);
    File file = new File(filePath);

            if (!file.exists())
            {
             file.createNewFile();
            }
            
            reader = new BufferedReader(new StringReader(content));
            writer = new BufferedWriter(new FileWriter(file));
            String tempStr = null;
            while((tempStr=reader.readLine()) != null )
            {
             writer.write(tempStr);
             writer.flush();
             writer.newLine();
            }
         }
         catch(Exception e)
         {
         e.printStackTrace();
         }
         finally
         {
        
    try
    {
    if (reader != null)
         {
    reader.close();
         }

    if (writer != null)
         {
         writer.close();
         }
    }

    catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
        }
      

  4.   

    <%@ page language="java" pageEncoding="gb2312"%>
    <%@ page import ="java.io.*"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
    <title>文件操作</title>
    </head>
    <body>
    <h2>jsp 中对文件的操作,其实跟java 一样</h2>
    <br>
    <h4>你先建立一个文件,放在%TOMCAT_HOME%\webapps\ROOT文件夹下。比如<br>
    test.txt,输入如下内容:<br>
    <div align="center">测试一下读取文件操作</div>
    <br>
    <%
      String filename="test.txt";
      String tempPath=request.getRealPath("/");
      String file=tempPath+"\\"+filename;
      File testFile=new File(file);
      BufferedReader fin=new BufferedReader(new FileReader(testFile));
      String str=null;
      out.println("文件内容如下:<br>");
      
      while((str=fin.readLine())!=null)
      {
        out.println(str+"<br>");
       }
      out.flush();
      fin.close();  
    %>