求java完整的读取ini的所有section及key及删除section或key等 如题!现在网上的java读取ini都是只读取某个section下的key的value哪个高人能实现如下功能!读取某个section下的key的value读取ini的所有section及key删除section及其下面的key只删除某个section下的一个key修改section我觉得这样算比较完整了..希望大家帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拜托楼主先把自己的问题整理一下你的问题有矛盾阿现在网上的java读取ini都是只读取某个section下的key的value 读取某个section下的key的value 还有,貌似楼主对ini文件的理解出现偏差1.ini文件中的key是唯一的所以,不存在读取某个section下的key的value检索ini文件的时候,肯定是全文检索key,怎莫能定位到section呢除非你把ini文件当普通的文本文件来处理 2.所谓section只不过用注释来区分段落,以便ini文件具有更好的可读性实际上不起任何作用 就是有两种嘛一种是带section一种是没有的我就是需要有section的嘛..key可能是检索全文的..但是他读取的还是某个section下的key的value呀 我都说了,section只不过是注释,可以自己加,也可以删掉不然,楼主自己贴一个你理解的带section的ini文件上来 section不是注释,以前的ini中不同section中的key是可以重复的,而且读取时也需要指定section才可以读取出来[mysqld]basedir=D:/MySQL/datadir=D:/MySQL/datadefault-character-set=gbk[client]default-character-set=gbkjava中默认没有对这样的ini支持。如果你需要,自己写咯,一行行的读取发现是"["开头就是section开头,然后读取到一行就找"=",前面是key后面是value就行了 所以偶说,除非不把它当ini文件看一行行读,和读一般文本文件有何区别呢 你就单独写一个类来处理这种ini文件啊,这个并不复杂啊。(实现当然还是一行行的读,Properties文件也一样是这种处理方式啊) 呵呵,楼主这些功能windows api里倒都直接有,java里不流行这些,流行的是xml public class IniFile { private Map<String, Map<String, String>> values = new HashMap<String, Map<String,String>>(); public IniFile(File file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; String section = null; while((line = reader.readLine()) != null) { line = line.trim(); if(line.length() == 0) continue; if(line.startsWith("#")) continue; if(line.startsWith("[")) section = line.substring(1, line.lastIndexOf(']')); else { int index = line.indexOf('='); if(index < 0) continue; String key = line.substring(0, index); String value = line.substring(index + 1); Map<String, String > sectionValues = values.get(section); if(sectionValues == null) { sectionValues = new HashMap<String, String>(); values.put(section, sectionValues); } sectionValues.put(key, value); } } reader.close(); } public String getProperty(String section, String key, String defaultValue) { Map<String, String> sectionValues = values.get(section); if(sectionValues == null) return defaultValue; String value = sectionValues.get(key); if(value == null) return defaultValue; else return value; }}随便写一个例子 其实早有免费的jar包可以用了http://ini4j.sourceforge.net/ import java.io.BufferedReader;import java.io.Closeable;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;public class Ini implements Iterable<Section> { private Map<String, Section> sections = null; public Ini() { } public void load(InputStream in) throws IOException { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(in)); Section section = Section.getDefaultSection(); for(String str = null; (str = br.readLine()) != null; ) { Item item = parseItem(str, section); if(item == null) { continue; } if(item instanceof Section) { section = addSection((Section)item); continue; } if(item instanceof Pair) { addPair((Pair)item); continue; } } } finally { closeIO(br); } } public Section getSection(String sectionName) { return sections.get(sectionName); } public Pair getPair(String sectionName, String pairName) { Section section = getSection(sectionName); if(section == null) { return null; } return section.getPair(pairName); } private void addPair(Pair pair) { initSections(); Section section = addSection(pair.getSection()); section.addItem(pair); } private Section addSection(Section section) { initSections(); if(sections.get(section.getName()) == null) { sections.put(section.getName(), section); } return section; } private void initSections() { if(sections == null) { sections = new LinkedHashMap<String, Section>(); } } private void closeIO(Closeable io) throws IOException { if(io != null) { io.close(); } } private Item parseItem(String line, Section section) { if(line == null || (line = line.trim()).length() == 0) { return null; } line = escape(line); if((line = line.trim()).length() == 0) { return null; } int idx = line.indexOf('='); if(idx > 0) { return new Pair(line.substring(0, idx).trim(), line.substring(idx + 1).trim(), section); } if(line.startsWith("[") && line.endsWith("]")) { return new Section(line.substring(1, line.length() - 1).trim()); } return null; } private String escape(String line) { if(line.indexOf('\\') < 0 && line.indexOf('#') < 0) { return line; } char[] chs = line.toCharArray(); char[] nchs = new char[chs.length]; boolean preBackslash = false; int k = 0; for(int i = 0; i < chs.length; i++) { if(chs[i] == '\\') { if(!preBackslash) { nchs[k++] = '\\'; } preBackslash = !preBackslash; continue; } if(chs[i] == '#') { if(!preBackslash) { return new String(nchs, 0, k); } preBackslash = false; nchs[k - 1] = '#'; continue; } if(preBackslash) { preBackslash = false; } nchs[k++] = chs[i]; } return new String(nchs, 0, k); } public String toString() { if(sections == null) { return null; } StringBuilder sb = new StringBuilder(); for(Map.Entry<String, Section> entry : sections.entrySet()) { sb.append(entry.getValue().serialize()).append("\n"); for(Pair p : entry.getValue()) { sb.append(p.serialize()).append("\n"); } } return sb.toString(); } public Iterator<Section> iterator() { if(sections == null) { return null; } return sections.values().iterator(); } public static void main(String[] args) throws FileNotFoundException, IOException { Ini ini = new Ini(); ini.load(new FileInputStream("mysql.ini")); System.out.println(ini.toString()); System.out.println(ini.getPair("mysqld", "datadir").serialize()); }}public abstract class Item { protected String name; public Item(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract String serialize();}import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;public class Section extends Item implements Iterable<Pair> { private Map<String, Pair> items; public final static String DEFAULT_SECTION_NAME = "default-section-name"; private static Section defaultSection = new Section(DEFAULT_SECTION_NAME); public static Section getDefaultSection() { return defaultSection; } public Section(String name) { super(name); } public int size() { if(items == null) { return 0; } return items.size(); } public Pair addItem(Pair item) { if(items == null) { items = new LinkedHashMap<String, Pair>(); } return items.put(item.getName(), item); } public Pair addItem(String key, String value) { return addItem(new Pair(key, value, this)); } public Pair removeItem(Pair item) { if(items == null) { return null; } return items.remove(item.getName()); } public Pair removeItem(String key) { if(items == null) { return null; } return items.remove(key); } public String serialize() { return "[" + getName() + "]"; } public Iterator<Pair> iterator() { if(items == null) { return null; } return items.values().iterator(); } public Pair getPair(String pairName) { if(items == null) { return null; } return items.get(pairName); }}public class Pair extends Item { private String value; private Section section; public Pair(String name, String value, Section section) { super(name); this.value = value; this.section = section; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Section getSection() { return section; } public void setSection(Section section) { this.section = section; } public String serialize() { return getName() + "=" + getValue(); }} 两个Java程序的区别 不知道是什么错误,怎么改呢? 新手问一个简单问题 请问这个程序中join的作用是什么? 如何有效清除Graphics所绘的图形 强烈要求CSDN恢复原来的收藏功能 关于C调用java的错误. 求助! 一个很急的图形界面编程的问题 怎么写重做和撤消的代码? java时间合并问题... 写了一个表格利用AbstractTableModel,如何实现动态插入行呢?
你的问题有矛盾阿现在网上的java读取ini都是只读取某个section下的key的value
读取某个section下的key的value
所以,不存在
读取某个section下的key的value检索ini文件的时候,肯定是全文检索key,怎莫能定位到section呢
除非你把ini文件当普通的文本文件来处理 2.所谓section
只不过用注释来区分段落,以便ini文件具有更好的可读性
实际上不起任何作用
一种是带section一种是没有的我就是需要有section的嘛..key可能是检索全文的..但是他读取的还是某个section下的key的value呀
我都说了,section只不过是注释,可以自己加,也可以删掉不然,楼主自己贴一个你理解的带section的ini文件上来
basedir=D:/MySQL/
datadir=D:/MySQL/data
default-character-set=gbk
[client]
default-character-set=gbk
java中默认没有对这样的ini支持。如果你需要,自己写咯,一行行的读取发现是"["开头就是section开头,然后读取到一行就找"=",前面是key后面是value就行了
一行行读,和读一般文本文件有何区别呢
private Map<String, Map<String, String>> values = new HashMap<String, Map<String,String>>();
public IniFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
String section = null;
while((line = reader.readLine()) != null) {
line = line.trim();
if(line.length() == 0)
continue;
if(line.startsWith("#"))
continue;
if(line.startsWith("["))
section = line.substring(1, line.lastIndexOf(']'));
else {
int index = line.indexOf('=');
if(index < 0)
continue;
String key = line.substring(0, index);
String value = line.substring(index + 1);
Map<String, String > sectionValues = values.get(section);
if(sectionValues == null) {
sectionValues = new HashMap<String, String>();
values.put(section, sectionValues);
}
sectionValues.put(key, value);
}
}
reader.close();
}
public String getProperty(String section, String key, String defaultValue) {
Map<String, String> sectionValues = values.get(section);
if(sectionValues == null)
return defaultValue;
String value = sectionValues.get(key);
if(value == null)
return defaultValue;
else
return value;
}
}随便写一个例子
http://ini4j.sourceforge.net/
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;public class Ini implements Iterable<Section> {
private Map<String, Section> sections = null;
public Ini() {
}
public void load(InputStream in) throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(in));
Section section = Section.getDefaultSection();
for(String str = null; (str = br.readLine()) != null; ) {
Item item = parseItem(str, section);
if(item == null) {
continue;
}
if(item instanceof Section) {
section = addSection((Section)item);
continue;
}
if(item instanceof Pair) {
addPair((Pair)item);
continue;
}
}
} finally {
closeIO(br);
}
}
public Section getSection(String sectionName) {
return sections.get(sectionName);
}
public Pair getPair(String sectionName, String pairName) {
Section section = getSection(sectionName);
if(section == null) {
return null;
}
return section.getPair(pairName);
}
private void addPair(Pair pair) {
initSections();
Section section = addSection(pair.getSection());
section.addItem(pair);
}
private Section addSection(Section section) {
initSections();
if(sections.get(section.getName()) == null) {
sections.put(section.getName(), section);
}
return section;
}
private void initSections() {
if(sections == null) {
sections = new LinkedHashMap<String, Section>();
}
}
private void closeIO(Closeable io) throws IOException {
if(io != null) {
io.close();
}
}
private Item parseItem(String line, Section section) {
if(line == null || (line = line.trim()).length() == 0) {
return null;
}
line = escape(line);
if((line = line.trim()).length() == 0) {
return null;
}
int idx = line.indexOf('=');
if(idx > 0) {
return new Pair(line.substring(0, idx).trim(), line.substring(idx + 1).trim(), section);
}
if(line.startsWith("[") && line.endsWith("]")) {
return new Section(line.substring(1, line.length() - 1).trim());
}
return null;
}
private String escape(String line) {
if(line.indexOf('\\') < 0 && line.indexOf('#') < 0) {
return line;
}
char[] chs = line.toCharArray();
char[] nchs = new char[chs.length];
boolean preBackslash = false;
int k = 0;
for(int i = 0; i < chs.length; i++) {
if(chs[i] == '\\') {
if(!preBackslash) {
nchs[k++] = '\\';
}
preBackslash = !preBackslash;
continue;
}
if(chs[i] == '#') {
if(!preBackslash) {
return new String(nchs, 0, k);
}
preBackslash = false;
nchs[k - 1] = '#';
continue;
}
if(preBackslash) {
preBackslash = false;
}
nchs[k++] = chs[i];
}
return new String(nchs, 0, k);
}
public String toString() {
if(sections == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for(Map.Entry<String, Section> entry : sections.entrySet()) {
sb.append(entry.getValue().serialize()).append("\n");
for(Pair p : entry.getValue()) {
sb.append(p.serialize()).append("\n");
}
}
return sb.toString();
} public Iterator<Section> iterator() {
if(sections == null) {
return null;
}
return sections.values().iterator();
}
public static void main(String[] args) throws FileNotFoundException, IOException {
Ini ini = new Ini();
ini.load(new FileInputStream("mysql.ini"));
System.out.println(ini.toString());
System.out.println(ini.getPair("mysqld", "datadir").serialize());
}
}public abstract class Item { protected String name;
public Item(String name) {
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public abstract String serialize();
}import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;public class Section extends Item implements Iterable<Pair> { private Map<String, Pair> items;
public final static String DEFAULT_SECTION_NAME = "default-section-name";
private static Section defaultSection = new Section(DEFAULT_SECTION_NAME);
public static Section getDefaultSection() {
return defaultSection;
}
public Section(String name) {
super(name);
}
public int size() {
if(items == null) {
return 0;
}
return items.size();
} public Pair addItem(Pair item) {
if(items == null) {
items = new LinkedHashMap<String, Pair>();
}
return items.put(item.getName(), item);
}
public Pair addItem(String key, String value) {
return addItem(new Pair(key, value, this));
}
public Pair removeItem(Pair item) {
if(items == null) {
return null;
}
return items.remove(item.getName());
}
public Pair removeItem(String key) {
if(items == null) {
return null;
}
return items.remove(key);
}
public String serialize() {
return "[" + getName() + "]";
} public Iterator<Pair> iterator() {
if(items == null) {
return null;
}
return items.values().iterator();
}
public Pair getPair(String pairName) {
if(items == null) {
return null;
}
return items.get(pairName);
}
}public class Pair extends Item { private String value;
private Section section;
public Pair(String name, String value, Section section) {
super(name);
this.value = value;
this.section = section;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
public String serialize() {
return getName() + "=" + getValue();
}
}