一. 编程题
2.1 注解,文件IO和字符串解析操作
已知文件 jdbc.txt
className=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/gp_01
username=root
password=root
要求针对于注解,注解用于在代码中提供 jdbc.txt 文件路径利用 io 读取文件内容将 文件内容读取存储到 HashMap<String, String> 中
private static HashMap<String, String> map1 = new HashMap<String, String>();
public static void main(String[] args) throws Exception {
Demo1.tFileInputStream();
}
public static void tFileInputStream() throws Exception{
FileInputStream fileInputStream = new FileInputStream("C:/Users/12997/Documents/WeChat Files/wxid_ckew8ta6dsu622/FileStorage/File/2023-03/周考/jdbc.txt");
byte[] bytes = new byte[1024*5];
int len = 0;
StringBuilder sb = new StringBuilder();
String str = bytes.toString();
while ((len = fileInputStream.read(bytes))!=-1){
sb.append(new String(bytes,0,len));
}
fileInputStream.close();
String s = sb.toString();
String[] split1 = s.split("\r\n"); // 必须先以换行分割,要不然先以等号的话,会导致,上一行的末尾,和下一行的开头分到同一个数组结点内
// 例如 上一行的末尾 com.mysql.jdbc.Driver 和下一行的开头 username 分到一个数组结点内
//
for (String s1 : split1) {
//System.out.println("s1 = " + s1);
//String[] split = s1.split("=");
int index = s1.indexOf("=");
map1.put(s1.substring(0,index),s1.substring(index + 1));
}
//增强for循环
map1.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
//迭代器有点low
// Set<Map.Entry<String ,String>>entrySet = map1.entrySet();
// Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
// while (iterator.hasNext()) {
// Map.Entry<String, String> next = iterator.next();
// System.out.println(next.getKey());
// //System.out.println(next.getValue());
// System.out.println();
// }
}