1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
import java.awt.image.SampleModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import org.ini4j.*;
public class lectureINI {
/**
* @param args
* @throws IOException
* @throws InvalidFileFormatException
*/
public static void main(String[] args) throws InvalidFileFormatException, IOException {
sample01(new Ini(new File("dwarfs.ini")));
sample02(new File("C:\\Users\\xxxxxxxx\\Documents\\FICHIERS STAGE JAVA\\essais ini\\dwarfs.ini"));
sample03(new Ini(new File("dwarfs.ini")));
sample04(new Ini(new File("dwarfs.ini")));
}
static void sample01(Ini ini)
{
Ini.Section section = ini.get("happy");
// read some values
String age = section.get("age");
String weight = section.get("weight");
String homeDir = section.get("homeDir");
// .. or just use java.util.Map interface...
Map<String, String> map = ini.get("happy");
age = map.get("age");
weight = map.get("weight");
homeDir = map.get("homeDir");
// get all section names
Set<String> sectionNames = ini.keySet();
System.out.println("sample01 : age = " + age);
System.out.println("sample01 : weight = " + weight);
System.out.println("sample01 : homeDir = " + homeDir);
}
static void sample02(File file) throws IOException
{
Ini ini = new Ini();
ini.load(new FileReader(file));
//
// or instantiate and load data:
//
ini = new Ini(new FileReader(file));
File copy = File.createTempFile("sample", ".ini");
ini.store(copy);
System.out.println("sample02 éxécuté. Allez voir dans le dossier contenant le .ini");
}
static void sample03(Ini ini)
{
Ini.Section dopey = ini.get("dopey");
// get method doesn't resolve variable references
String weightRaw = dopey.get("weight"); // = ${bashful/weight}
String heightRaw = dopey.get("height"); // = ${doc/height}
System.out.println("sample03 : weight = " + weightRaw);
System.out.println("sample03 : height = " + heightRaw);
// to resolve references, you should use fetch method
String weight = dopey.fetch("weight"); // = 45.7
String height = dopey.fetch("height"); // = 87.7
System.out.println("sample03 : age = " + weight);
System.out.println("sample03 : weight = " + height);
}
static void sample04(Ini ini)
{
Ini.Section sneezy = ini.get("sneezy");
String n1 = sneezy.get("fortuneNumber", 0); // = 11
String n2 = sneezy.get("fortuneNumber", 1); // = 22
String n3 = sneezy.get("fortuneNumber", 2); // = 33
String n4 = sneezy.get("fortuneNumber", 3); // = 44
System.out.println("sample04 : N1 = " + n1);
System.out.println("sample04 : N2 = " + n2);
System.out.println("sample04 : N3 = " + n3);
System.out.println("sample04 : N4 = " + n4);
// ok, lets do in it easier...
// int[] n = sneezy.get("fortuneNumber", int[].class);
}
void sample05()
{
Ini ini = new Ini();
// lets add a section, it will create needed intermediate sections as well
ini.add("root/child/sub");
//
Ini.Section root;
Ini.Section sec;
root = ini.get("root");
sec = root.getChild("child").getChild("sub");
// or...
sec = root.lookup("child", "sub");
// or...
sec = root.lookup("child/sub");
// or even...
sec = ini.get("root/child/sub");
}
} |
Partager