Skip to content

Instantly share code, notes, and snippets.

@wfwei
Last active October 9, 2015 03:57
Show Gist options
  • Save wfwei/3434220 to your computer and use it in GitHub Desktop.
Save wfwei/3434220 to your computer and use it in GitHub Desktop.
Basic Java Usage
/* MARK 使用Matcher.quoteRepalcement()过滤特殊字符 */
matchRes.appendReplacement(sb, Matcher.quoteReplacement(replacement));
// 格式化日期
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
public void getNdayBefor(int n){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar now = Calendar.getInstance();
now.set(Calendar.DATE, now.get(Calendar.DATE) - n);
System.out.println(sdf.format(now.getTime()));
return now.getTime();
}
// 筛选目录中的.java件
// 使用了匿名内部类,FilenameFilter是个借口
new File(".").list(new FilenameFilter(){
public boolean accept(File dir, String name){
return name.endswith(".java");
}
});
// Java IO
// Buffered input file
BufferedReader in = new BufferedReader(new FileReader("file.name"));
while(in.readline()!=null){}
// Input from memory
StringReader sin = new StringReader("source string");
// Formatted memory input
DataInputStream din = new DataInputStream(new ByteArrayInputStream("source string for bytes".getBytes()));
// Basic file ouput
PrintWriter pout = new PrintWriter(new BufferedWriter(new FileWriter("file.name")));
// Storing and Recovering Data DataOutputStream vs DataInputStream
// Alternatively using RandomAccessFile
RandomAccessFile rf = new RandomAccessFile("file.name", "rw");
// Reading binary files
BufferedInputStream bf = new BuferedInputStream(new FileInputStream("file.name"));
try{
byte[] data = new byte[bf.available()];
bf.read(data);
} finnaly {
bf.close();
}
// Process Control
Process proc = new ProcessBuilder("javap HelloWorld".split()).start();// Runtime.getRuntime().exec("javap HelloWorld")
BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream()));//得到子程序标准输出流
BufferedReader errs = new BufferedReader(new InputStreamReader(process.getErrorStream()));//得到子程序标准输出流
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment