科帮网-Java论坛、Java社区、JavaWeb毕业设计

登录/注册
您现在的位置:论坛 资料库 JAVA开发 > java读取ini文件
总共48086条微博

动态微博

查看: 1071|回复: 0

java读取ini文件

[复制链接]

10

主题

3

听众

1289

金钱

五袋长老

该用户从未签到

跳转到指定楼层
楼主
发表于 2016-03-17 00:13:11 |只看该作者 |倒序浏览
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>
* @author  zyy
* @version  [版本号, 2012-11-17]
* @see  [相关类/方法]
* @since  [产品/模块版本]
*/
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论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表