OutputStream-InputStream-FileOutputStream-FileInputStream-BufferedOutputStream-BufferedInputStream

1. Output Stream 클래스 (java. io)
개요: OutputStream 클래스 는 프로그램 에서 데 이 터 를 미디어 에 기록 하 는 데 사 용 됩 니 다.
정의: public abstract class OutputStream extends Object implements Closeable, Flushable
OutputStream 클래스: 이 추상 클래스 는 출력 바이트 흐름 을 나타 내 는 모든 종류의 초 클래스 입 니 다.
메모: io 흐름 은 폴 더 를 조작 할 수 없습니다. (폴 더 에 내용 을 직접 쓸 수 없습니다.)
작업 의 최소 단 위 는 bit 입 니 다.
저장 소 의 최소 단 위 는 byte 입 니 다.
1byte = 8 bit
2. FileOutputStream 클래스 (java. io)
개요: OutputStream 상용 하위 클래스:;
구조 방법:
FileOutputStream (String name): 지정 한 이름 을 가 진 파일 에 데 이 터 를 기록 하 는 출력 파일 흐름 을 만 듭 니 다.
Name 은 파일 의 경 로 를 표시 합 니 다. 상대 적일 수도 있 고 절대적 일 수도 있 습 니 다!
FileOutputStream (File file): 지정 한 File 대상 에 게 표 시 된 파일 에 데 이 터 를 기록 하 는 파일 출력 흐름 을 만 듭 니 다.
다시 쓰기:
FileOutputStream (File file, boolean append): 지정 한 File 대상 에 표 시 된 파일 에 데 이 터 를 기록 하 는 파일 출력 흐름 을 만 듭 니 다.
append 를 전달 해 야 하 는 값 은: true 입 니 다.
FileOutputStream (String name, boolean append): 지정 한 name 이 있 는 파일 에 데 이 터 를 기록 하 는 출력 파일 흐름 을 만 듭 니 다.
줄 바 꾸 기:
문자 흐름 을 사용 할 때 "\ r" 를 직접 쓸 수 있 습 니 다.
바이트 흐름 을 사용 할 때 '\ r' 의 바이트 배열 을 써 야 합 니 다.
상용 방법:
public void write (byte [] b) {}: b. length 바이트 가 지정 한 byte 배열 에서 이 파일 출력 흐름 에 기 록 됩 니 다.
public void write (byte [] b, int off, int len) {}: 지정 한 byte 배열 에서 오프셋 off 에서 시작 하 는 len 바이트 를 이 파일 출력 흐름 에 기록 합 니 다.
public void write (int b) {}: 지정 한 바이트 를 이 파일 의 출력 흐름 에 기록 합 니 다.
public void close () {}: 이 파일 의 출력 흐름 을 닫 고 이 흐름 과 관련 된 모든 시스템 자원 을 방출 합 니 다.
String 클래스 의 일반적인 방법:
public byte [] getBytes () {}: 플랫폼 의 기본 문자 집합 을 사용 하여 이 String 을 byte 시퀀스 로 인 코딩 하고 결 과 를 새로운 byte 배열 에 저장 합 니 다.
3. InputSteam 클래스 (java. io)
정의: public abstract class InputStream extends Object implements Closeable
개술: InputStream 이라는 추상 류 는 바이트 입력 흐름 을 나타 내 는 모든 종류의 초 류 입 니 다.
4. FileInputStream 클래스 (java. io)
개요: InputStream 류 의 상용 하위 클래스;FileInputStream 류 는 파일 에서 바이트 데 이 터 를 읽 는 데 사용 되 는 흐름 으로 읽 은 내용 을 10 진 데이터 로 되 돌려 줍 니 다.
10 진법 의 데 이 터 를 대응 하 는 문자 로 바 꾸 려 면 프로그래머 가 메타 변환 을 대조 해 야 합 니 다.
구체 적 인 전환 방식 은 두 가지 가 있다.
1: char 형식의 문자 로 직접 강하 게 변환 하기;(한 숫자 에 적용)
2: String 류 의 구조 방법 사용 하기;(하나 이상 의 숫자 에 적용)
구조 방법:
FileInputStream (String name): 실제 파일 로 연결 하 는 연결 을 열 어 FileInputStream 을 만 듭 니 다.
이 파일 은 파일 시스템 의 경로 이름 name 을 통 해 지정 합 니 다.
FileInputStream (File file): 실제 파일 로 의 연결 을 열 어 FileInputStream 을 만 듭 니 다.
이 파일 은 파일 시스템 의 File 대상 file 을 통 해 지 정 됩 니 다.
상용 방법:
public int read () {}: 이 입력 흐름 에서 데이터 바이트 를 읽 습 니 다.읽 은 이 바이트 의 코드 값 을 되 돌려 줍 니 다!
public int read (byte [] b) {}: 이 입력 흐름 에서 최대 b. length 바이트 의 데 이 터 를 byte 배열 에 읽 습 니 다.읽 은 바이트 의 개 수 를 되 돌려 줍 니 다!
public int read (byte [] b, int off, int len) {}: 이 입력 흐름 에서 최대 len 바이트 의 데 이 터 를 byte 배열 에 읽 습 니 다.읽 은 바이트 의 개 수 를 되 돌려 줍 니 다!
public void close () {}: 이 파일 의 입력 흐름 을 닫 고 이 흐름 과 관련 된 모든 시스템 자원 을 방출 합 니 다.
5. BufferedOutputStream 클래스 (java. io)
정의: public class BufferedOutputStream extends FilterOutputStream
OutputStream 에 속 하 는 손자 류 입 니 다.
구조 방법: BufferedOutputStream (OutputStream out) 은 지정 한 바 텀 출력 흐름 에 데 이 터 를 기록 하기 위해 새로운 버퍼 출력 흐름 을 만 듭 니 다.
6. BufferedInputStream 클래스 (java. io)
정의: public class BufferedInputStream extends FilterInputStream
구조 방법: BufferedInputStream (InputStream in) 은 BufferedInputStream 을 만 들 고 나중에 사용 할 수 있 도록 인 자 를 저장 합 니 다.
7. 네 가지 방식 으로 파일 을 복사 합 니 다.
1: 고 효율 흐름 플러스 배열
//1:      ,  "E:\\JAVAPractice\\IO\\1.jpg"
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy01{
public static void main(String[] args) throws IOException{
//      
long t1 = System.currentTimeMillis();
//     
BufferedInputStream bin = new BufferedInputStream(new FileInputStream("E:\\JAVAPractice\\IO\\1.jpg"));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\\JAVAPractice\\IO\\1_1.jpg"));
//            
byte[] b = new byte[1024];
int i = -1;//        
while((i = bin.read(b)) != -1){
bout.write(b,0,i);
bout.flush();
}
bout.close();
bin.close();
//        
long t2 = System.currentTimeMillis();
System.out.println("  :"+(t2-t1)/1000.0);//  :0.25
}
}

2: 기본 흐름 플러스 배열
//      ,  "E:\\JAVAPractice\\IO\\1.jpg
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Copy02{
public static void main(String[] args) throws IOException{
//        
long t1 = System.currentTimeMillis();
//         
FileInputStream in = new FileInputStream("E:\\JAVAPractice\\IO\\1.jpg");
FileOutputStream out = new FileOutputStream("E:\\JAVAPractice\\IO\\1_2.jpg");
//            
byte[] b = new byte[1024];
int i = -1;//        
while((i = in.read(b)) != -1){
out.write(b,0,i);
out.flush();
}
//   
in.close();
out.close();
//        
long t2 = System.currentTimeMillis();
System.out.println("  :"+(t2-t1)/1000.0);//  :0.344
}
}

3: 효율 적 인 흐름 바이트
//      ,  "E:\\JAVAPractice\\IO\\1.jpg
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy03{
public static void main(String[] args) throws IOException{
//      
long t1 = System.currentTimeMillis();
//       
BufferedInputStream bin = new BufferedInputStream(new FileInputStream("E:\\JAVAPractice\\IO\\1.jpg"));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\\JAVAPractice\\IO\\1_3.jpg"));
//  i ASCII  
int i = -1;
while((i = bin.read()) != -1){
bout.write(i);
}
//   
bin.close();
bout.close();
//      
long t2 = System.currentTimeMillis();
System.out.println("  :"+(t2-t1)/1000.0);//  :0.625
}
}

4: 기본 스 트림 바이트 복사
//        ,E:\\JAVAPractice\\IO\\1.jpg
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy04{
public  static void main(String[] args) throws IOException{
//      
long t1 = System.currentTimeMillis();
//         
FileInputStream in = new FileInputStream("E:\\JAVAPractice\\IO\\1.jpg");
FileOutputStream out = new FileOutputStream("E:\\JAVAPractice\\IO\\1_4.jpg");
//    ASCII      i
int i = -1;
while((i = in.read()) != -1){
out.write(i);
out.flush();
}
//   
out.close();
in.close();
//      
long t2 = System.currentTimeMillis();
System.out.println("  :"+(t2-t1)/1000.0);//  :52.383
}
}

8. 단일 폴 더 복사
//  E:\\JAVAPractice\\IO\\demo         txt    
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class DirCopy{
public static void main(String[] args) throws IOException{
//        
File src = new File("E:\\JAVAPractice\\IO\\demo");
//         
File dest = new File("E:\\JAVAPractice\\IO\\democopy");
dest.mkdir();
//           
File[] files = src.listFiles();
//  
for(File f : files){
//             
FileInputStream in = new FileInputStream(f);
//i        
int i = -1;
byte[] b = new byte[1024];
//       
File fileOut = new File(dest,f.getName());
FileOutputStream out = new FileOutputStream(fileOut);
while((i = in.read(b)) != -1){
out.write(b,0,i);
out.flush();
}
out.close();//out foreach   ,   foreach   
in.close();//  
}
System.out.println("    ");
}
}

좋은 웹페이지 즐겨찾기