·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JSP文件操作:读取,写入和追加

JSP文件操作:读取,写入和追加

类别: JSP教程  评论数:0 总得分:0
jsp文件操作之读取篇

Read.jsp

<html>
<head>
<title>Read a file</title>
</head>
<body bgcolor="#000000">

<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
 <jsp:setProperty name="reader" property="path" value="/path/to/afile.txt" />
</jsp:useBean>

<h3>Contents of the file:</h3>

<p>

<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
 <% count++; %>   
 <b>Line <% out.print(count); %>:</b> <% out.print(reader.returnRecord()); %><br>    
<% } %> 
</p>
</body>
</html>



import java.io.*;
import java.util.StringTokenizer;

public class DelimitedDataFile
{
  /**
  * DelimitedDataFile.java
  * Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu
  *  April 6, 1999
  *
  * Variables:
  *  String currentRecord = the record the bean is currently working on
  *  BufferedReader file = the file the bean is working with
  *  String path = the path to the file (ie. /home/you/afile.txt)
  *  StringTokenizer token = the currentRecord tokenized
  *
  * Methods:
  *  public void setPath() - creates a BufferedReader that reads the file in path
  *  public String getPath() - returns path
  *  public void fileClose() - closes the file that is being read
  *  public int nextRecord() - reads the next record(line) in the file,
  *               and returns the number of tokens in the record
  *               or else returns -1 if there aren&acute;t anymore records
  *  public double returnDouble() - returns the next token as a double
  *  public int returnInt() - returns the next token as an int
  *  public String returnString() - returns the next token as a String
  *  public String returnRecord() - returns the entire record as a String
  */
 
     private String      currentRecord = null;
     private BufferedReader  file;
     private String      path;
     private StringTokenizer token;
 
     public DelimitedDataFile()
        {
            file = new BufferedReader(new InputStreamReader(System.in),1);
        } // constructor 1
     public DelimitedDataFile(String filePath) throws FileNotFoundException
        {
            // gets file
            path = filePath;
            file = new BufferedReader(new FileReader(path));
        } // constructor DelimitedDataFile
    
     public void setPath(String filePath)
        {
            // sets the file
            path = filePath;
            try {
               file = new BufferedReader(new
               FileReader(path));
            } catch (FileNotFoundException e) {
            System.out.println("file not found");
            }
    
        } // method setPath

        public String getPath() {
        return path;
     } // method getPath
 
     public void fileClose() throws IOException
        {
            // closes file
            file.close();
        } // method fileClose
 
     public int nextRecord()
        {
            // this method reads the next record and returns the number of
            // tokens or else returns -1
    
            int returnInt = -1;
            try
              {
                currentRecord = file.readLine();
              } // end try
    
            catch (IOException e)
              {
                System.out.println("readLine problem, terminating.");
              } // end catch
    
            if (currentRecord == null)
              returnInt = -1;
            else
              {
                 token = new StringTokenizer(currentRecord);
                 returnInt = token.countTokens();
              } // end else
            return returnInt;
        } // method nextRecord
 
     public double returnDouble()
        {
            // this method returns the next token as a double
            double doubleReturn = Double.valueOf(token.nextToken()).doubleValue();
            return doubleReturn;
        } // method returnDouble
 
     public int returnInt()
        {
            // this method returns the next token as an int
            int returnint = Integer.parseInt(token.nextToken());
            return returnint;
        } // method returnInt
 
     public String returnString()
        {
            // this method returns the next token as a String
            String stringReturn = token.nextToken();
            return stringReturn;
        } // method returnString
      
     public String returnRecord()
        {
            // this method returna the entire record as a string
            return currentRecord;
        } // method returnRecord
  } // class DelimitedDataFile

jsp文件操作之写入篇
WriteOver.Jsp

<html>
<head>
<title>Write over a file</title>
</head>
<body bgcolor="#000000">

<jsp:useBean id="writer" class="WriteOver" scope="request">
<jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" />
<jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteOver" />
</jsp:useBean>

<h3>Write to the file</h3>

<p>

<% writer.setSomething("Something to write to the file"); %>
<% out.print(writer.getSomething()); %>

<% out.print(writer.writeSomething()); %>

</p>
</body>
</html>


import java.io.*;

/**
* WriteOver.java
* Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu
*  August 19, 1999
*
* Variables:
*  String path = path to file to write to (ie. /you/afile.txt)
*  String something = something to write to the file
*
* Methods:
*  void setPath() = sets path
*  String getPath() = returns path
*  void setSomething() = sets something
*  String getSomething() = returns something
*  String writeSomething() = writes something to the path,
*   returns a message that indicates success or failure(an Exception)
*/

public class WriteOver {
 
  private String path;
  private String something;
 
  public WriteOver() {
   path = null;
   something = "Default message";
  } // constructor WriteOver
 
  /**
  * Mutator for the path property
  */
  public void setPath(String apath) {
   path = apath;
  } // mutator setPath
 
  /**
  * Accessor for the path property
  */
  public String getPath() {
   return path;
  } // accessor getPathClient
 
  /**
  * Mutator for the something property
  */
  public void setSomething(String asomething) {
   something = asomething;
  } // mutator setSomething
 
  /**
  * Accessor for the something property
  */
  public String getSomething() {
   return something;
  } // accessor getSomething
   
  /**
  * This method writes something to the path
  */
  public String writeSomething() {
   try {
    
     File f = new File(path);
     PrintWriter out = new PrintWriter(new FileWriter(f));
    
     out.print(this.getSomething() + " ");
     out.close();
     return "Alles ist Gut.";
   } catch (IOException e) {
     return e.toString();
   }    
  } // method writeSomething
} // class WriteOver


jsp文件操作之追加篇

如何用jsp将数据追加到一个文件中,下面是实现方法

writeAppend.jsp

<html>
<head>
<title>Append a file</title>
</head>
<body bgcolor="#000000">

<jsp:useBean id="writer" class="WriteAppend" scope="request">
 <jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" />
 <jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteAppend" />
</jsp:useBean>

<h3>Write to the file</h3>

<p>

<% writer.setSomething("Something to write to the file"); %>
<% out.print(writer.getSomething()); %>

<% out.print(writer.writeSomething()); %>

</p>
</body>
</html>

WriteAppend.java
import java.io.*;

/**
* WriteAppend.java
* Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu
*  August 19, 1999
*
* Variables:
*  String path = path to file to write to (ie. /you/afile.txt)
*  String something = something to write to the file
*
* Methods:
*  void setPath() = sets path
*  String getPath() = returns path
*  void setSomething() = sets something
*  String getSomething() = returns something
*  String writeSomething() = writes something to the path,
*   returns a message that indicates success or failure(an Exception)
*/

public class WriteAppend {
 
  private String path;
  private String something;
 
  public WriteAppend() {
   path = null;
   something = "Default message";
  } // constructor WriteAppend
 
  /**
  * Mutator for the path property
  */
  public void setPath(String apath) {
   path = apath;
  } // mutator setPath
 
  /**
  * Accessor for the path property
  */
  public String getPath() {
   return path;
  } // accessor getPathClient
 
  /**
  * Mutator for the something property
  */
  public void setSomething(String asomething) {
   something = asomething;
  } // mutator setSomething
 
  /**
  * Accessor for the something property
  */
  public String getSomething() {
   return something;
  } // accessor getSomething
   
  /**
  * This method writes something to the path
  */
  public String writeSomething() {
   try {
    
    FileWriter theFile = new FileWriter(path,true);
    PrintWriter out = new PrintWriter(theFile);
    out.print(something + " ");
    out.close();
    theFile.close();
    return "Das war sehr gut!";
   } catch (IOException e) {
     return e.toString();
   }    
  } // method writeSomething
} // class WriteAppend
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1