java国际化配置文件如何使用UTF8

1.直接设置SpringMessage的编码

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

@AutoConfiguration
public class MessageSourceConfig {
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

2.使用XML格式的配置文件

import com.zyym.common.core.i18n.XmlMessageSource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
public class MessageSourceConfig {

    @Bean
    public MessageSource messageSource() {
        return new XmlMessageSource(new String[]{"messages"});
    }
}
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Properties;

public class XmlMessageSource extends AbstractMessageSource {

    private final Properties properties = new Properties();

    public XmlMessageSource(String[] basenames) {
        for (String basename : basenames) {
            loadProperties(basename);
        }
    }

    private void loadProperties(String basename) {
        try {
            Resource resource = new ClassPathResource(basename + ".xml");
            InputStream inputStream = resource.getInputStream();
            DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
            persister.loadFromXml(properties, inputStream);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load properties from XML file: " + basename, e);
        }
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String msg = properties.getProperty(code);
        if (msg != null) {
            return new MessageFormat(msg, locale);
        }
        return null;
    }

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        return properties.getProperty(code);
    }
}

messages_en_US.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="notice.title">notice.title</entry>
    <entry key="not.script">not.script</entry>
    <entry key="not.null">not.null</entry>
</properties>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以通过Properties类来读取properties配置文件。具体步骤如下: 1. 创建Properties对象 Properties prop = new Properties(); 2. 加载配置文件 prop.load(new FileInputStream("config.properties")); 3. 读取配置文件中的属性值 String value = prop.getProperty("key"); 其中,key为配置文件中的属性名,value为属性值。 需要注意的是,配置文件的路径需要根据实际情况进行修改,可以使用相对路径或绝对路径。另外,如果配置文件中存在中文字符,需要使用UTF-8编码格式进行读取。 ### 回答2: Java读取properties配置文件的方法很简单,可以借助Java中的Properties类实现。 首先,需要引用java.util.Properties类,通过load()方法读取配置文件中的数据,返回一个Properties对象。其中,配置文件可以是任何文件类型,但其结构必须按照.properties格式书写。在读取时,可以使用相对或绝对路径指定配置文件的位置。 读取配置文件的示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { FileInputStream in = new FileInputStream("config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 上述代码中,首先创建了一个Properties对象,并通过调用load()方法读取了配置文件config.properties中的数据。然后,通过调用getProperty()方法分别获取了配置文件中的url、user和password属性值,并将其输出到控制台中。 另外,也可以使用Class.getResourceAsStream()或ClassLoader.getResourceAsStream()方法读取配置文件。示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { InputStream in = ReadProperties.class.getResourceAsStream("/config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 参考上述示例代码,读取properties配置文件非常简单,只需要使用Java中的Properties类和相关方法即可。值得注意的是,在读取配置文件时,需要注意配置文件的位置及格式,以避免出现读取失败或读取错误的情况。 ### 回答3: properties 配置文件是一种常见的文本格式文件,其结构类似键值对。在 Java 中,读取 properties 配置文件非常方便,在 Java 的标准库中已经提供了相关的 API。本文将介绍 Java 读取 properties 配置文件的方法。 Java 提供了 java.util.Properties 类用于操作 properties 文件。该类实现了 Map 接口,可以通过 key-value 的方式存储和操作属性,在 Java 中读取 properties 文件通常需要进行两个步骤: 1.创建 Properties 对象,同时将 properties 文件的内容加载到 Properties 对象中。 2.通过 Properties 对象获取流中的配置值。 读取 Properties 文件,首先需要创建一个 Properties 对象,通常使用 Properties 类的 load 方法来加载 properties 文件,load 方法支持两种方法加载 properties 文件。其一是使用文件路径加载,其二是使用 InputStream 对象加载。 public void load(InputStream inStream) public void load(Reader reader) 对于第一种方式,看一下下面的代码: Properties properties = new Properties(); properties.load(new FileInputStream("/path/to/my.properties")); 对于第二种方式,将 InputStream 对象传递给 Properties 对象,调用 load 方法来读取内容: InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); Properties properties = new Properties(); properties.load(inputStream); 完成 Properties 文件的加载后,接下来就可以通过 getProperty() 方法获取到 key 对应的 value 内容了: String value = properties.getProperty("key"); 当然,Properties 对象还提供了很多其他的方法用于操作 properties 文件,例如,store() 方法用于存储 properties 文件, toString() 方法可用于将 Properties 对象转化为字符串等等。 总的来说,Java 读取 properties 配置文件非常简单,通过上述方法,可以轻松得到 properties 文件中的配置项,完成自己关于 properties 文件的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值