properties
View Code
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Properties; public class MyProperties extends ArrayList { private String fileName; private String code = "GBK"; public static void main(String[] args) throws IOException{ String pathString="D:/proper.properties"; MyProperties proper=new MyProperties(pathString,"GBK"); proper.setTitle("测试用Properties设置键值对的值"); proper.setProperties("studentName", "珍妮"); proper.setNotes("studentName","学生姓名"); proper.setProperties("room", "三年级一班"); proper.setNotes("room", "所在年级"); proper.setProperties("score", "98.5"); proper.setNotes("score", "数学分数"); proper.saveFile(); System.out.println(readFile(pathString,"GBK")); proper.getValue(); } public MyProperties(String fileName, String Code) throws UnsupportedEncodingException { this.setFileName(fileName); this.setCharacterEncoding(Code); } private void setFileName(String fileName) { this.fileName = fileName; } private void setCharacterEncoding(String code) throws UnsupportedEncodingException { new String("".getBytes("iso8859_1"), code); this.code = code; } public static boolean isExist(String fileName) { return new File(fileName).isFile(); } public static String readFile(String fileName, String code) throws IOException { StringBuffer sb = new StringBuffer();// 创建字符缓冲流 BufferedReader in = new BufferedReader(new FileReader(fileName));// 创建缓冲读对象 String s; while ((s = in.readLine()) != null) {// 循环读取文件信息 sb.append(s);// 拼接字符串 sb.append("\n"); } in.close(); return sb.toString(); } public void writeFile(String proper) throws IOException {// 字符串写入文件 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( fileName)));// 创建文本输入流打印对象 out.print(proper);// 将字符串写入文件 out.close();// 释放资源 } public void saveFile() throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( fileName))); String tmpString; for (int i = 0; i < size(); i++) { tmpString = get(i) + ""; out.println(tmpString); } out.close(); } public void setProperties(String key, String val) throws UnsupportedEncodingException {// 设置Properties键值 int pro = LookForKey(key); if (pro >= 0) { this.set(pro, key + "=" + val); } else { this.add(key + "=" + val); } } public int LookForKey(String key) throws UnsupportedEncodingException {// 查找键的序号 String tempString; for (int i = 0; i < size(); i++) {// 循环显示集合的信息 tempString = get(i) + ""; tempString = new String(tempString.getBytes("iso8859_1"), code);// 编码转换 if (tempString.indexOf(key) == 0) {// 没有找到键值对 return i; } } return -1; } public void setNotes(String key, String memo) throws UnsupportedEncodingException {// 设置备注 if ("".equals(key)) { this.add("#" + memo); return; } String tempString; int result = LookForKey(key); if (result == -1) { this.add("#" + memo); this.add(key + "="); } else { int postion = result - 1; if (postion < 0) { this.add(postion, "#" + memo); } else { tempString = this.get(postion) + ""; if ("#".equals(tempString.substring(0, 1)))// 判断截取的值 this.set(postion, "#" + memo); else { this.add(postion + 1, "#" + memo); } } } } public void setTitle(String title) {// 设置注释内容 String tmp= this.get(0) + " "; if (tmp == null || tmp.length() == 0) tmp = ""; else { tmp = tmp.substring(0, 1); } if ("#".equals(tmp)) this.set(0, "#" + title); else { this.add(0, ""); this.add(0, "#" + title); } } public String getProperties(String key) throws UnsupportedEncodingException { return getProperties(key, ""); } public String getProperties(String key, String defaultStr) throws UnsupportedEncodingException { String temp, resultString; for (int i = 0; i < size(); i++) { temp = get(i) + ""; temp = new String(temp.getBytes("iso8859_1"), code); if (temp.indexOf(key) == 0) { resultString = temp.substring(key.length() + 1); return resultString; } } return defaultStr; } public void getValue() throws IOException { InputStream in = new BufferedInputStream(new FileInputStream( "/proper.properties")); Properties p = new Properties(); p.load(in); String s = (String) p.get("studentName"); String name = new String(s.getBytes("iso8859_1"), "GBK"); System.out.println("输出studentName的值:" + name); String aString = (String) p.get("room"); String room = new String(aString.getBytes("iso8859_1"), "GBK"); System.out.println("输出room的值:" + room); } }
setTitle()总是报错,求解这个错误原因在哪里?错误如下:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at MyProperties.setTitle(MyProperties.java:137)
at MyProperties.main(MyProperties.java:21)