본문 바로가기

Java/[Basic Source]

자바스레드 샘플코드 자바스레드 샘플코드 class ThreadTest implements Runnable{ private int loopCount; private String code_name; public ThreadTest(String code_name){ this.code_name = code_name; } public void run(){ loopCount = 0; try { while ( !Thread.currentThread().isInterrupted() && //외부 종료용 this.loopCount < 10) { //내부 종료 용 System.out.println("Thread is alive.." + loopCount + this.code_name); // 0.5초간 멈춤. Thread.sleep(500)..
문자열 치환 - 재귀호출 /** * 재귀 호출로 된 replace * @param str : 원본 문자열 * @param old : 바꿀문자 * @param new : 바뀔문자 * @return */ public static String replace(String str, String old, String new) { int pos = str.indexOf(old); if(pos==-1) return str; return str.substring(0, pos) + new + replace(str.substring(pos+old.length()),old, new); }
constant(상수) 선언하기 자바에서 변수로 선언된 것들중에 'public static final' 로 선언된 변수가 constant(상수) 라 한다. 일반적으로 class에서 선언할때는 다음과 같이 선언 할 수 있다. static final int MENU_ITEM = 12; static final float PI = (float) 22 / 7;이렇게 선언된 상수는 해당 Class에서 항상 동일한 값으로 존재한다. 만약 해당 Class가 아닌 다른 Class에서 값을 가져다 사용하기를 원할 경우에는 앞에 public을 붙여 준다. public static final int MENU_ITEM = 12; public static final float PI = (float) 22 / 7;그러면, 다른 클래스에서 참조가 가능하다.
자바 주석 처리 방법 자바에서 주석을 처리 하는 방법은 Single line of Comment 방식과 Multiple lines of Comment 방식이 있다. 뭐 그러게 어려운게 아니니 바로 소스를 보시면 이해가 될듯.. 첫째. Single line of Comment 주석이 한줄로 처리가 될 경우 사용한다. // Single line of comment public class MainClass{ public static void main(String[] arg){ System.out.println("test"); //Single line of comment } } 둘째, Multiple lines of Comment 주석이 한줄로 처리가 안되고 여러줄일 경우 사용한다. /* multiple lines of commen..
FileChannel을 이용한 파일 copy FileChannel을 이용하여 파일을 복사하게 되면, 일반적으로 사용하는 InputStream을 이용해서 loop를 돌면서 특정 buffer size만큼 읽어서 파일에 write하는 방식보다 좋은 성능을 낼수 있네요... import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; public class FChannel { public FChannel() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated..