2012-08-14 2 views
3

캐릭터 파일이 1.99GB입니다. 자, 예를 들어 위치 90에서 190, 10에서 110, 50000에서 50100 등 (각각 100 자 길이)에서 무작위로 해당 파일에서 수백만 개의 하위 시퀀스를 추출하고 싶습니다. 나는 다음 코드를 사용 나는 보통 사용 할자바 - 문자 버퍼 문제

, 코드 위의 파일이 오류를 제공 1.99 기가 바이트에 대한

FileChannel channel = new RandomAccessFile(file , "r").getChannel(); 
    ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
    Charset chars = Charset.forName("ISO-8859-1"); 
    CharBuffer cbuf = chars.decode(buffer); 
    String sub = cbuf.subSequence(0, 100).toString(); 

    System.out.println(sub); 

그러나, 그래서

java.lang.IllegalArgumentException 
     at java.nio.CharBuffer.allocate(CharBuffer.java:328) 
     at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:792) 
     at java.nio.charset.Charset.decode(Charset.java:791) 

,

FileChannel channel = new RandomAccessFile(file , "r").getChannel(); 
CharBuffer cbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()).asCharBuffer() ; 
String sub = cbuf.subSequence(0, 100).toString(); 

System.out.println(sub); 

하는 위의 오류를 제공하지 않지만 출력을 반환합니다 :

ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ä¹ 
"011111000000가 ........"

이 가

아무도이 일이 일어나고 그 해결 방법에 왜 저를 도와 드릴까요해야

?

+0

"IllegalArgumentException - 용량이 음수 인 경우"문서를 기반으로합니다. allocate()를 사용하여 용량을 명시 적으로 설정해보십시오. http : //docs.oracle.com/javase/1.5.0/docs/api/java/nio/CharBuffer.html – kosa

+0

@thinksteep, 명시 적으로 20 억으로 설정했습니다. , IllegalArgumentException 예외를줍니다. – Arpssss

+0

JVM -Xmx 설정에 할당 된 RAM은 무엇입니까? – kosa

답변

2

난 그냥 추측하고있어,하지만 난 그것을 뒤에서 당신을 위해 거대한CharBuffer를 할당 할 때 Charset.decode(ByteBuffer)이 실패라고 생각한다. 다시 말하지만 이것은 단지 직감이지만, decode 메서드는 버퍼의 현재 위치에서 한계까지만 바이트를 디코딩하므로 이와 같은 작업을 수행 할 수 있습니다. decode 메소드에 의해 반환되는 CharBuffer의 (문자)

ByteBuffer buffer = ... 
Charset charset = ... 

buffer.position(0); 
buffer.limit(100); 

System.out.println(charset.decode(buffer)); 

용량은, 당신은하지 않았다 때문에

(100)는 (보조 노트에, 나는 두 번째 시도가 잘못된 출력을 제공합니다 생각됩니다 특정 문자 집합을 사용하여 CharBuffer을 디코딩하십시오.