Java填坑笔记:OGG-PRM项目知识点

  1. 三元运算符:用来完成简单的选择逻辑,使用格式:
    (条件表达式)?表达式1:表达式2;
public static void ternary() {
  boolean boo = false;
  int c = boo ? test1(1) : test2(2);  //返回数据
}
  1. 遍历List集合,对集合中最后一个元素进行判断:
    foreach语句是for语句在特殊情况下的增强版本,简化编程,提高了代码的可读性和安全性(不用担心数组越界),相对于for语句是一个很好的补充,但foreach并不能替代for语句:
int lastIndex = colList.size()-1;
        for (int index = 0;index<=lastIndex;index++){
            String colName = colList.get(index);
            s.append(colName+" = "+colName);
            if(lastIndex == index){
                s.append("\n--插入标记\n),\n");
            }else {
                s.append(",\n");
            }
        }

还不能确定这样是最优雅的写法!

  1. PrepareStatement的用法及解释

1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理

  1. 判断String中是否包含指定字符(判断前三个字符):
String c = s.substring(0,3);
String schema = "";
  switch (c){
    case "ins":
    case "INS":
    schema = "ZOEINSUR";
    break;
  }

在java1.7之前大家都清楚switch的比较范围只能局限于(int 、short 、byte 、char)之间,Java 虚拟机和字节代码这个层次上,只支持在 switch 语句中使用与整数类型兼容的类型。在1.7后switch实现字符串比较的功能。具体是如何做到的?实际上,Java虚拟机和字节码层次上只支持switch语句中使用与整数类型兼容的类型没有变,只是这个实现字符串比较的新特性是在编译器这个层次上实现的。实现的机制是:将字符串之间的比较转换为其哈希值的比较。

  1. 遍历Map
public static void main(String[] args) {


 Map map = new HashMap();
 map.put("1", "value1");
 map.put("2", "value2");
 map.put("3", "value3");
 
 //第一种:普遍使用,二次取值
 System.out.println("通过Map.keySet遍历key和value:");
 for (String key : map.keySet()) {
  System.out.println("key= "+ key + " and value= " + map.get(key));
 }
 
 //第二种
 System.out.println("通过Map.entrySet使用iterator遍历key和value:");
 Iterator> it = map.entrySet().iterator();
 while (it.hasNext()) {
  Map.Entry entry = it.next();
  System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
 }
 
 //第三种:推荐,尤其是容量大时
 System.out.println("通过Map.entrySet遍历key和value");
 for (Map.Entry entry : map.entrySet()) {
  System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
 }

 //第四种
 System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
 for (String v : map.values()) {
  System.out.println("value= " + v);
 }
}
  1. 将Array转换成List
String[] userid = {"aa","bb","cc"};
List userList = new ArrayList();
Collections.addAll(userList, userid);
  1. String分割字符串,通过判断"\n"换行符进行字符串切割,
String[] res = str.split("\n");  

需要注意的是:

1、如果这个文件是在Linux或者mac下建立编写的,那么用str.split("\n")会出现正确的结果,如下图:


2、如果这个文件是在window下编写的,那么就该注意了,如果你还是用str.split("\n")就会出现错误的结果,如下图:

有人可能说没什么区别啊,仔细看好了,第2个的结果,this和is testing中间有个空行,而第一个没有。为什么会出现这个结果。
这还要从回车符来讲,简单来说,window下回车是由\r\n(即0x0D和0x0A)组成的,注意不是\n\r,而 linux下回车是由\n(即0x0A)小伙伴们别搞错了,这个我自己证实过,

这个window下编辑的文件,我用16进制打开的,大家看到第3,4列中的是0D、0A,也就是回车。
而在Linux下是这个样子的:

  1. Java IO流写入文件
public class WriteFileExample {
    public static void main(String[] args) {
 
        File file = new File("c:/newfile.txt");
        String content = "This is the text content";
 
        try (FileOutputStream fop = new FileOutputStream(file)) {
 
            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
 
            // get the content in bytes
            byte[] contentInBytes = content.getBytes();
 
            fop.write(contentInBytes);
            fop.flush();
            fop.close();
 
            System.out.println("Done");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

但这里的字符串如果包含中文,就会出现乱码,这是因为FileOutputStream是字节流,将文本按字节写入文件,而一个汉字是两个字节,无法一次写入,就会出现乱码,解决方法是使用OutputStreamWriter将字节流转换为字符流写入,同时指定utf-8编码,修改后代码如下:

public static void write2PRMFILE(String string){
        File file = new File("d:/test.prm");
        String content = "";
        content = string;

        try (OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");) {

            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            oStreamWriter.write(content);
            oStreamWriter.flush();
            oStreamWriter.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Java填坑笔记:OGG-PRM项目知识点)