|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
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´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 返回类别: 教程 上一教程: 提高 JAVA 代码的性能 下一教程: TOMCAT5.0+MYSQL配置JDBC,DBCP,SSL 您可以阅读与"JSP文件操作:读取,写入和追加"相关的教程: · JSP文件操作之读取篇 · JSP文件操作之追加篇 · JSP文件操作之写入篇 · JSP文件操作例程-读文件 · JSP文件操作 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |