不错代码

不错代码_第1张图片

不错代码_第2张图片

    // 得到当前方法的名字
    public static void getName() {
        System.out.println(Thread.currentThread().getStackTrace()[1]
                .getMethodName());
    }

    // 截屏
    public static void captureScreen() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        try {
            Robot robot = new Robot();
            BufferedImage image = robot.createScreenCapture(screenRectangle);
            ImageIO.write(image, "png", new File("jieping.png"));
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    // 使用NIO进行快速的文件拷贝
    public static void fileCopy(File in, File out) throws IOException {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            // magic number for windows, 64M-32Kb
            int maxCount = (64 * 1024 * 1024) - (32 * 1024);
            long size = inChannel.size();
            long position = 0;
            while (position < size) {
                position += inChannel
                        .transferTo(position, maxCount, outChannel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }

    }

    // 创建json格式数据
    //依赖jar:json-rpc-1.0.jar
    public static void demoJson() {
        JSONObject json = new JSONObject();
        json.put("city", "shanghai");
        json.put("name", "earic");
        System.out.println(json.toString());
    }

    // 设置http代理
    public static void setProxy() {
        System.getProperties().put("http.proxyHost", "43.82.218.50");
        System.getProperties().put("http.proxyPort", "8080");
        System.getProperties().put("http.proxyUser", "ap\5109v20459");
        System.getProperties().put("http.proxyPassword", "qazxsw12");
        try {
            URL url = new URL("http://www.baidu.com");
            URLConnection conn = url.openConnection();
            String str = conn.getHeaderField(0);
            if(str.indexOf("OK")>0){
                System.out.println("ok connetcted……");
            }else{
                System.out.println("error……");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //列出文件和目录
    public static void listFiles(){
        File dir = new File("C:\\Users\\spring\\workspace\\newsisAP");
        String[] children = dir.list();
        if(children == null){
            System.out.println("error……");
        }else{
            for(int i=0;i<children.length;i++){
                String filename = children[i];
                //列出文件目录(含文件和文件夹)
                System.out.println(filename);
            }
        }
        FilenameFilter filter = new FilenameFilter(){
            public boolean accept(File dir,String name){
                return !name.startsWith(".");
            }
        };
        children = dir.list(filter);
        for(String name:children){
            //过滤掉“.”开头的文件
//            System.out.println(name);
        }
        File[] files = dir.listFiles();
        FileFilter fileFilter = new FileFilter(){
            public boolean accept(File file){
                return file.isDirectory();
            }
        };
        files = dir.listFiles(fileFilter);
        for(File name:files){
            //过滤非文件夹
//            System.out.println(name.getName());
        }
    }
    
    // 把 Array 转换成 Map 
    //依赖jar:commons-lang3-3.1.jar
    public static void getArrayToMap(){
        String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },  
                { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };  
        Map countryCapitals =  ArrayUtils.toMap(countries);  
         
        System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));  
        System.out.println("Capital of France is " + countryCapitals.get("France"));  
    }

 

你可能感兴趣的:(代码)