Java5.0 부터 여러가지를 수용하더니 잼난 기능을 하나 추가된듯.

변수를 가변으로 받을 수 있게 됨. 뭐.. 결국 array 로 취급하기는 하지만.. 알아두면 코드 사이즈를 줄이는데 도움이 되지 않을까? 아니면 말구~ 쩌ㅃ

package demo.vat;

/**
 * 자바 가변 인자 테스트
 * jdk 1.5 이상 부터 가능
 *
 * @author bbaeggar
 *
 */
public class VariableArgumentTypeDemo {

    public void test(int type, String... list) {
   
        System.out.println("type : " + type);
       
        for(int i = 0;i<list.length;i++) {
            System.out.println("list[" + i + "] : " + list[i]);
        }
    }
   
    public static void main(String[] args) {
        new VariableArgumentTypeDemo().test(0, "a", "b", "c");
    }
}

- 코드를 자세히 보면 결국 array 로 취급하기 때문에 argument 는 같은 타입이고, 마지막에 선언해야 한다는 제약이 있는듯.

Posted by 짱가쟁이
InputStream 을 문자열로 반환하는 넘.
public static String readStringFromStream(InputStream in) throws IOException {   
        StringBuilder sb = new StringBuilder(1024);
   
        for (int i = in.read(); i != -1; i = in.read()) {
            sb.append((char) i);
        }   
        in.close();   
        return sb.toString();
}

InputStream 을 byte array로 반환하는 넘
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

public static byte[] readBytesFromStream(InputStream in) throws IOException {
        int i = in.available();
        if (i < DEFAULT_BUFFER_SIZE) {
            i = DEFAULT_BUFFER_SIZE;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
        copy(in, bos);
        in.close();
        return bos.toByteArray();
}

public static int copy(final InputStream input, final OutputStream output) throws IOException {
         return copy(input, output, DEFAULT_BUFFER_SIZE);
}
     
public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
        int avail = input.available();
        if (avail > 262144) {
            avail = 262144;
        }
        if (avail > bufferSize) {
            bufferSize = avail;
        }
        final byte[] buffer = new byte[bufferSize];
        int n = 0;
        n = input.read(buffer);
        int total = 0;
        while (-1 != n) {
            output.write(buffer, 0, n);
            total += n;
            n = input.read(buffer);
        }
        return total;
}

Posted by 짱가쟁이
매번 까먹고 다시 만들고.. 머릿속의 지우개는 어떻게 하지 못하는 건지.... 쩌ㅃ~

 /**
     *
     * @param format
     *             ex> format : yyyyMMddHHmmssSSS
     * @return
     */
    public static String getCurrentTime_0(String format) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);

        return dateFormat.format(calendar.getTime());
    }

    public static String getCurrentTime_1(String format) {
        DateFormat df = new SimpleDateFormat(format); // HH=24h, hh=12h
         return df.format(System.currentTimeMillis());
    }

- format 을 맞춰주면 오늘 날짜/시간을 가져온다. 밀리세컨드 까징..
- 뭐 나오세컨드 까지 나온듯 싶지만.. 고넘은 벌써 까먹음.. 다음에 기회가 생기면 그때.. 쩌ㅃ~
Posted by 짱가쟁이
String 클래스에 repalceAll() 요넘.. '$' 특수문자 변환에 걸려서리.. 사용하기 존내 짜증남.. 예전에 어떻게 처리 한거 같기도 한데.. 마찮가지로 머릿속의 지우개가 문제인듯.

public static String replaceAll(String buffer, String src, String dst)
{
    if (buffer == null)
        return null;
    if (src == null || buffer.indexOf(src) < 0)
        return buffer;

    int bufLen = buffer.length();
    int srcLen = src.length();
    StringBuffer result = new StringBuffer();

    int i = 0;
    int j = 0;
    for (; i < bufLen;)
    {
        j = buffer.indexOf(src, j);
        if (j >= 0)
        {
            result.append(buffer.substring(i, j));
            result.append(dst);

            j += srcLen;
            i = j;
        }
        else
            break;
    }
    result.append(buffer.substring(i));
    return result.toString();
}

Posted by 짱가쟁이

- Sample
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(9);
nf.setGroupingUsed(false);

nf.format((double)228588.0/(double)1000000000.0);

- Result
0.000228588

'java > util' 카테고리의 다른 글

Calendar, SimpleDateFormat 사용하자  (0) 2010.06.28
[replaceAll] - 문자열 치환  (0) 2010.06.28
[정규식] - Pattern 을 사용한 Validate  (0) 2010.06.25
[정규식] - 한글 검사  (0) 2010.06.25
[정규식] - 초성 검사  (0) 2010.06.25
Posted by 짱가쟁이
입력값을 검사해서 한글이면 따로 조건을 줘야할때 사용하면 좋다.

public static boolean isKorean(String src) {
    String regEx = "[ㄱ-ㅎㅏ-ㅣ가-힣]*";
    return Pattern.matches(regEx, src);
}

Posted by 짱가쟁이
이번 프로젝트는 나름 알아가는 재미가 있는듯. 입력받은 문자열이 초성만 있는지 검사하는 정규식.

public static boolean isChosung(String src) {
       String regEx = "[ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ]*";
    return Pattern.matches(regEx, src);
}


Posted by 짱가쟁이
이전버튼 1 2 3 이전버튼