// 得到当前方法的名字 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: 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
java怎么用一行代码初始化ArrayList1、ArrayListplaces = new ArrayList (Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));2、Java 7 中的列表中文字:List list = ["A", "B", "C"];3、List places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");4、ArrayList arrList = new ArrayList() {"1",2,3,"4" };5、List strings = asList("foo", "bar", "baz");6、若要设置列表填充 N 的默认对象的副本:ArrayList
过滤文件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; } }