解决在读取properties文件中出现中文报错问题

// 读取properties中的参数
        Properties properties = new Properties();
        // 读取properties文件  使用InputStreamReader来解决中文报错问题
        InputStreamReader inputStreamReader = null;
//        InputStream inputStream = Main.class.getResourceAsStream("/file.properties");
        // 需要遍历的路径  --在properties文件中
        String path = "";
        try {
            inputStreamReader =  new InputStreamReader(Main2.class.getResourceAsStream("/file.properties"),"UTF-8");
            properties.load(inputStreamReader);
            // 读取到properties中的参数 赋值给path
            path = properties.getProperty("path");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 释放inputStream
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

  

原文地址:https://www.cnblogs.com/CanBlog/p/11370335.html

时间: 2024-08-06 05:56:59

解决在读取properties文件中出现中文报错问题的相关文章

Flex读取txt文件中的内容报错

Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePath+"/phone.txt"); 3.解决办法 将文件导入进去 Flex读取txt文件中的内容报错

spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换. 下面列举两种常见的方法. 方法一:在配置spring.xml文件时,声明所需的∗.properties文件时直接使用"utf−8"编码 <context:property-placeholder location="classpath:conf/*.properties

读取properties文件中的内容

看情况有时候某些属性值在很多地方要用,而且可能会有改动,就可以把它们存在properties文件中. 当然也可以准备一个静态类来放. //调用方法的静态代码块 private static String getStr = ""; static { try { Properties properties = PropertiesUtil.readProperties("sys.properties"); //Str是文件中配置的名字部分 //例:文件中存了几个属性,其

使用spring最简单地读取properties文件中的内容

相比传统的读取propertis文件内容,使用spring框架会更加简单快捷 1. 第一步,在spring的配置文件中,将propertis文件加载到spring容器 2. 加载了配置文件后,只需要在需要使用的地方,使用spring注入即可 原文地址:https://www.cnblogs.com/mayiaction/p/10869758.html

Flex读取txt文件里的内容报错

Flex读取txt文件里的内容 1.详细错误例如以下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePath+"/phone.txt"); 3.解决的方法 将文件导入进去

注解形式读取properties文件中的属性

1.spring.xml中加入(多个properties 用逗号隔开)  <context:property-placeholder location="classpath:jdbc.properties,classpath:config.properties" /> 2.spring-mvc.xml 中加入(只加上面一句controller中读取不到值) <context:property-placeholder location="classpath:c

Powershell 批量重命名文件中含有 [] 导致报错

下载的所有文件中都包含了 "[下载网站地址]", 按照常规方法 Get-ChildItem "D:\Bluey\" -Recurse |ForEach-Object{Rename-Item -Path $_.FullName -NewName $_.FullName.Replace('old','new')} 一直报告无法发现源文件,查阅后得知当文件名中包含特殊字符,需要使用 -LiteralPath 参数. Get-ChildItem "D:\Bluey

mac上运行php文件中的date()报错问题

学习PHP这门技术,本人新手一枚,踩到一坑,在此记录一下:  Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you a

time.strftime:格式化字符串中含中文报错处理

import time print(time.strftime("%Y年%m月%d日 %H时%M分%S秒",time.localtime())) 报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence 打印当前时间 import time print(time.localtime()) 运行结果: time.struct_tim