ini工具类;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; public class IniReader
{
// section item value
private static Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>(); // item value
private static HashMap<String, String> itemsMap = new HashMap<String, String>(); private static String currentSection = ""; /**
* Load data from target file
* @param file target file. It should be in ini format
*/
private static void loadData(File file)
{
BufferedReader reader = null;
try
{ reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null)
{
line = line.trim();
if ("".equals(line))
continue;
if (line.startsWith("[") && line.endsWith("]"))
{
// Ends last section
if (itemsMap.size() > 0 && !"".equals(currentSection.trim()))
{
sectionsMap.put(currentSection, itemsMap);
}
currentSection = "";
itemsMap = null; // Start new section initial
currentSection = line.substring(1, line.length() - 1);
itemsMap = new HashMap<String, String>();
}
else
{
int index = line.indexOf("=");
if (index != -1)
{
String key = line.substring(0, index);
String value = line.substring(index + 1, line.length());
itemsMap.put(key, value);
}
}
// System.out.println(line);
}
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
} public static String getValue(String section, String item, File file)
{
loadData(file); HashMap<String, String> map = sectionsMap.get(section);
if (map == null)
{
return "No such section:" + section;
}
String value = map.get(item);
if (value == null)
{
return "No such item:" + item;
}
return value;
} public static String getValue(String section, String item, String fileName)
{
File file = new File(fileName);
return getValue(section, item, file);
} public static List<String> getSectionNames(File file)
{
List<String> list = new ArrayList<String>();
loadData(file);
Set<String> key = sectionsMap.keySet();
for (Iterator<String> it = key.iterator(); it.hasNext();)
{
list.add(it.next());
}
return list;
} public static Map<String, String> getItemsBySectionName(String section, File file)
{
loadData(file);
return sectionsMap.get(section);
}
}

具体使用:

 * 文 件 名:  TestReadIni.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List; /**
* <读取ini文件中的内容>
* <把源文件从GBK转成UTF-8>
*/
public class TestReadIni
{
public static void main(String[] args)
{
String srcFileName = "D:\\shMap.ini";
String destFileName = "D:\\test.ini"; try
{
transferFile(srcFileName, destFileName);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} File file = new File(destFileName); List<String> se = IniReader.getSectionNames(file);
String distance = ""; // 离基站的距离
for (int i = 0; i < se.size(); i++)
{
distance = IniReader.getValue(se.get(i), "POSITION", file);
System.out.println("distance: " + distance);
}
} private static void transferFile(String srcFileName, String destFileName)
throws IOException
{
String line_separator = System.getProperty("line.separator");
FileInputStream fis = new FileInputStream(srcFileName);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));
String line = null;
while ((line = d.readLine()) != null)
content.append(line + line_separator);
d.close();
in.close();
fis.close(); Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), "utf-8");
ow.write(content.toString());
ow.close();
}
}
 
 
 
 

java读取ini文件的更多相关文章

  1. java 读取ini文件

    1.情景:需要将硬代码写到文件中,这样以后改动只需改动灵活 1)txt文件,需要将这code字符串读到代码中,保存成数组 2)导包:pom.xml添加依赖: <dependency> &l ...

  2. Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法

    转载:https://blog.csdn.net/clementad/article/details/47168573 2015-18-01修改:增加 apache commons io 处理方法. ...

  3. Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题

    在Windows下用文本编辑器创建的文本文件,如果选择以UTF-8等Unicode格式保存,会在文件头(第一个字符)加入一个BOM标识.   这个标识在Java读取文件的时候,不会被去掉,而且Stri ...

  4. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  5. C#读取ini文件的方法

    最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...

  6. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  7. Java读取txt文件

    package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...

  8. java 读取XML文件作为配置文件

    首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...

  9. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

随机推荐

  1. 工具 - 正则Cheat sheet

  2. WinForm开发(1)——DataGridView控件(1)——C# DataGridView控件用法介绍

    DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...

  3. webstrom激活码

    转自https://www.cnblogs.com/mahmud/p/11847825.html 812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNIIiwibGlj ...

  4. web优化(一 前端)

    当我们在浏览器地址栏中输入一个URL的时候,网页开始请求,我们在页面上看到的内容就是许多个HTTP请求从服务器返回的数据展示,这些展示的快慢很大程度依赖前端的优化,怎样做好前端的优化,我这里总结了几点 ...

  5. JMS消息传递的类型

    对于消息的传递有两种类型: 一种是点对点的,即一个生产者和一个消费者一一对应: 另一种是发布/ 订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进 行接收.

  6. office 格式定义

    在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...

  7. 「AHOI2014/JSOI2014」拼图

    「AHOI2014/JSOI2014」拼图 传送门 看到 \(n \times m \le 10^5\) ,考虑根号分治. 对于 \(n < m\) 的情况,我们可以枚举最终矩形的上下边界 \( ...

  8. php 高级 多台web服务器共享session的方法

    解决多台web服务器共享session的问题,至少有以下三种方法:   一.将本该保存在web服务器磁盘上的session数据保存到cookie中 即用cookie会话机制替代session会话机制, ...

  9. PHP5接口技术入门

    在PHP中我们声明类一般都用class来声明. <?php class Student{ //用class声明一个Student类 function __construct(){ //实例化类的 ...

  10. AttributeError: 'Word2Vec' object has no attribute 'vocab'

    在 Gensim 1.0.0 版本后移除了 vocab,需使用 model.wv.vocab