Fork me on GitHub

Java中一些常用的方法

1.计算程序运行时常

long start = System.currentTimeMillis();

…

…

…

long end = System.currentTimeMillis();

System.out.println("程序运行时常 : "+(end-start)+" ms");

2.文件读写

    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();
            }
     }

3.立flag

  设置布尔变量,用来在程序运行时对一些逻辑进行标记。其中false和true需要自己定义其含义。因此在设置flag的时,需要注意false以及true对应的含义。否则这些逻辑上的错误很难被检查出来。

    boolean flag = true;
    …
    …
    If()
    }
    …
    /* 根据程序运行逻辑进行对flag的修改 */
    else{
    
  }    

4.使用HashMap

  声明myMap为HashMAp类型

HashMap<String,String> myMap=new HashMap<String,String>();

  其中HashMap中的第一个参数,第二个参数为String

  可以使用HashMap来构造key,value一一对应的结构。

  例如:学号对应一个姓名

            

  

 

 

 

    则可以使用put来构造HashMap

myMap.put("1","张三");

myMap.put("2","李四");

myMap.put("3","王五");

myMap.put("4","赵六");

  可以使用get来查看key对应的value

myMap.get("1");//会返回张三

5.将excel的去重后内容放到list中

    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();

6.定时进行写文件

  使用静态方法

    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();
        }
 }

7.第二种定时写文件

  使用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();
        }
    }

 

posted @ 2016-10-29 16:35  伊甸一点  阅读(2061)  评论(0编辑  收藏  举报