자바 프로 그래 밍 사상 자바 I/O 시스템 장절 읽 기 노트
-------------------------------------------------------------------------------------------------------------------------------------------
프로 그래 밍 언어의 I/O 라 이브 러 리 에 서 는 스 트림 개념 을 자주 사용 하지만 실제 I/O 장치 에서 데 이 터 를 처리 하 는 디 테 일 을 차단 합 니 다.자바 에 서 는 단일 클래스 를 사용 하여 스 트림 대상 을 만 드 는 것 이 아니 라 여러 대상 을 겹 쳐 서 원 하 는 기능 을 제공 합 니 다(장식 기 모드 를 사용 합 니 다).InputStream 과 OutputStream 은 입력 과 출력 에 쓰 인 다.자바 1.1 에서 Reader 와 Writer 는 어댑터 를 이용 하여 국제 화 를 실현 했다.오래된 I/O 흐름 계승 결 과 는 8 개의 바이트 흐름 만 지원 하기 때문에 16 개의 유 니 코드 문 자 를 잘 처리 할 수 없다.
파일 작업 의 클래스:FileInputReader,FileOutputReader,RandomAccessFile.그 중에서 RandomAccessFile 은 스스로 독립 된 유형 으로 읽 고 쓸 수 있 지만 모두 자신의 내부 에서 이 루어 진 것 으로 입 출력 흐름 의 차원 구 조 를 계승 하지 않 았 다.JDK 1.4 에서 대부분의 기능 은 nio 메모리 맵 파일 로 대체 되 었 습 니 다.
기본 파일 출력:FileWriter 대상 은 파일 에 데 이 터 를 기록 할 수 있 습 니 다.실제로 저 희 는 Buffered Writer 로 포장 하여 버퍼 출력 을 사용 합 니 다.포맷 체 제 를 제공 하기 위해 PrintWriter 로 장식 합 니 다.예 를 들 어 new PrintWriter(new Buffered Writer(file))는 J2SE 5 에서 보조 구조 기 를 사용 하여 new PrintWriter(file)로 간략하게 쓸 수 있다.
파이프 흐름:Piped InputStream,Piped OutputStream,Piped Reader 및 Piped Writer,그들의 가 치 는 다 중 스 레 드 에서 만 나타 날 수 있 습 니 다.파이프 흐름 은 작업 간 의 통신 에 사용 되 기 때 문 입 니 다.
-------------------------------------------------------------------------------------------------------------------------------------------
new I/O:속 도 를 높이 기 위해 새로운 버 전의 JDK 에서 오래된 I/O 도 nio 를 사용 하여 재 구현 하여 성능 을 향상 시 키 는 것 이 목적 입 니 다.속도 의 향상 은 사용 하 는 구조 가 운영 체제 의 실행 I/O 방식 인 채널 과 버퍼 에 더욱 가깝다.
getChannel()방법 을 사용 하면 FileChannel 이 생 성 됩 니 다.채널 은 상당히 기본 적 인 것 입 니 다.읽 기와 쓰기 에 사용 되 는 ByteBuffer 를 전송 할 수 있 고 파일 의 일부 영역 을 잠 그 고 독점 적 인 접근 에 사용 할 수 있 습 니 다(뒤에 언급 되 어 있 습 니 다).
- import java.nio.*;
- import java.nio.channels.*;
- import java.io.*;
-
- public class GetChannel {
- private static final int BSIZE = 1024;
- public static void main(String[] args) throws Exception {
- // Write a file:
- FileChannel fc =
- new FileOutputStream("data.txt").getChannel();
- fc.write(ByteBuffer.wrap("Some text ".getBytes()));
- fc.close();
- // Add to the end of the file:
- fc = new RandomAccessFile("data.txt", "rw").getChannel();
- fc.position(fc.size()); // Move to the end
- fc.write(ByteBuffer.wrap("Some more".getBytes()));
- fc.close();
- // Read the file:
- fc = new FileInputStream("data.txt").getChannel();
- ByteBuffer buff = ByteBuffer.allocate(BSIZE);
- fc.read(buff);
- buff.flip();
- while(buff.hasRemaining())
- System.out.print((char)buff.get());
- }
- }
-
바이트 를 ByteBuffer 에 저장 하 는 방법 은 두 가지 가 있 습 니 다.하 나 는 put 방법 으로 하나 이상 의 바이트 나 기본 데이터 형식 을 직접 채 우 는 것 입 니 다.다른 하 나 는 이미 존재 하 는 바이트 배열 을 warp 방법 으로 ByteBuffer 에 포장 하 는 것 이다.읽 기 전용 접근 에 대해 서 는 정적 인 allocate 방법 으로 ByteBuffer 를 할당 해 야 합 니 다.nio 의 목 표 는 대량의 데 이 터 를 빠르게 이동 하 는 것 입 니 다.따라서 ByteBuffer 의 크기 는 매우 중요 합 니 다.실제 실행 되 는 프로그램 에서 테스트 를 통 해 최 적 치 를 찾 아야 합 니 다.더 높 은 속 도 를 내 려 면 allocateDirect()가 아 닌 allocate()를 사용 하여 운영 체제 와 더욱 높 은 결합 성 을 가 진'직접'버퍼 를 만 드 는 것 이 방법 이지 만 효 과 는 실제 테스트 가 필요 합 니 다.
- import java.nio.*;
- import java.nio.channels.*;
- import java.io.*;
-
- public class ChannelCopy {
- private static final int BSIZE = 1024;
- public static void main(String[] args) throws Exception {
- if(args.length != 2) {
- System.out.println("arguments: sourcefile destfile");
- System.exit(1);
- }
- FileChannel in = new FileInputStream(args[0]).getChannel();
- FileChannel out = new FileOutputStream(args[1]).getChannel();
- // , 2
- // in = transferTo(0, in.size(), out); out = transferFrom(in, 0, in.size());
- ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
- while(in.read(buffer) != -1) {
- buffer.flip(); // Prepare for writing
- out.write(buffer);
- buffer.clear(); // Prepare for reading
- }
- }
- }
Buffer Writer 를 char 형 으로 바 꾸 는 것 은 매우 불편 합 니 다.우 리 는 CharBuffer 의 toString()방법 을 이용 하여 String 형 으로 바 꾸 는 것 이 훨씬 편리 합 니 다.
- import java.nio.*;
- import java.nio.channels.*;
- import java.nio.charset.*;
- import java.io.*;
-
- public class BufferToText {
- private static final int BSIZE = 1024;
- public static void main(String[] args) throws Exception {
- FileChannel fc =
- new FileOutputStream("data2.txt").getChannel();
- fc.write(ByteBuffer.wrap("Some text".getBytes()));
- fc.close();
- fc = new FileInputStream("data2.txt").getChannel();
- ByteBuffer buff = ByteBuffer.allocate(BSIZE);
- fc.read(buff);
- buff.flip();
- // Doesn't work:
- System.out.println(buff.asCharBuffer());
- // Decode using this system's default Charset:
- buff.rewind();
- String encoding = System.getProperty("file.encoding");
- System.out.println("Decoded using " + encoding + ": "
- + Charset.forName(encoding).decode(buff));
- // Or, we could encode with something that will print:
- fc = new FileOutputStream("data2.txt").getChannel();
- fc.write(ByteBuffer.wrap(
- "Some text".getBytes("UTF-16BE")));
- fc.close();
- // Now try reading again:
- fc = new FileInputStream("data2.txt").getChannel();
- buff.clear();
- fc.read(buff);
- buff.flip();
- System.out.println(buff.asCharBuffer());
- // Use a CharBuffer to write through:
- fc = new FileOutputStream("data2.txt").getChannel();
- buff = ByteBuffer.allocate(24); // More than needed
- buff.asCharBuffer().put("Some text");
- fc.write(buff);
- fc.close();
- // Read and display:
- fc = new FileInputStream("data2.txt").getChannel();
- buff.clear();
- fc.read(buff);
- buff.flip();
- System.out.println(buff.asCharBuffer());
- }
- }
-
-------------------------------------------------------------------------------------------------------------------------------------------
메모리 맵 파일:메모리 에 넣 을 수 없 는 파일 을 만 들 고 수정 할 수 있 습 니 다.
- import java.nio.*;
- import java.nio.channels.*;
- import java.io.*;
- import static net.mindview.util.Print.*;
-
- public class LargeMappedFiles {
- static int length = 0x8FFFFFF; // 128 MB
- public static void main(String[] args) throws Exception {
- MappedByteBuffer out =
- new RandomAccessFile("test.dat", "rw").getChannel()
- .map(FileChannel.MapMode.READ_WRITE, 0, length);
- for(int i = 0; i < length; i++)
- out.put((byte)'x');
- print("Finished writing");
- for(int i = length/2; i < length/2 + 6; i++)
- printnb((char)out.get(i));
- }
- }
-
파일 잠 금 추가:FileChannel 에 tryLock()과 lock()을 호출 하면 전체 파일 의 FileLock 을 얻 을 수 있 습 니 다.try Lock()을 시작 하 는 것 은 비 차단 식 입 니 다.자 물 쇠 를 가 져 오 려 고 노력 하지만 얻 지 못 하면 직접 놓 는 방법 을 되 돌려 줍 니 다.lock()은 자 물 쇠 를 얻 을 때 까지 스 레 드 를 막 거나 lock()의 스 레 드 가 중단 되 거나 lock()의 채널 을 호출 하여 닫 아야 합 니 다.
- import java.nio.channels.*;
- import java.util.concurrent.*;
- import java.io.*;
-
- public class FileLocking {
- public static void main(String[] args) throws Exception {
- FileOutputStream fos= new FileOutputStream("file.txt");
- FileLock fl = fos.getChannel().tryLock();
- if(fl != null) {
- System.out.println("Locked File");
- TimeUnit.MILLISECONDS.sleep(100);
- fl.release();
- System.out.println("Released Lock");
- }
- fos.close();
- }
- }
-
맵 파일 의 부분 잠 금 추가:
- import java.nio.*;
- import java.nio.channels.*;
- import java.io.*;
-
- public class LockingMappedFiles {
- static final int LENGTH = 0x8FFFFFF; // 128 MB
- static FileChannel fc;
- public static void main(String[] args) throws Exception {
- fc =
- new RandomAccessFile("test.dat", "rw").getChannel();
- MappedByteBuffer out =
- fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);
- for(int i = 0; i < LENGTH; i++)
- out.put((byte)'x');
- new LockAndModify(out, 0, 0 + LENGTH/3);
- new LockAndModify(out, LENGTH/2, LENGTH/2 + LENGTH/4);
- }
- private static class LockAndModify extends Thread {
- private ByteBuffer buff;
- private int start, end;
- LockAndModify(ByteBuffer mbb, int start, int end) {
- this.start = start;
- this.end = end;
- mbb.limit(end);
- mbb.position(start);
- buff = mbb.slice();
- start();
- }
- public void run() {
- try {
- // Exclusive lock with no overlap:
- FileLock fl = fc.lock(start, end, false);
- System.out.println("Locked: "+ start +" to "+ end);
- // Perform modification:
- while(buff.position() < buff.limit() - 1)
- buff.put((byte)(buff.get() + 1));
- fl.release();
- System.out.println("Released: "+start+" to "+ end);
- } catch(IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.