构造函数是static的,饿汉单例模式时,注意与其他static成员的初始化顺序。

    xiaoxiao2021-04-16  31

     String filePath = "E:/";  String fileName = "X1logRecords.txt";  String linuxFilePath = "/usr/" + fileName;  static boolean isOpen = true; //switch  private File file;

    // static boolean isLinux = true; //ok  甚至可以把这边开关放到其他类中,免得搅不清。

     private static X1RecordsToFile records = new X1RecordsToFile(); //依赖于isLinux

     static boolean isLinux = true; //error       private X1RecordsToFile(){   //isLinux = true;   String pathname = filePath + fileName;      if (true == isLinux){ //linux    file = new File(linuxFilePath);    if (file.exists()) {     file.delete();    }    try {     file.createNewFile();    } catch (IOException e1) {     e1.printStackTrace();    }   }

    else{ //windows    file = new File(pathname);    if (file.exists()) {     file.delete();    }    try {     file.createNewFile();    } catch (IOException e) {     e.printStackTrace();      }   }      }

    一直是windnws的目录,不是linux的,因为isLinux一直是false。

    转载请注明原文地址: https://ju.6miu.com/read-672389.html

    最新回复(0)