Home >Database >Mysql Tutorial >jsp文件操作之写入篇_MySQL

jsp文件操作之写入篇_MySQL

WBOY
WBOYOriginal
2016-06-01 14:07:581009browse

   
    文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
  这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松写文本文件,注意请建立一个test目录到web根目录下,程序将会建立一个afile.txt文件,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。
  有了在jsp下读取和写入文件的方法,要做出一个简单的计数器来相信不是一件困难的事情了,大家可以尝试一下:)

WriteOver.Jsp



写一个文件








写一个文件















//WriteOver.java javabean文件
import java.io.*;


public class WriteOver {

private String path; //文件路径
private String something;//写入的字符串
//初始化
public WriteOver() {
path = null;
something = "缺省文字";
}

//设置文件路径
public void setPath(String apath) {
path = apath;
}

//得到文件路径
public String getPath() {
return path;
}
//得到字符串
public void setSomething(String asomething) {
something = asomething;
}
//设置字符串
public String getSomething() {
return something;
}
//写入字符串到文件中,成功则返回success字符串
public String writeSomething() {
try {
    
     File f = new File(path);
     PrintWriter out = new PrintWriter(new FileWriter(f));
     out.print(this.getSomething() + "
");
     out.close();
     return "Success.";
} catch (IOException e) {
     return e.toString();
}    
}
}
  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn