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;
publicclass GeneratePDF {
publicstaticvoid 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 示例
请先阅读这篇文章 了解更多信息
publicclass SimpleSingleton {
privatestatic SimpleSingleton singleInstance = new SimpleSingleton();
//Marking default constructor private
//to avoid direct instantiation.
private SimpleSingleton() {
}
//Get instance for class SimpleSingleton
publicstatic SimpleSingleton getInstance() {
return singleInstance;
}
}
另一种实现
publicenum SimpleSingleton {
INSTANCE;
publicvoid 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.p_w_picpath.BufferedImage;
import javax.p_w_picpathio.ImageIO;
import java.io.File;
...
publicvoid captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage p_w_picpath = robot.createScreenCapture(screenRectangle);
ImageIO.write(p_w_picpath, "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() {
publicboolean 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() {
publicboolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);