「java读配置」java读配置文件中文乱码
本篇文章给大家谈谈java读配置,以及java读配置文件中文乱码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、linux 下java读取配置文件
- 2、JAVA零配置读取配置文件
- 3、java读取配置文件的方法(xml)
- 4、java 多线程怎么读取一个表了的多个配置?
- 5、Java读取配置文件的几种方法以及路径问题
- 6、java 怎么读取配置文件
linux 下java读取配置文件
如果是相对路径无论windows还是linux都是一样的。
如果是绝对路径,只需要看好配置文件所在linux的目录,其他的操作和windows一样(例如:linux【/usr/local/src/1.config】,windows【c:/1.config】)
使用File就可以直接读取了。
JAVA零配置读取配置文件
package resources;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtil {
public static Connection getConnection(){
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//读取类路径下的 jdbc.properties 文件,我的配置文件放在src包下
InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("username");
password = properties.getProperty("password");
Driver driver = null;
try {
driver = (Driver) Class.forName(driverClass).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
//通过 Driver 的 connect 方法获取数据库连接.
Connection connection = null;
try {
connection = driver.connect(jdbcUrl, info);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----The database is connected----");
return connection;
}
}
jdbc.properties内容如下:
driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc\:oracle\:thin\:@10.91.4.102\:1521\:orcljdbcUrl=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=cp2
password=cp2test
//调用方法
public static void main(String[] args) {
String id = "111111";
String gread = "3";
ListStudent sList = new ArrayListStudent();
Connection con = DbUtilCR.getConnection();
PreparedStatement pre = null;
ResultSet result = null;
String sql = "select s.name,s.age from student s where s.id=? and s.gread=?";
try {
pre = con.prepareStatement(sql);
pre.setString(1, id);//传参数学号
pre.setString(2, gread);//传参数年级
result = pre.executeQuery();
System.out.println("执行SQL为:["+sql+"]");
System.out.println("参数为:["+id+","+gread+"]");
while (result.next()){
Student st = new Student();
st.setName(result.getString("name"));//与查询出的字段或者别名保持一致
st.setAge(result.getString("age"));
sList.add(st);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0;isList.size();i++) {
System.out.println("姓名:"+sList.get(i).getName()+"\t年龄:"+sList.get(i).getAge());
}
}
java读取配置文件的方法(xml)
#include XMLDoc.hpp
——相关说明
_di_IXMLDocument 为模板类
typedef System::DelphiInterface IXMLDocument _di_IXMLDocument;
_di_IXMLNode
typedef System::DelphiInterface IXMLNode _di_IXMLNode;
_di_IXMLNodeList 同
——类方法
//设置参数
void TXXX::setOptions(String name,String value){
//创建文档对象
_di_IXMLDocument XMLDoc = LoadXMLDocument(L"文件路径");
XMLDoc-Active=true;
//文档根节点
_di_IXMLNode root = XMLDoc-DocumentElement;
//想要查找节点
_di_IXMLNode tempNode;
//调用搜索方法
searchXml(name,root,tempNode);
// 处理
if(tempNode!=NULL)
tempNode-SetText(value);
XMLDoc-SaveToFile(L"文件路径");
}
//递归搜索参数节点树
void TXXX::searchXml(String name,_di_IXMLNode Parent_Node,_di_IXMLNode tempNode){
_di_IXMLNode Child_Node; //子结点
//子节点列表
_di_IXMLNodeList list = Parent_Node-ChildNodes;
for(int i=0;ilist-Count;i++)
{
Child_Node = list-Get(i);
//递归结束条件
if(Child_Node-GetNodeName()==name)
{
tempNode = Child_Node;
break;
}
else
{
//递归函数
searchXml(name,Child_Node,tempNode);
}
}
}
java 多线程怎么读取一个表了的多个配置?
没看懂,个人觉得你的一条记录就是一个配置信息,对应一个配置对象
数据库就是存储记录的地方, 每次从数据库取自然就会取到最新的所有记录,也就是所有的配置项。
你的配置是纵向增加的,1个配置、2个配置、...、n个配置,没什么问题,数据库增加一行就行
配置是横向增加的,那你的配置对象就要变化了
而且这个跟线程池也没什么关系吧,或者你只是想来实现一个定时任务不断刷新,springboot注入个@Schedule也行
Java读取配置文件的几种方法以及路径问题
.类加载器读取:
只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。
①获取类加载器 ClassLoader cl = 类名.class.getClassLoader();
②调用类加载器对象的方法:public URL getResource(String name);
此方法查找具有给定名称的资源,资源的搜索路径是虚拟机的内置类加载器的路径。
类 URL 代表一个统一资源定位符,它是指向互联网”资源”的指针。
资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用.
URL对象方法:public String getPath(),获取此 URL 的路径部分。
示例代码:
2.类加载器读取:
只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。
①获取类加载器 ClassLoader cl = 类名.class.getClassLoader();
②调用类加载器对象的方法:public InputStream getResourceAsStream(String name);
返回读取指定资源的输入流。资源的搜索路径是虚拟机的内置类加载器的路径。
java 怎么读取配置文件
一.读取xml配置文件
(一)新建一个java bean(HelloBean. java)
java代码
(二)构造一个配置文件(beanConfig.xml)
xml 代码
(三)读取xml文件
1.利用ClassPathXmlApplicationContext
java代码
2.利用FileSystemResource读取
java代码
二.读取properties配置文件
这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean. java文件,构造如下beanConfig.properties文件:
properties 代码
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
java代码
(二)利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
properties 代码
ip=192.168.0.1
port=8080
三.读取位于Jar包之外的properties配置文件
下面仅仅是列出读取文件的过程,剩下的解析成为properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");
2 num = reader.read(byteStream);
3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);
四.要读取的配置文件和类文件一起打包到一个Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //写配置文件
。。。
out.close();
java读配置的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读配置文件中文乱码、java读配置的信息别忘了在本站进行查找喔。
发布于:2022-12-13,除非注明,否则均为
原创文章,转载请注明出处。