
正文
java配置文件代码 java配置文件用什么格式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 怎么读取配置文件代码
方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。
因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
具体举例如下:
//ServletContext.getRealPath(name)读取路径
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throwsServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //读取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServlet
相关问答
Q1: 如何用java修改freeswitch的配置文件
要使用Java修改FreeSWITCH的配置文件,您可以使用Java IO类库中的FileWriter和BufferedWriter类来打开、读取和写入配置文件。以下是一个简单的示例代码:
java
Copy code
import java.io.*;
public class ModifyConfigFile {
public static void main(String[] args) {
try {
// 打开配置文件
File file = new File("/usr/local/freeswitch/conf/sip_profiles/external.xml");
BufferedReader reader = new BufferedReader(new FileReader(file));
// 读取配置文件
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
// 进行修改操作
if (line.contains("param name=\"ext-rtp-ip\"")) {
line = "param name=\"ext-rtp-ip\" value=\"192.168.1.100\"/";
}
stringBuilder.append(line).append("\n");
}
reader.close();
// 写入修改后的内容
FileWriter writer = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(stringBuilder.toString());
bufferedWriter.close();
System.out.println("配置文件修改成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上代码将打开"/usr/local/freeswitch/conf/sip_profiles/external.xml"文件,并查找包含字符串"param name="ext-rtp-ip""的行,并将其替换为"param name="ext-rtp-ip" value="192.168.1.100"/"。然后,它将修改后的内容写回到同一文件中。
请注意,在实际操作中,您需要根据您的实际需求修改代码中的文件路径和替换字符串,以确保代码能够正确地修改您想要修改的配置文件。此外,您还需要确保您的应用程序具有足够的权限来修改配置文件。
Q2: java配置文件怎么写?
参考java.util.Properties对象进行书写,另外可以在网上找一写辅助书写材料。
代码:
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id", "user1");
p.setProperty("password", "123456");
try{
PrintStream stm = new PrintStream(new File("e:\test.properties"));
p.list(stm);
} catch (IOException e) {
e.printStackTrace();
}
}
Q3: java配置文件怎么写
web.xml配置如下:
?xml version="1.0" encoding="UTF-8" ?
- web-app version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=" "
- filter
filter-nameMyFilter/filter-name
filter-classgame.filter.MyFilter/filter-class
/filter
- filter-mapping
filter-nameMyFilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
- welcome-file-list
welcome-fileindex.jsp/welcome-file
/welcome-file-list
/web-app
过滤器类如下:
package game.filter;
import java.io.IOException;
import javax.servlet.*;
public class MyFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
java配置文件代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java配置文件用什么格式、java配置文件代码的信息别忘了在本站进行查找喔。







