不错代码
// 得到当前方法的名字 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")); }
java怎么用一行代码初始化ArrayList 1、ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); 2、Java 7 中的列表中文字:List<String> list = ["A", "B", "C"]; 3、List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata"); 4、ArrayList arrList = new ArrayList() {"1",2,3,"4" }; 5、List<String> strings = asList("foo", "bar", "baz"); 6、若要设置列表填充 N 的默认对象的副本:ArrayList<Object> list = new ArrayList<Object>(Collections.nCopies(1000, new Object()));
过滤文件 import java.io.File; import java.io.FilenameFilter; public class FileFilterDemo { public static void main(String[] args) throws Exception { File f = new File("D:\\workspace\\JavaTest\\src\\demo"); File[] files = f.listFiles(new FilenameFilter(){ public boolean accept(File dir, String name) { return name.endsWith(".java"); } }); for (File a : files) { System.out.println(a.getName()); } } }
//遍力 String[] strings = {"A", "B", "C", "D"}; Collection stringList = java.util.Arrays.asList(strings); /* 开始遍历 */ for (Iterator itr = stringList.iterator(); itr.hasNext();) { Object str = itr.next(); System.out.println(str); } //禁止重新赋值 int[] integers = {1, 2, 3, 4}; for (final int i : integers) { //i = i / 2; /* 编译时出错 */ //注意,这只是禁止了对循环变量进行重新赋值。给循环变量的属性赋值,或者调用能让循环变量的内容变化的方法,是不被禁止的 System.out.println(i); } //允许修改状态 Random[] randoms = new Random[]{new Random(1), new Random(2), new Random(3)}; for (final Random r : randoms) { r.setSeed(4);/* 将所有Random对象设成使用相同的种子 */ System.out.println(r.nextLong());/* 种子相同,第一个结果也相同 */ }
catch (Exception e) { StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); log.error(errors.toString()); return null; }
//静态内部类 public class Singleton { private static class LazyHolder { private static final Singleton INSTANCE = new Singleton(); } private Singleton (){} public static final Singleton getInstance() { return LazyHolder.INSTANCE; } }