'JAVA/JSP/Java'에 해당되는 글 13건
- 2009.01.06 CharsetTest
import java.nio.charset.*;
import java.nio.channels.*;
import java.nio.*;
import java.io.*;
public class CharsetTest {
private static String fileName = "temp.tmp";
private static String usedCharset = "EUC-JP";
private static String testText = "従来の中高年歴史好き以外にも、漫画やゲームをきっかけに若者もハマるという戦国武将。";
public static void main(String[] args) {
FileChannel channel = null;
try {
Charset charset = Charset.forName(usedCharset);
ByteBuffer inBuf = charset.encode(testText);
FileOutputStream out = new FileOutputStream(fileName);
channel = out.getChannel();
channel.write(inBuf);
ByteBuffer outBuf = ByteBuffer.allocate(512);
FileInputStream in = new FileInputStream(fileName);
channel = in.getChannel();
channel.read(outBuf);
outBuf.flip();
CharBuffer charBuffer = charset.decode(outBuf);
//출력 해봐라...
System.out.println(charBuffer.toString());
} catch(IllegalCharsetNameException ex) {
System.out.println("잘못된 캐릭터셋 이름: " + usedCharset);
} catch(UnsupportedCharsetException ex) {
System.out.println("지원하지 않는 캐릭터셋: " + usedCharset);
} catch(IOException ex) {
System.out.println("입출력 예외: " + ex.getMessage());
} finally {
if (channel != null){
try {
channel.close();
}
catch(IOException ex){}
}
}
}
}
'JAVA/JSP > Java' 카테고리의 다른 글
Connection pooling (0) | 2008.10.06 |
---|---|
DB Connection (0) | 2008.10.06 |
MouseEvent/ItemEvent (0) | 2008.07.14 |
Exception/ Sort (0) | 2008.07.14 |
instanceof/StringTokenizer/innerclass (0) | 2008.07.14 |