9. 创建 JSON 格式的数据

请先阅读这篇文章 了解一些细节,
并下面这个JAR 文件:json-rpc-1.0.jar (75 kb)

 
  
  1. import org.json.JSONObject;  

  2. ...  

  3. ...  

  4. JSONObject json = new JSONObject();  

  5. json.put("city", "Mumbai");  

  6. json.put("country", "India");  

  7. ...  

  8. String output = json.toString();  

  9. ...  

10. 使用iText JAR生成PDF

阅读这篇文章 了解更多细节

 
  
  1. import java.io.File;  

  2. import java.io.FileOutputStream;  

  3. import java.io.OutputStream;  

  4. import java.util.Date;  

  5. import com.lowagie.text.Document;  

  6. import com.lowagie.text.Paragraph;  

  7. import com.lowagie.text.pdf.PdfWriter;  

  8. publicclass GeneratePDF {  

  9. publicstaticvoid main(String[] args) {  

  10. try {  

  11.            OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));  

  12.            Document document = new Document();  

  13.            PdfWriter.getInstance(document, file);  

  14.            document.open();  

  15.            document.add(new Paragraph("Hello Kiran"));  

  16.            document.add(new Paragraph(new Date().toString()));  

  17.            document.close();  

  18.            file.close();  

  19.        } catch (Exception e) {  

  20.            e.printStackTrace();  

  21.        }  

  22.    }  

  23. }  

11. HTTP 代理设置

阅读这篇 文章 了解更多细节。

 
  
  1. System.getProperties().put("http.proxyHost", "someProxyURL");  

  2. System.getProperties().put("http.proxyPort", "someProxyPort");  

  3. System.getProperties().put("http.proxyUser", "someUserName");  

  4. System.getProperties().put("http.proxyPassword", "somePassword");

2. 单实例Singleton 示例

请先阅读这篇文章 了解更多信息

 
  
  1. publicclass SimpleSingleton {  

  2. privatestatic SimpleSingleton singleInstance =  new SimpleSingleton();  

  3. //Marking default constructor private  

  4. //to avoid direct instantiation.  

  5. private SimpleSingleton() {  

  6.    }  

  7. //Get instance for class SimpleSingleton  

  8. publicstatic SimpleSingleton getInstance() {  

  9. return singleInstance;  

  10.    }  

  11. }  

另一种实现

 
  
  1. publicenum SimpleSingleton {  

  2.    INSTANCE;  

  3. publicvoid doSomething() {  

  4.    }  

  5. }  

  6. //Call the method from Singleton:  

  7. SimpleSingleton.INSTANCE.doSomething();  

13. 抓屏程序

阅读这篇文章 获得更多信息。

 
  
  1. import java.awt.Dimension;  

  2. import java.awt.Rectangle;  

  3. import java.awt.Robot;  

  4. import java.awt.Toolkit;  

  5. import java.awt.p_w_picpath.BufferedImage;  

  6. import javax.p_w_picpathio.ImageIO;  

  7. import java.io.File;  

  8. ...  

  9. publicvoid captureScreen(String fileName) throws Exception {  

  10.   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  

  11.   Rectangle screenRectangle = new Rectangle(screenSize);  

  12.   Robot robot = new Robot();  

  13.   BufferedImage p_w_picpath = robot.createScreenCapture(screenRectangle);  

  14.   ImageIO.write(p_w_picpath, "png", new File(fileName));  

  15. }  

  16. ...  

14. 列出文件和目录

 
  
  1. File dir = new File("directoryName");  

  2.  String[] children = dir.list();  

  3. if (children == null) {  

  4. // Either dir does not exist or is not a directory  

  5.  } else {  

  6. for (int i=0; i < children.length; i++) {  

  7. // Get filename of file or directory  

  8.          String filename = children[i];  

  9.      }  

  10.  }  

  11. // It is also possible to filter the list of returned files.  

  12. // This example does not return any files that start with `.'.  

  13.  FilenameFilter filter = new FilenameFilter() {  

  14. publicboolean accept(File dir, String name) {  

  15. return !name.startsWith(".");  

  16.      }  

  17.  };  

  18.  children = dir.list(filter);  

  19. // The list of files can also be retrieved as File objects  

  20.  File[] files = dir.listFiles();  

  21. // This filter only returns directories  

  22.  FileFilter fileFilter = new FileFilter() {  

  23. publicboolean accept(File file) {  

  24. return file.isDirectory();  

  25.      }  

  26.  };  

  27.  files = dir.listFiles(fileFilter);