20非常有用的Java程序片段(1)(2)
分享于 点击 4106 次 点评:205
9. 创建 JSON 格式的数据
请先阅读这篇文章 了解一些细节,
并下面这个JAR 文件:json-rpc-1.0.jar (75 kb)
- import org.json.JSONObject;
- ...
- ...
- JSONObject json = new JSONObject();
- json.put("city", "Mumbai");
- json.put("country", "India");
- ...
- String output = json.toString();
- ...
10. 使用iText JAR生成PDF
阅读这篇文章 了解更多细节
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import java.util.Date;
- import com.lowagie.text.Document;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.pdf.PdfWriter;
- public class GeneratePDF {
- public static void main(String[] args) {
- try {
- OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
- Document document = new Document();
- PdfWriter.getInstance(document, file);
- document.open();
- document.add(new Paragraph("Hello Kiran"));
- document.add(new Paragraph(new Date().toString()));
- document.close();
- file.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
11. HTTP 代理设置
阅读这篇 文章 了解更多细节。
- System.getProperties().put("http.proxyHost", "someProxyURL");
- System.getProperties().put("http.proxyPort", "someProxyPort");
- System.getProperties().put("http.proxyUser", "someUserName");
- System.getProperties().put("http.proxyPassword", "somePassword");
2. 单实例Singleton 示例
请先阅读这篇文章 了解更多信息
- public class SimpleSingleton {
- private static SimpleSingleton singleInstance = new SimpleSingleton();
- //Marking default constructor private
- //to avoid direct instantiation.
- private SimpleSingleton() {
- }
- //Get instance for class SimpleSingleton
- public static SimpleSingleton getInstance() {
- return singleInstance;
- }
- }
另一种实现
- public enum SimpleSingleton {
- INSTANCE;
- public void doSomething() {
- }
- }
- //Call the method from Singleton:
- SimpleSingleton.INSTANCE.doSomething();
13. 抓屏程序
阅读这篇文章 获得更多信息。
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- import java.io.File;
- ...
- public void captureScreen(String fileName) throws Exception {
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Rectangle screenRectangle = new Rectangle(screenSize);
- Robot robot = new Robot();
- BufferedImage image = robot.createScreenCapture(screenRectangle);
- ImageIO.write(image, "png", new File(fileName));
- }
- ...
14. 列出文件和目录
- File dir = new File("directoryName");
- String[] children = dir.list();
- if (children == null) {
- // Either dir does not exist or is not a directory
- } else {
- for (int i=0; i < children.length; i++) {
- // Get filename of file or directory
- String filename = children[i];
- }
- }
- // It is also possible to filter the list of returned files.
- // This example does not return any files that start with `.'.
- FilenameFilter filter = new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return !name.startsWith(".");
- }
- };
- children = dir.list(filter);
- // The list of files can also be retrieved as File objects
- File[] files = dir.listFiles();
- // This filter only returns directories
- FileFilter fileFilter = new FileFilter() {
- public boolean accept(File file) {
- return file.isDirectory();
- }
- };
- files = dir.listFiles(fileFilter);
用户点评