0
点赞
收藏
分享

微信扫一扫

ReadFileUtils

IT影子 2022-03-14 阅读 7


title: UtilsClass_ReadFileUtils

date: 2015-12-28 18:30:59

categories: UtilsClass_Resource

tags: Utils

xl_echo编辑整理

读取文件

读取方式一:使用类的加载器

public class ReadFileServlet extends HttpServlet {
public void ReadFile() {
InputStream is = ReadFileServlet.class.getClassLoader().getResourceAsStream("ReadFile.properties");
Properties prop = new Properties();
prop.load(is);

String driverClass = prop.getProperty("driverClass");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");

System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
}
}

读取方式二:使用this.getServletContext();

public class ReadFileServlet1 extends HttpServlet {
public void ReadFile() {
//获取全局域变量ServletContext
ServletContext context = this.getServletContext();

//使用全局变量获取输入流
InputStream is = context.getResourceAsStream("/WEB-INF/classes/ReadFile.properties");
Properties prop = new Properties();
prop.load(is);

String driverClass = prop.getProperty("driverClass");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");

System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
}
}

读取方式三:使用context.getRealPath();

public class ReadFileServlet2 extends HttpServlet {
public void ReadFile() {
//获得ServletContext全局域对象
ServletContext context = this.getServletContext();
//得到文件在磁盘的绝对路径
String realPath = context.getRealPath("/WEB-INF/classes/ReadFile.properties");

//使用字节输入流加绝对路径读取文件
InputStream is = new FileInputStream(realPath);

Properties prop = new Properties();
prop.load(is);

String driverClass = prop.getProperty("driverClass");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");

System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
}
}

使用全局变量来读取配置文件中的值

配置文件信息

<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123</param-value>
</context-param>
<context-param>
<param-name>111</param-name>
<param-value>222</param-value>
</context-param>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = this.getServletContext().getInitParameter("username");
String password = this.getServletContext().getInitParameter("password");
System.out.println(username + password);

//获得枚举类型
Enumeration<String> e = this.getServletContext().getInitParameterNames();

//遍历枚举对象
while(e.hasMoreElements()){
String name = e.nextElement();
String value = this.getServletContext().getInitParameter(name);
System.out.println(name + "\t" + value);
}
}

使用全局变量来读取文件在网络上的类型

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得文件MIME的类型(主要是判断文件在网络上的类型,用于上传和下载)
String type = this.getServletContext().getMimeType("aa.html");
System.out.println(type);
}



举报
0 条评论