/** * -------------------------------------------------- * 瀛楁杈撳叆嫻? * -------------------------------------------------- * 浠嶥ataInputStream緇ф壙 * 涓昏澧炲姞浜嗕粠鏂囨湰鏍煎紡杈撳叆嫻佷腑璇誨叆鏁版嵁瀛楁鐨勮兘鍔? * -------------------------------------------------- * * @author iwinyeah 鏉庢案瓚? * @version 1.0.0 * */ import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class FieldInputStream extends DataInputStream { public final static int BIN_MODE = 0; public final static int TXT_MODE = 1; int mode; public FieldInputStream(InputStream in, int mode) { super(in); this.mode = mode; } public boolean getBoolean() throws IOException { if (mode == 0) { return readBoolean(); } else { if ("1".equals(next())) { return true; } return false; } } public byte getByte() throws IOException { if (mode == 0) { return readByte(); } else { return (byte) Integer.parseInt(next()); } } public short getShort() throws IOException { if (mode == 0) { return readShort(); } else { return (short) Integer.parseInt(next()); } } public int getInt() throws IOException { if (mode == 0) { return readInt(); } else { return Integer.parseInt(next()); } } public long getLong() throws IOException { if (mode == 0) { return readLong(); } else { return Long.parseLong(next()); } } public String getString() throws IOException { if (mode == 0) { if (read() == 0) { return null; } else { return readUTF(); } } else { return next(); } } // 鍙栦笅涓鏍囪瘑絎? private byte[] buffer = new byte[255]; private int length = 0; private boolean eos = false; private final static int INITIAL = 0; private final static int ESCAPE = 1; private final static int COMMENT_START = 2; private final static int LINE_COMMENT = 3; private final static String WHITESPACE = "\n\r\t"; public String next() throws IOException { length = 0; int c = in.read(); int status = INITIAL; READWHILE: while (c != -1) { // 涓鐩磋鍒版枃浠跺熬 switch (status) { case INITIAL: if (c == '\n' || c == '\t') { // 濡傛灉鏄垎闅旂 break READWHILE; } else if (c == '\\') { status = ESCAPE; // 璁捐漿涔夊瓧絎︽爣蹇? } else if (c == '/') { status = COMMENT_START; // 璁炬敞閲婃爣蹇? } else { if (WHITESPACE.indexOf(c) < 0) { append(c); } } break; case ESCAPE: // 澶勭悊杞箟瀛楃 switch (c) { case 'n': append('\n'); break; case 'r': append('\r'); break; case 't': append('\t'); break; case 'b': append('\b'); break; case 'f': append('\f'); break; default: append(c); break; } status = INITIAL; // 璁炬甯告儏鍐墊爣蹇? break; case COMMENT_START: // 澶勭悊娉ㄩ噴 if (c == '/') { status = LINE_COMMENT; // 鏄寮忔敞閲? } else { status = INITIAL; // 濡傛灉閮戒笉鏄垯鎶婃敞閲婅搗濮嬬鍜屽垰璇誨叆鐨勫瓧絎﹂兘鍔犲叆鍒版爣璇嗙涓? append('/'); append(c); } break; case LINE_COMMENT: if (c == '\n') { status = INITIAL; // 濡傛灉褰撳墠涓鴻娉ㄩ噴鐘舵佸垯瑕佷竴鐩磋鍒拌灝炬墠鎭㈠姝e父鎯呭喌鏍囧織 break READWHILE; } break; } c = in.read(); // 璇誨叆涓嬩竴瀛楃 } if (c == -1) { eos = true; } // 濡傛灉璇誨埌鏂囦歡灝炬椂錛屾爣璇嗙闀垮害澶т簬闆訛紝鍒欒繑鍥炴爣璇嗙錛屽惁鍒欒繑鍥濶ULL鍊? if (length <= 0) { return null; } else { return new String(buffer, 0, length, "UTF-8"); } } // 灝嗚鍏ョ殑瀛楃鍔犲叆緙撳啿鍖? private void append(int c) { // 緙撳啿鍖轟笉瓚蟲椂鑷姩鎵╁睍 if (length >= buffer.length) { byte[] xBuffer = new byte[buffer.length + 16]; System.arraycopy(buffer, 0, xBuffer, 0, buffer.length); buffer = null; buffer = xBuffer; } buffer[length++] = (byte) c; } public boolean eos() { return eos; } }
package util; /** * -------------------------------------------------- * 瀛楁杈撳嚭嫻? * -------------------------------------------------- * 浠嶥ataOutputStream緇ф壙 * 涓昏澧炲姞浜嗗悜杈撳嚭嫻佸啓鍏ユ枃鏈牸寮忕殑鏁版嵁瀛楁鐨勮兘鍔? * 鏂囨湰鏍煎紡嫻佸皢鐢盩AB鍒嗛殧瀛楁錛屽洖杞︽崲琛岀鍒嗛殧璁板綍 * -------------------------------------------------- * * @author iwinyeah 鏉庢案瓚? * @version 1.0.0 * */ import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; public class FieldOutputStream extends DataOutputStream { public final static int BIN_MODE = 0; public final static int TXT_MODE = 1; private final static byte[] fieldSplit = {'\t'}; private final static byte[] recordSplit = {'\r','\n'}; private int mode; private boolean nextEnd = false; public FieldOutputStream(OutputStream out, int mode) { super(out); this.mode = mode; } // 鎺ョ潃鍐欏叆鐨勬槸鍚︽渶鍚庝竴涓瓧孌? // 鍐欑涓涓瓧孌靛墠浠ュ弬鏁癴alse璋冪敤瀹? // 鍐欐渶鍚庝竴涓瓧孌靛墠浠ュ弬鏁癴alse璋冪敤瀹? public void setNextEnd(boolean end){ nextEnd = end; } public void putBoolean(boolean value) throws IOException { if (mode == 0) { writeBoolean(value); } else { out.write(value ? '1' : '0'); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putByte(byte value) throws IOException { if (mode == 0) { writeByte(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putShort(short value) throws IOException { if (mode == 0) { writeShort(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putInt(int value) throws IOException { if (mode == 0) { writeInt(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putLong(long value) throws IOException { if (mode == 0) { writeLong(value); } else { out.write(String.valueOf(value).getBytes("UTF-8")); out.write(nextEnd ? recordSplit : fieldSplit); } } public void putString(String value) throws IOException { if (mode == 0) { if (value == null) { writeByte(0); } else { writeByte(1); writeUTF(value); } } else { if(value != null){ byte[] b = value.getBytes("UTF-8"); for(int i = 0; i < b.length; i++){ if(b[i] == '\n'){ out.write('\\'); out.write('n'); } else if(b[i] == '\r'){ out.write('\\'); out.write('r'); } else if(b[i] == '\t'){ out.write('\\'); out.write('t');} else if(b[i] == '\b'){ out.write('\\'); out.write('b');} else if(b[i] == '\f'){ out.write('\\'); out.write('f'); }else{ out.write(b[i]); } } } out.write(nextEnd ? recordSplit : fieldSplit); } } }
package util; /** * -------------------------------------------------- * 鏃ユ湡杞崲瀵硅薄 * -------------------------------------------------- * 涓昏鎻愪緵鏃ユ湡涓?970-01-01鍚庣殑澶╂暟鐨勮漿鎹㈠拰鍒板瓧絎︿覆鐨勮漿鎹? * -------------------------------------------------- * * @author iwinyeah 鏉庢案瓚? * @version 1.0.0 * */ import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class DateUtil { private static Calendar _calendar = Calendar.getInstance(); // 鐢ㄤ簬鏃ユ湡璁$畻 private static long MSEC_EVERYDAY = 86400000L; // 涓澶╃殑寰鏁? private static int rawOffset = TimeZone.getDefault().getRawOffset(); /** * 灝嗘棩鏈熻漿鎹負1970-01-01鍚庣殑澶╂暟 * * @param Date * theDate 瑕佽綆楀ぉ鏁扮殑鏃ユ湡 * @return int 鎵浼犲叆鏃ユ湡涓?970-01-01鐩稿樊鐨勫ぉ鏁? */ public static int dateToDay(Date theDate) { return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY); } /** * 灝?970-01-01鍚庣殑澶╂暟杞崲涓烘棩鏈? * * @param int * 瑕佸彇鐨勬棩鏈熶笌1970-01-01鐩稿樊鐨勫ぉ鏁? * @return Date theDate 涓?970-01-01鐩稿樊鐩稿簲澶╂暟鐨勬棩鏈? */ public static Date dayToDate(int day) { return new Date(day * MSEC_EVERYDAY); } /** * 鍙栦粖澶╀笌1970-01-01鐩稿樊鐨勫ぉ鏁? * * @return int 鍙栦粖澶╀笌1970-01-01鐩稿樊鐨勫ぉ鏁? */ public static int toDay() { return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY); } /** * 灝嗘棩鏈熻漿鎹負騫存湀鏃ュ瓧絎︿覆 * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return String 瀵瑰簲鏃ユ湡騫存湀鏃ュ艦寮忕殑瀛楃涓? */ public static String getYMD(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR) % 100 + "/" + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1) + "/" + (_calendar.get(Calendar.DATE) > 9 ? "" : "0") + _calendar.get(Calendar.DATE); } /** * 灝嗘棩鏈熻漿鎹負騫存湀瀛楃涓? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return String 瀵瑰簲鏃ユ湡騫存湀褰㈠紡鐨勫瓧絎︿覆 */ public static String getYM(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR) + "/" + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1); } /** * 灝嗘棩鏈熻漿鎹負鏈堟棩瀛楃涓? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return String 瀵瑰簲鏃ユ湡鏈堟棩褰㈠紡鐨勫瓧絎︿覆 */ public static String getMD(int theDay) { _calendar.setTime(dayToDate(theDay)); return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (_calendar.get(Calendar.MONTH) + 1) + "/" + (_calendar.get(Calendar.DATE) > 9 ? "" : "0") + _calendar.get(Calendar.DATE); } /** * 灝嗘棩鏈熻漿鎹負褰撴湀涓鍙? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return int 瀵瑰簲鏃ユ湡鎵鍦ㄦ湀浠界涓澶╀笌1970-01-01鐩稿樊鐨勫ぉ鏁? */ public static int getMonthFirstDay(int theDay) { _calendar.setTime(dayToDate(theDay)); _calendar.set(Calendar.DAY_OF_MONTH, 1); return (int) (dateToDay(_calendar.getTime())); } /** * 鍙栨棩鏈熸墍鍦ㄥ勾浠? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return int 瀵瑰簲鏃ユ湡鎵鍦ㄥ勾浠? */ public static int getYear(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.YEAR); } /** * 鍙栨棩鏈熸墍鍦ㄦ湀浠? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return int 瀵瑰簲鏃ユ湡鎵鍦ㄦ湀浠? */ public static int getMonth(int theDay) { _calendar.setTime(dayToDate(theDay)); return _calendar.get(Calendar.MONTH); } /** * 鍙栨棩鏈熸墍鍦ㄥ懆嬈? * * @param int * theDay 涓?970-01-01鐩稿樊鐨勫ぉ鏁? * @return int 瀵瑰簲鏃ユ湡鎵鍦ㄥ懆嬈? */ public static int getWeek(int theDay) { // 1971-01-03鏄槦鏈熸棩,浠庤鏃ュ紑濮嬭綆楀懆嬈? _calendar.setTime(dayToDate(theDay)); return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L); } }
// 鏂規硶涓 public class firstManager implements Runnable { public void runTask() { (new Thread(this)).start(); } public void run() { System.out.println("\nfirst thread method!"); // Do some thing ... } } // 鏂規硶浜? public class secondManager { private BackTask backTask; private Timer timer; public secondManager() { backTask = new BackTask(); timer = new Timer(); } public void runTask() { timer.schedule(backTask, 0); } private class BackTask extends TimerTask { public void run() { System.out.println("\nsecond thread method!"); // Do some thing ... } } } // 鏂規硶涓? public class thirdManager { private BackTask backTask; private int cmd = 0; public thirdManager() { backTask = new BackTask(); backTask.start(); } public void runTask() { synchronized (backTask) { cmd = 1; backTask.notify(); } } private class BackTask extends Thread { public void run() { while (true) { try { if (cmd == 0) { synchronized (this) { wait(); } continue; } System.out.println("\nthird thread method!"); // Do some thing ... } catch (Exception e) { } cmd = 0; } } } } // 鐢ㄤ緥 public void main(){ firstManager man1 = new firstManager(); secondManager man2 = new secondManager(); thirdManager man3 = new thirdManager(); man1.runTask(); man2.runTask(); man3.runTask(); }