java中如何调用配置文件,java类读取配置文件

本文目录一览:

java怎么调用properties配置文件的值

projectPath=11

aa= 11

public class ConfigTool {

public static String projectPath = “”;

public static HashMapString,String hashMap = new HashMap();

static {

      Properties property = new Properties();

      InputStream in = ConfigTool.class.getResourceAsStream(“/config.properties”);

   

      try {

          property.load(in);

          projectPath = property.getProperty(“projectPath”).trim();

          hashMap.put(“huifu”, property.getProperty(“aa”));

      } catch (Exception e) {

      e.printStackTrace();

      }

      finally {

          if (null != in) {

              try {

                  in.close();

              } catch (Exception e) {}

          }

      }

  }

}

java怎样提取配置文件!怎么才能采用ServletContext读取

创建配置文件:

1、在项目的任意地方,右键-》New-》File-》FileName-》输入-》名称.properties(比如:config.properties)

2、访问路径:从根目录开始出发(WebRoot)-WEB-INF-classes-config.properties,(如果有包名,在classes-包名-config.properties)(路径可以直接从本地中项目的路径,找到WEB-INF直接从地址中copy(比如我的本地磁盘保存是这样的:F:\课程\s2课程\s2书上内容\Java Web\ServletTest\WebRoot\WEB-INF\classes\config.properties))

response.setContentType(“text/html”);

response.setCharacterEncoding(“utf-8”);

request.setCharacterEncoding(“utf-8”);

PrintWriter out = response.getWriter();

/************************使用servletContext.getResourceAsStream**************************************/

//实例化ServletContext

ServletContext servletContext=this.getServletContext();

// //获取输入流

// InputStream in=servletContext.getResourceAsStream(“\\WEB-INF\\classes\\config.properties”);

// Properties p=new Properties();

// //类的装载

// p.load(in);

// //拿到配置文件中userName参数

// out.println(p.getProperty(“userName”));

/***************************普通的获取配置文件**************************************/

String path= servletContext.getRealPath((“\\WEB-INF\\classes\\config.properties”));//拿到绝对路径

FileInputStream in=new FileInputStream(path);

Properties p=new Properties();

p.load(in);

out.println(p.get(“userName”));

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代码中怎么读取配置文件

需要使用的是jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。

可以读取的配置文件:xml和properties

Java中spring读取配置文件的几种方法

Java中spring读取配置文件的几种方法如下:

一、读取xml配置文件

(一)新建一个java bean

package chb.demo.vo;

public class HelloBean {

private String helloWorld;

public String getHelloWorld() {

return helloWorld;

}

public void setHelloWorld(String helloWorld) {

this.helloWorld = helloWorld;

}

}

(二)构造一个配置文件

?xml version=”1.0″ encoding=”UTF-8″?

!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “”

beans

bean id=”helloBean” class=”chb.demo.vo.HelloBean”

property name=”helloWorld”

valueHello!chb!/value

/property

/bean

/beans

(三)读取xml文件

1.利用ClassPathXmlApplicationContext

ApplicationContext context = new ClassPathXmlApplicationContext(“beanConfig.xml”);

//这种用法不够灵活,不建议使用。

HelloBean helloBean = (HelloBean)context.getBean(“helloBean”);

System.out.println(helloBean.getHelloWorld());

2.利用FileSystemResource读取

Resource rs = new FileSystemResource(“D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml”);

BeanFactory factory = new XmlBeanFactory(rs);

HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);

System.out.println(helloBean.getHelloWorld());

值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。

二、读取properties配置文件

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取

(一)利用spring读取properties 文件

我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:

helloBean.class=chb.demo.vo.HelloBean

helloBean.helloWorld=Hello!chb!

属性文件中的”helloBean”名称即是Bean的别名设定,.class用于指定类来源。

然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();

PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);

reader.loadBeanDefinitions(new ClassPathResource(“beanConfig.properties”));

BeanFactory factory = (BeanFactory)reg;

HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);

System.out.println(helloBean.getHelloWorld());

(二)利用java.util.Properties读取属性文件

比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:

ip=192.168.0.1

port=8080

则,我们可以用如下程序来获得服务器配置信息:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(“ipConfig.properties”);

Properties p = new Properties();

try {

p.load(inputStream);

} catch (IOException e1) {

e1.printStackTrace();

}

System.out.println(“ip:”+p.getProperty(“ip”)+”,port:”+p.getProperty(“port”));

三 、用接口类WebApplicationContext来取。

private WebApplicationContext wac;

wac =WebApplicationContextUtils.getRequiredWebApplicationContext(

this.getServletContext());

wac = WebApplicationContextUtils.getWebApplicationContext(

this.getServletContext());

JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean(“jdbcTemplate”);

其中,jdbcTemplate为spring配置文件中的一个bean的id值。

这种用法比较灵活,spring配置文件在web中配置启动后,该类会自动去找对应的bean,而不用再去指定配置文件的具体位置。

java 程序调用c的dll怎么配置文件

JAVA通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOWS平台上是DLL文件形式,在UNIX机器上是SO文件形式)。通过调用本地的库文件的内部方法,使JAVA可以实现和本地机器的紧密联系,调用系统级的各接口方法。

简单介绍及应用如下:

一、JAVA中所需要做的工作

在JAVA程序中,首先需要在类中声明所调用的库名称,如下:

static {

System.loadLibrary(“goodluck”);

}

在这里,库的扩展名字可以不用写出来,究竟是DLL还是SO,由系统自己判断。

还需要对将要调用的方法做本地声明,关键字为native。并且只需要声明,而不需要具 体实现。如下:

public native static void set(int i);

public native static int get();

然后编译该JAVA程序文件,生成CLASS,再用JAVAH命令,JNI就会生成C/C++的头文件。

例如程序testdll.java,内容为:

public class testdll

{

static

{

System.loadLibrary(“goodluck”);

}

public native static int get();

public native static void set(int i);

public static void main(String[] args)

{

testdll test = new testdll();

test.set(10);

System.out.println(test.get());

}

}

用javac testdll.java编译它,会生成testdll.class。

再用javah testdll,则会在当前目录下生成testdll.h文件,这个文件需要被C/C++程序调用来生成所需的库文件。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/204626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-07 12:17
下一篇 2024-12-07 12:17

相关推荐

  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • VSCode为什么无法运行Java

    解答:VSCode无法运行Java是因为默认情况下,VSCode并没有集成Java运行环境,需要手动添加Java运行环境或安装相关插件才能实现Java代码的编写、调试和运行。 一、…

    编程 2025-04-29
  • Java任务下发回滚系统的设计与实现

    本文将介绍一个Java任务下发回滚系统的设计与实现。该系统可以用于执行复杂的任务,包括可回滚的任务,及时恢复任务失败前的状态。系统使用Java语言进行开发,可以支持多种类型的任务。…

    编程 2025-04-29
  • Java 8 Group By 会影响排序吗?

    是的,Java 8中的Group By会对排序产生影响。本文将从多个方面探讨Group By对排序的影响。 一、Group By的概述 Group By是SQL中的一种常见操作,它…

    编程 2025-04-29

发表回复

登录后才能评论