20个非常有用的Java的程序片段
8. 创建图片的缩略图 private void createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename) // determine thumbnail size from WIDTH and HEIGHT // draw original image to thumbnail image object and // save thumbnail image to outFilename 9. 创建 JSON 格式的数据 请先阅读这篇文章 了解一些细节, imp 10. 使用iText JAR生成PDF 阅读这篇文章 了解更多细节
imp public class GeneratePDF { public static void main(String[] args) { Document document = new Document(); document.close(); } catch (Exception e) { e.printStackTrace(); 11. HTTP 代理设置 阅读这篇 文章 了解更多细节。 System.getProperties().put("http.proxyHost", "someProxyURL"); 12. 单实例Singleton 示例 请先阅读这篇文章 了解更多信息
//Marking default constructor private //Get instance for class SimpleSingleton return singleInstance; 另一种实现 public enum SimpleSingleton { //Call the method from Singleton: 13. 抓屏程序 阅读这篇文章 获得更多信息。 imp ... public void captureScreen(String fileName) throws Exception { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); } 14. 列出文件和目录 File dir = new File("directoryName"); // It is also possible to fil // The list of files can also be retrieved as File objects // This fil 15. 创建ZIP和JAR文件
public class ZipIt { 16. 解析/读取XML 文件 XML文件 Java代码
imp imp public class XMLParser { public void getAllUserNames(String fileName) { // Print root element of the document NodeList studentList = docEle.getElementsByTagName("student"); // Print total student elements in document if (studentList != null && studentList.getLength() > 0) { Node node = studentList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { System.out Element e = (Element) node; nodeList = e.getElementsByTagName("grade"); nodeList = e.getElementsByTagName("age"); XMLParser parser = new XMLParser();
public class Main { public static void main(String[] args) { Map countryCapitals = ArrayUtils.toMap(countries); System.out.println("Capital of Japan is " + countryCapitals.get("Japan")); 18. 发送邮件 imp public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException //Set the host smtp address // create some properties and get the default Session // create a message // set the from and to address InternetAddress[] addressTo = new InternetAddress[recipients.length]; // Optional : You can also set your custom headers in the Email if you Want // Setting the Subject and Content Type 19. 发送代数据的HTTP 请求
public class Main { 20. 改变数组的大小
// Test routine for resizeArray(). |