Java读取ini文件和中文乱码问题解决
来源:互联网 发布:中美网络安全问题 编辑:程序博客网 时间:2024/05/21 17:57
提出问题:
初用properties,读取java properties文件的时候如果value是中文,会出现读取乱码的问题 。
问题分析:
最初认为是文件保存编码问题,于是进行编码的统一:
1)把MyEclipse中所有的文件编码都修改成UTF-8,问题仍然存在;
2)把内容复制到EditPlus进行UTF-8编码转换,问题仍然存在;
3)上网搜索有人提议重写properties类或者用jdk自带的编码转换工具,非常麻烦而且不可取,Java的开发者可定会考虑到编码问题;最初的方法如下:
/*** 读取配置文件* @param filePath配置文件的路径*/public static Properties readFileByStream(String filePath){filePath = getRealPath(filePath);//获取文件的绝对路径/**定义变量*/FileInputStream fileInput = null;//读 Propertiesproper = new Properties(); try {fileInput = new FileInputStream(filePath);proper.load(fileInput); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return proper;}
这个方法用的是字节流来读取文件,中文会乱码;
因为字节流是无法读取中文的,所以采取把FileInputStream转换成InputStreamReader用字符流来读取中文。代码如下:
/*** 读取配置文件* @param filePath文件的路径*/public static Properties readFileByReader(String filePath){/**定义变量*/InputStream inputStream = ConfigFile.class.getClass().getResourceAsStream(filePath); Propertiesproper = new Properties(); try { BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); proper.load(bf); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return proper;}
不再乱码了
为了方便查看,贴出完整代码:public class ConfigFile {public static final String configFile = "/config/path.ini";//配置文件的路径/** * 获取文件中的某一项的值 * @param key * @return */public static String getKey(String key){Properties proper = readFileByReader(configFile);return proper.getProperty(key);}/** * 字节流读取配置文件 * @param filePath */public static Properties readFileByStream(String filePath){filePath = getRealPath(filePath);/**定义变量*/FileInputStream fileInput = null; //读 Properties proper; proper = new Properties(); try {fileInput = new FileInputStream(filePath);proper.load(fileInput); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return proper;}/** * 字符流读取配置文件 * @param filePath */public static Properties readFileByReader(String filePath){/**定义变量*/InputStream inputStream = ConfigFile.class.getClass().getResourceAsStream(filePath); Properties proper; proper = new Properties(); try { BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); proper.load(bf); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return proper;}/** 获取类根路径 */public static String getClassRoot(){return ConfigFile.class.getResource("/").getPath().substring(1);}/** 获取相对路径的真实的路径 */public static String getRealPath(String path){if(path.charAt(0) == '/'){path = getClassRoot() + path.substring(1);}else if(path.charAt(1) != ':'){path = getClassRoot() + path;}return path;}/** * 测试 */public static void main(String[] args) {System.out.println(getKey("welcome_message"));System.exit(0);}}
0 0
- Java读取ini文件和中文乱码问题解决
- Java读取properties文件中文乱码问题解决
- Java读取properties文件中文乱码问题解决
- Java读取properties文件中文乱码问题解决
- Java读取properties文件中文乱码问题解决
- Java读取properties文件中文乱码问题解决
- Java 读取ANSI文件中文乱码问题解决方式
- Qt中文|qt5解决读取ini文件中中文乱码
- 【Java IO知识】读取中文乱码问题解决
- java读取文件,中文乱码
- qt5解决读取ini文件中中文乱码
- 关于QT5读取ini文件中文乱码问题
- qt5 中读取ini文件 中文出现乱码
- java 拷贝文件和读取ini文件
- java文件读取写出乱码问题解决
- Java 读取写入文件乱码问题解决
- java生成xml文件中文乱码问题解决
- java读取ini文件
- Xcode更新之后提示App Transport Security has blocked a cleartext HTTP(http://)resource load since it is ...
- 用vlookup函数提取字符串中的数字
- 如何办理提前还贷
- hdu 5122(K.Bro Sorting, 简单思维题)
- 企业级证书打包下载安装之后无法使用问题
- Java读取ini文件和中文乱码问题解决
- Hibernate与 MyBatis的比较
- 【DOS批处理】函数定义和用法
- HQL 表连接
- Nosql简介
- bootstrap学习教程
- 我很笨
- iOS 如何面试
- 少年啊,来一发昆特吗(三)——昆特牌的选择界面