读取文件信息

来源:互联网 发布:js中addeventlistener 编辑:程序博客网 时间:2024/05/21 17:51

package psic;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class ClientConf
{

    private String CONFIG_PATH;

    public ClientConf()
    {
        CONFIG_PATH = "";
        String Os_Name = System.getProperty("os.name").toLowerCase();
        String File_Sep = System.getProperty("file.separator");
        if(Os_Name.indexOf("win") != -1)
            CONFIG_PATH = "C:" + File_Sep + "Config" + File_Sep + "Client.conf";
        if(Os_Name.indexOf("linux") != -1 || Os_Name.indexOf("unix") != -1)
            CONFIG_PATH = File_Sep + "usr" + File_Sep + "local" + File_Sep + "Config" + File_Sep + "Client.conf";
    }

    public String getParamValue(String paramName)
    {
        byte buf[] = new byte[1024];
        String paramValue;
        try
        {
            ByteArrayOutputStream b_out = new ByteArrayOutputStream();
            FileInputStream fin = new FileInputStream(CONFIG_PATH);
            int i;
            while((i = fin.read(buf, 0, buf.length)) != -1)
            {
                b_out.write(buf, 0, i);
                b_out.flush();
            }
            String fileBuffer = b_out.toString().trim();
            b_out.close();
            fin.close();
            int index1 = fileBuffer.indexOf("<" + paramName + ">");
            int index2 = fileBuffer.indexOf("</" + paramName + ">");
            paramValue = fileBuffer.substring(index1 + (new String("<" + paramName + ">")).length(), index2);
        }
        catch(Exception exception)
        {
            return null;
        }
        return paramValue;
    }

原创粉丝点击