我在网上找的读取ini文件的例子,但是每次执行都是null,请问ini文件里的内容该怎么写呢import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class IniReader
{
protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;
public IniReader(String filename) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}
protected void read(BufferedReader reader) throws IOException
{
String line;
while((line = reader.readLine()) != null)
{
parseLine(line);
}
}
protected void parseLine(String line)
{
line = line.trim();
if(line.matches("\\[.*\\]"))
{
if (current != null)
{
sections.put(currentSecion,current);
}
currentSecion = line.replaceFirst("\\[(.*)\\]","$1");
current = new Properties();
}
else if(line.matches(".*=.*"))
{
int i = line.indexOf('=');
String name = line.substring(0,i);
String value = line.substring(i+1);
current.setProperty(name,value);
}
}
public String getValue(String section,String name)
{
Properties p = (Properties) sections.get(section);
if (p == null)
{
return null;
}
String value = p.getProperty(name);
return value;
}
public static void main(String[] args) throws IOException
{
IniReader reader = new IniReader("c:\\File.ini");
System.out.println(reader.getValue("MCI Extensions.BAK","asf"));
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class IniReader
{
protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;
public IniReader(String filename) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}
protected void read(BufferedReader reader) throws IOException
{
String line;
while((line = reader.readLine()) != null)
{
parseLine(line);
}
}
protected void parseLine(String line)
{
line = line.trim();
if(line.matches("\\[.*\\]"))
{
if (current != null)
{
sections.put(currentSecion,current);
}
currentSecion = line.replaceFirst("\\[(.*)\\]","$1");
current = new Properties();
}
else if(line.matches(".*=.*"))
{
int i = line.indexOf('=');
String name = line.substring(0,i);
String value = line.substring(i+1);
current.setProperty(name,value);
}
}
public String getValue(String section,String name)
{
Properties p = (Properties) sections.get(section);
if (p == null)
{
return null;
}
String value = p.getProperty(name);
return value;
}
public static void main(String[] args) throws IOException
{
IniReader reader = new IniReader("c:\\File.ini");
System.out.println(reader.getValue("MCI Extensions.BAK","asf"));
}
}
解决方案 »
- 如何用正则表达式一次移除url中的多个参数
- 一个PreparedStatement的问题,进者有分~谢谢了
- 请问高手,JMenuItem中怎样设置一个键为快捷键啊,比如F2,delete,望指教!
- 目前最快的N皇后问题算法!!!
- 急问!JSlider的问题
- 关于String的问题!!!!!!!!!!!!!!****
- 创建可序化对象的问题
- applet 的半透明效果
- 用jdbc连接数据库的方法是什么;那位高手指点一下
- applet的更新问题,程序改动后,调用的还是原来的class,急!!!
- bufferedReader 读取服务器传来的数据时候 老是读重复 数据,为什么
- 用getResource方法返回的路径中多了一个!符号。
QihooHint=1[REG_TCP]
HOST=reg2t.sandai.net
PORT=5200[REG_UDP]
HOST=reg2u.sandai.net
PORT=6200
INTERVAL=300
DISUDPNUM=5
类似吧,哪行报的异常?
protected void parseLine(String line)
{
line = line.trim();
if(line.matches("\\[.*\\]"))
{
System.out.println("1");
currentSecion = line.replaceFirst("\\[(.*)\\]","$1");
current = new Properties();
if (current != null)
{
sections.put(currentSecion,current);
}
}
else if(line.matches(".*=.*"))
{
System.out.println("2");
int i = line.indexOf('=');
String name = line.substring(0,i);
String value = line.substring(i+1);
System.out.println("name="+name+"value="+value);
current.setProperty(name,value);
}
}
File.ini内容[MCI Extensions.BAK]
asf=你要的值
liblist=libwhite.dat,
libversion=1.0.1.1114,
libdate=2008-10-22
libver2=1.0.1.2121
libdate2=2008-10-22
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class IniReader
{
protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;
public IniReader(String filename) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}
protected void read(BufferedReader reader) throws IOException
{
String line;
while((line = reader.readLine()) != null)
{
parseLine(line);
}
sections.put(currentSecion, current);
}
protected void parseLine(String line)
{
line = line.trim();
if(line.matches("\\[.*\\]"))
{
if (current != null)
{
sections.put(currentSecion,current);
}
currentSecion = line.replaceFirst("\\[(.*)\\]","$1");
current = new Properties();
}
else if(line.matches(".*=.*"))
{
int i = line.indexOf('=');
String name = line.substring(0,i);
String value = line.substring(i+1);
current.setProperty(name,value);
}
}
public String getValue(String section,String name)
{
Properties p = (Properties) sections.get(section);
if (p == null)
{
return null;
}
String value = p.getProperty(name);
return value;
}
public static void main(String[] args) throws IOException
{
IniReader reader = new IniReader("c:\\File.ini");
System.out.println(reader.getValue("REG_UDP","PORT"));
}
}
INI文件:
[AppCtrl]
QihooHint=1 [REG_TCP]
HOST=reg2t.sandai.net
PORT=5200 [REG_UDP]
HOST=reg2u.sandai.net
PORT=6200
INTERVAL=300
DISUDPNUM=5 是正常的啊,能取到值的啊。