Skip to content

Instantly share code, notes, and snippets.

@wfwei
wfwei / read file
Created May 7, 2012 10:33
java io
StringBuffer sb = new StringBuffer();
File f = new File(file);
if (f.exists()) {
System.out.println("file not exist");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
while ((s = br.readLine()) != null) {
sb.append(s);
}
@wfwei
wfwei / nio file copy
Created May 16, 2012 06:58
use nio to fast copy file
public static void fileCopy( File in, File out )
throws IOException
{
FileChannel inChannel = new
FileInputStream( in ).getChannel();
@wfwei
wfwei / getEncoding
Created May 23, 2012 13:11
charset detect in tika
/**
* TIKA-332: Check for meta http-equiv tag with charset info in
* HTML content.
* <p>
* TODO: Move this into core, along with CharsetDetector
*/
private String getEncoding(InputStream stream, Metadata metadata) throws IOException {
stream.mark(META_TAG_BUFFER_SIZE);
char[] buffer = new char[META_TAG_BUFFER_SIZE];
InputStreamReader isr = new InputStreamReader(stream, "us-ascii");
@wfwei
wfwei / usage.java
Last active October 9, 2015 03:57
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);
@wfwei
wfwei / common-usage.py
Last active October 12, 2015 06:47
python datetime from datetime to timestamp
from datetime import datetime
import time
now = datetime.now()
u = str(long(time.mktime(now.timetuple())))
@wfwei
wfwei / gist:4976396
Created February 18, 2013 10:13
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用, 改用下一个数字,比如7他们用8代替,17用19代替。 给定这个国家的数字,如何编程翻译成我们用的数字。
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 100
/**
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用,
改用下一个数字,比如7他们用8代替,17用19代替。
给定这个国家的数字,如何编程翻译成我们用的数字。
**/
@wfwei
wfwei / gist:4983615
Created February 19, 2013 06:42
首先定义运算"swap":从数组中移除一个元素,然后放在该数组的尾部。 用最少的“swaps"将数组排序。 比如,[3 1 2 4],最少需要[3 1 2 4]->[1 2 4 3]->[1 2 3 4]
#include <stdio.h>
/*
首先定义运算"swap":从数组中移除一个元素,然后放在该数组的尾部。
用最少的“swaps"将数组排序。
比如,[3 1 2 4],最少需要[3 1 2 4]->[1 2 4 3]->[1 2 3 4]
程序的输入为小于65536的正整数,数组长度小于100
*/
@wfwei
wfwei / gist:4984928
Created February 19, 2013 11:07
"this" cause stackoverflow
package mine.forfun;
public class InfiniteRecursion {
@Override
public String toString(){
return " InfiniteRecursion address: " + this; // + super.toString();
}
public static void main(String[] args) {
@wfwei
wfwei / gist:5011101
Created February 22, 2013 06:07
import static ... 导入静态成员(变量或方法)
import static java.lang.Math.*;
import static java.lang.System.out;
public class StaticImport {
public static void main(String args[]){
double r = cos(PI * theta);
out.println(r);
}
}
@wfwei
wfwei / gist:5011558
Last active December 14, 2015 02:09
汉诺塔问题递归算法----一个庙里有三个柱子,第一个有64个盘子,从上往下盘子越来越大。要求庙里的老和尚把这64个盘子全部移动到第三个柱子上。移动的时候始终只能小盘子压着大盘子。而且每次只能移动一个。
#include "stdio.h"
void move(int id, char f, char t){
printf("move %d from %c to %c\n", id, f, t);
}
void hanoi(int n, char a, char b, char c){
if(n==1){
move(n, a, c);
}else{