2009년 10월 27일 화요일

[Java] 임의의 문자 생성기- 알파벳과 숫자로 된 임의의 키나 문자 생성기(Random letter generator)

가끔 임의의 키를 생성할 필요가 있다.
그럴땐 아래의 코드를 사용하면 쉽게 처리할 수 있을 것이다.



package random.string;

import java.util.Random;

public class RandomString {
public RandomString() {
}
public static void main(String args[]) {
RandomString rs=new RandomString();
String randomKey=rs.generateData(20);
System.out.println(randomKey);
}
final String dummyString="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlmnopqrstuvwxyz";
final Random random=new Random();
public String generateData(int loopCount) {
// char 48=0 65=A 90=Z 97=a 122=z
StringBuilder tempBuilder=new StringBuilder(100);
int randomInt;
char tempChar;
for(int loop=0;loop<loopCount;loop++) {
randomInt=random.nextInt(61);
tempChar=dummyString.charAt(randomInt);
tempBuilder.append(tempChar);
}
//System.out.println(tempBuffer);
return tempBuilder.toString();
}
}


[자바 문자 입력 받기] Java Console

JDK 6.0 이상부터 추가된 클래스중에 Console이라는 클래스가 있다.
이제부터는 자바 커맨드 창에서 문자를 입력받을때,
복잡하게 stream을 구현할 필요가 없어 졌다.



package console;

import java.io.Console;

public class ConsoleTest {
public static void main(String args[]) {
Console console=System.console();
if(console!=null) {
String id=console.readLine("%s","ID:");
char[] password=console.readPassword("%s", "Password:");
if(id!=null & password!=null) {
System.out.println("ID="+id);
System.out.println("PW="+new String(password));
}
} else {
System.out.println("Console is null");
}
}
}




2009년 6월 30일 화요일

[Java File] 특정 디렉토리에 있는 파일 목록을 읽어내기




String path="C:\";
File dirFile=new File(path);
File []fileList=dirFile.listFiles();
for(File tempFile : fileList) {
  if(tempFile.isFile()) {
    String tempPath=tempFile.getParent();
    String tempFileName=tempFile.getName();
    System.out.println("Path="+tempPath);
    System.out.println("FileName="+tempFileName);
    /*** Do something withd tempPath and temp FileName ^^; ***/
  }

}



이것도 설명은 나중에...
뭐 그냥 소스 보면 설명은 필요 없지머...

2009년 6월 27일 토요일

[HTML] 쉽게 HTML 화면을 구성하기 위한 Fieldset



<Fieldset>
  <legend>이게 제목</legend>
  본문 부분<BR>본분 부분 2
</Fieldset>



요렇게 하면 제목과 본문이 나타난다.

이 블로그의 바탕색이 검은색이라 - -;

2009년 6월 25일 목요일

[JSP] JSP의 매일 까먹는 태그들: 페이지 인코딩 지정, import, include 구문

 <%@ page contentType="text/html; charset=euc-kr" %>

HTML 에서는

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">

 


<%@ page import="java.util.Map" %>

동적 include
<jsp:include page="a.jsp" flush="true" />

정적 include
<%@ include file="SystemCallData3.jsp" %>

 <%!
변수 선언
메소드 선언
%>

 

설명은 나중에 ^^;

 

2009년 6월 16일 화요일

[Java DecimalFormat] 자바에서 소수점 단위의 출력을 선언하여 사용하기

Java에서 숫자를 출력할 때, 기본적으로는 갖고 있는 값을 모두 출력한다.

하지만, 소수점 2자리를 짜른다던지, 1000 이상 단위의 값에 comma를 추가할 경우에는 NumberFormat 이나 DecimalFormat을 사용하는 것이 좋다.

DecimalFormat df=new DecimalFormat("###,##0.00");
public String format(double number) {
  return df.format(number);
}

위와 같이 사용하면

0.2, 1.5, 1123.52

는 각각

0.20, 1.50, 1,123.52 로 표시된다.


#이나 0 이나 모두 숫자를 표시하는데,

#은 0일 경우 숫자를 표시하지 않고, 0은 0일 경우에도 숫자를 표시한다.

 

 

 

2009년 6월 13일 토요일

About "Code Samples"

Code Samples 블로그에는 코드 샘플을 관리합니다.

전혀 저작권에 문제가 되지 않는,
개발자들이 매번 참고해야만 하는,
10줄 이하의 짤막한 코드들을 정리합니다.

저작권에 문제되는 그런 코드가 있을 일은 거의 없겠죠?