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. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

  2. 第八届极客大挑战 Web-故道白云&Clound的错误

    web-故道白云 题目: 解题思路: 0x01 首先看到题目说html里有秘密,就看了下源代码如图, 重点在红圈那里,表示输入的变量是id,当然上一行的method=“get”同时说明是get方式获取 ...

  3. 前端必学---JavaScript数据结构与算法---简介

    前端必学---JavaScript数据结构与算法---简介 1. 数据结构: 数据结构是相互之间存在一种或者多种特定关系的数据元素的集合.---<大话数据结构> 1.1 数据结构的分类 1 ...

  4. Django框架之ORM对表结构操作

    ORM的优点:(1)简单,不用自己写SQL语句 (2)开发效率高 ORM的缺点:对于不同的人写的代码,执行效率有差别 ORM的对应关系: 类  ---------->  数据表 对象------ ...

  5. 14. 深入解析Pod对象(一)

    14. 深入解析Pod对象(一) """ 通过前面的讲解,大家应该都知道: Pod,而不是容器,它是 Kubernetes 项目中的最小编排单位.将这个设计落实到 API ...

  6. Many Formulas

    You are given a string S consisting of digits between 1 and 9, inclusive. You can insert the letter ...

  7. cookie,session,localStorage和sessionStorage

    cookies:存储于浏览器端的数据.可以设置 cookies 的Max-Age或者Expires到期时间,如果不设置时间,则在浏览器关闭窗口的时候会消失. session:存储于服务器端的数据.se ...

  8. Babel的安装和使用

    安装Node.JS 和 npm,如未安装可参照其他文章 1.创建一个package.json npm init (回车, 一直下一步即可) 安装 Babel npm install --save-de ...

  9. 很重要的C++的位运算bitset

    本文摘录于柳神笔记: bitset ⽤来处理⼆进制位⾮常⽅便.头⽂件是 #include , bitset 可能在PAT.蓝桥OJ中不常 ⽤,但是在LeetCode OJ中经常⽤到-⽽且知道 bits ...

  10. Notepad++查看文本文件的总的字符数、GBK字节数、UTF8字节数

    如果其编码是 小结:UTF-8编码下,一个汉字占3字节,GBK编码下,一个汉字占2字节: