20非常有用的Java程序片段(2)

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.    
  6. import com.lowagie.text.Document;   
  7. import com.lowagie.text.Paragraph;   
  8. import com.lowagie.text.pdf.PdfWriter;   
  9.    
  10. public class GeneratePDF {   
  11.    
  12.     public static void main(String[] args) {   
  13.         try {   
  14.             OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));   
  15.    
  16.             Document document = new Document();   
  17.             PdfWriter.getInstance(document, file);   
  18.             document.open();   
  19.             document.add(new Paragraph("Hello Kiran"));   
  20.             document.add(new Paragraph(new Date().toString()));   
  21.    
  22.             document.close();   
  23.             file.close();   
  24.    
  25.         } catch (Exception e) {   
  26.    
  27.             e.printStackTrace();   
  28.         }   
  29.     }   
  30. }  

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. public class SimpleSingleton {   
  2.     private static SimpleSingleton singleInstance =  new SimpleSingleton();   
  3.    
  4.     //Marking default constructor private   
  5.     //to avoid direct instantiation.   
  6.     private SimpleSingleton() {   
  7.     }   
  8.    
  9.     //Get instance for class SimpleSingleton   
  10.     public static SimpleSingleton getInstance() {   
  11.    
  12.         return singleInstance;   
  13.     }   
  14. }  

另一种实现

 
 
  1. public enum SimpleSingleton {   
  2.     INSTANCE;   
  3.     public void doSomething() {   
  4.     }   
  5. }   
  6.    
  7. //Call the method from Singleton:   
  8. 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.image.BufferedImage;   
  6. import javax.imageio.ImageIO;   
  7. import java.io.File;   
  8.    
  9. ...   
  10.    
  11. public void captureScreen(String fileName) throws Exception {   
  12.    
  13.    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();   
  14.    Rectangle screenRectangle = new Rectangle(screenSize);   
  15.    Robot robot = new Robot();   
  16.    BufferedImage image = robot.createScreenCapture(screenRectangle);   
  17.    ImageIO.write(image, "png"new File(fileName));   
  18.    
  19. }   
  20. ...  

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.    
  12.   // It is also possible to filter the list of returned files.   
  13.   // This example does not return any files that start with `.'.   
  14.   FilenameFilter filter = new FilenameFilter() {   
  15.       public boolean accept(File dir, String name) {   
  16.           return !name.startsWith(".");   
  17.       }   
  18.   };   
  19.   children = dir.list(filter);   
  20.    
  21.   // The list of files can also be retrieved as File objects   
  22.   File[] files = dir.listFiles();   
  23.    
  24.   // This filter only returns directories   
  25.   FileFilter fileFilter = new FileFilter() {   
  26.       public boolean accept(File file) {   
  27.           return file.isDirectory();   
  28.       }   
  29.   };   
  30.   files = dir.listFiles(fileFilter);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值