假设一个已经编译好了的class,但是其中用到的一个文件名需要换一下,难道你要重新修改source code、编译此class吗?利用属性文件properties可以避免不必要的麻烦,可编辑属性文件如下:
filename=(此处是一个文件的名字)
可以啦,让已经编译好了class读取此properties文件的filename的值吧,这样就不必修改source了。
:)
filename=(此处是一个文件的名字)
可以啦,让已经编译好了class读取此properties文件的filename的值吧,这样就不必修改source了。
:)
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class FileService extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; public static final String PARAMETER_SERVICE_TYPE = "FileService";
public static final String SERVICE_TYPE_EXPLORER = "Explorer";
public static final String SERVICE_TYPE_DOWNLOAD = "Download"; private static final String DEFAULT_CONF = "conf.properties";
private Properties props; //Initialize global variables
public void init() throws ServletException {
java.io.InputStream in = this.getClass().getResourceAsStream(DEFAULT_CONF);
props = new Properties();
try {
props.load(in);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
in = null;
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String serviceType = request.getParameter(PARAMETER_SERVICE_TYPE);
if (serviceType == null || serviceType.length() == 0) {}
else if (serviceType.equals(SERVICE_TYPE_EXPLORER))
processExplorerRequest(request, response);
else if (serviceType.equals(SERVICE_TYPE_DOWNLOAD))
processDownloadRequest(request, response);
}
。
在同一级目录下放:conf.properties 内容就是:c:\tomcat
我就可以读取到c:\tomcat目录下的所有文件!