1.計算程序運行時常
long start = System.currentTimeMillis();
…
…
…
long end = System.currentTimeMillis();
System.out.println("程序運行時常 : "+(end-start)+" ms");
String fileName = "/home/test";//定義寫文件路徑
FileWriter writer = null;//文件讀寫流
public void write_To_File(){
writer = new FileWriter(fileName, true);
try {
writer.write("Hello World!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
設置布爾變量,用來在程序運行時對一些邏輯進行標記。其中false和true需要自己定義其含義。因此在設置flag的時,需要注意false以及true對應的含義。否則這些邏輯上的錯誤很難被檢查出來。
boolean flag = true;
…
…
If()
}
…
/* 根據程序運行邏輯進行對flag的修改 */
else{
}
聲明myMap為HashMAp類型
HashMap<String,String> myMap=new HashMap<String,String>();
其中HashMap中的第一個參數,第二個參數為String
可以使用HashMap來構造key,value一一對應的結構。
例如:學號對應一個姓名
myMap.put("1","張三");
myMap.put("2","李四");
myMap.put("3","王五");
myMap.put("4","趙六");
可以使用get來查看key對應的value
myMap.get("1");//會返回張三
String path = "/home/allNumber.csv";
public static ArrayList<String> myList = new ArrayList<String>();//聲明list, 內容為String類型
public static void createList(String path) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
String line = "";
while((line=reader.readLine())!=null){//賦值並進行判斷
if(!myListlist.contains(line )){//去重
myList.add(line );
}
}
reader.close();
}
首先聲明文件讀寫流,傳入參數path為文件路徑;
while循環體中需要判斷是否已經到了文件的末尾,同時進行賦值操作;
由於需要進行去重操作,只需要每次向myList中添加數據之前前進行判斷該數據是否已經存在;
記住最後要將文件的讀寫流關閉 reader.close();
使用靜態方法
public class test {
public static void executeFixedRate() throws IOException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
/*
* 參數1 new count() 表示運行的方法
* 參數2 0 表示第一次調用等待 0ms 之後運行count中的run方法
* 參數3 5000 表示之後每經過5000ms再次調用
* 參數4 TimeUnit.MILLISECONDS 設置時間為毫秒
*/
executor.scheduleAtFixedRate(new count(),0,5000,TimeUnit.MILLISECONDS);
}
static class count implements Runnable{
private String fileName = "/home/test";
private FileWriter writer = null;
//設置日期格式
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void run(){//運行代碼
try {
writer = new FileWriter(fileName, true);
writer.write("Hello World"+df.format(new Date())+"\n");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
test.executeFixedRate();
}
}
使用Timer類
public class test {
private static void executeFixedRate() {
Timer timmerTask = new Timer();
Calendar calEnviron = Calendar.getInstance();
// 每00:00:00 開始執行
calEnviron.set(Calendar.HOUR_OF_DAY, 0);
calEnviron.set(Calendar.MINUTE, 0);
calEnviron.set(Calendar.SECOND, 0);
// date制定間
Date dateSetter = new Date();
dateSetter = calEnviron.getTime();
// nowDate前間
Date nowDateSetter = new Date();
// 所間差距現待觸發間間隔
long intervalEnviron = dateSetter.getTime() - nowDateSetter.getTime();
if (intervalEnviron < 0) {
calEnviron.add(Calendar.DAY_OF_MONTH, 1);
dateSetter = calEnviron.getTime();
intervalEnviron = dateSetter.getTime() - nowDateSetter.getTime();
}
timmerTask.schedule(new count(), intervalEnviron, 1 * 1000 * 60 * 60 * 24);
}
static class count implements Runnable{
private String fileName = "/home/test";
private FileWriter writer = null;
//設置日期格式
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void run(){//運行代碼
try {
writer = new FileWriter(fileName, true);
writer.write("Hello World"+df.format(new Date())+"\n");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
test.executeFixedRate();
}
}