import java.io.FileInputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Scanner;
public class NIOFileReaderTest
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
String fileName = "input.txt";
FileChannel channel = null;
MappedByteBuffer byteBuffer = null;
Scanner scanner = null;
try
{
channel = new FileInputStream(fileName).getChannel();
byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
CharBuffer charBuffer = decoder.decode(byteBuffer);
scanner = new Scanner(charBuffer).useDelimiter("\n");
while(scanner.hasNext())
{
System.out.println(scanner.next());
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(scanner != null) scanner.close();
if(channel != null) channel.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
'백엔드기술 > 개발언어' 카테고리의 다른 글
WEB Socket (0) | 2013.01.05 |
---|---|
Tika 소개 - 유용한 Parser 라이브러리 (1) | 2012.12.14 |
정보보호 진흥원 SEED 암호화/ SEED 알고리즘 (0) | 2012.12.04 |