산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

자바 FileOutputStream 클래스

자바 FileOutputStream 클래스

이 자습서에서는 예제를 통해 Java FileOutputStream 및 해당 메서드에 대해 알아봅니다.

FileOutputStream java.io 클래스 패키지는 파일에 데이터(바이트 단위)를 쓰는 데 사용할 수 있습니다.

OutputStream 확장 추상 클래스.

FileOutputStream에 대해 알아보기 전에 , Java Files에 대해 알고 있어야 합니다.

<시간>

FileOutputStream 생성

파일 출력 스트림을 생성하려면 java.io.FileOutputStream을 가져와야 합니다. 먼저 패키지. 패키지를 가져온 후 Java에서 파일 출력 스트림을 만드는 방법은 다음과 같습니다.

1. 파일 경로 사용

// Including the boolean parameter
FileOutputStream output = new FileOutputStream(String path, boolean value);

// Not including the boolean parameter
FileOutputStream output = new FileOutputStream(String path);

여기에서 path로 지정된 파일에 연결될 출력 스트림을 만들었습니다. .

또한 선택적 부울 매개변수입니다. true로 설정된 경우 , 새 데이터는 파일의 기존 데이터 끝에 추가됩니다. 그렇지 않으면 새 데이터가 파일의 기존 데이터를 덮어씁니다.

2. 파일의 개체 사용

FileOutputStream output = new FileOutputStream(File fileObject);

여기에서 fileObject으로 지정된 파일에 연결될 출력 스트림을 만들었습니다. .

<시간>

FileOutputStream의 메소드

FileOutputStream 클래스는 OutputStream에 있는 다양한 메서드에 대한 구현을 제공합니다. 수업.

write() 메소드

예:파일에 데이터를 쓰는 FileOutputStream

import java.io.FileOutputStream;

public class Main {
    public static void main(String[] args) {
        
        String data = "This is a line of text inside the file.";

        try {
            FileOutputStream output = new FileOutputStream("output.txt");

            byte[] array = data.getBytes();

            // Writes byte to the file
            output.write(array);

            output.close();
        }

        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

위의 예에서 output이라는 파일 출력 스트림을 만들었습니다. . 파일 출력 스트림은 output.txt 파일과 연결됩니다. .

FileOutputStream output = new FileOutputStream("output.txt");

파일에 데이터를 쓰기 위해 write()를 사용했습니다. 방법.

여기에서 프로그램을 실행하면 output.txt 파일은 다음 내용으로 채워집니다.

This is a line of text inside the file.

참고 :getBytes() 프로그램에서 사용된 메소드는 문자열을 바이트 배열로 변환합니다.

<시간>

flush() 메소드

출력 스트림을 지우려면 flush()를 사용할 수 있습니다. 방법. 이 메서드는 출력 스트림이 모든 데이터를 대상에 쓰도록 합니다. 예를 들어,

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {

        FileOutputStream out = null;
        String data = "This is demo of flush method";

        try {
            out = new FileOutputStream(" flush.txt");

            // Using write() method
            out.write(data.getBytes());

            // Using the flush() method
            out.flush();
            out.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

프로그램을 실행하면 flush.txt 파일이 문자열 data로 표시되는 텍스트로 채워집니다. .

<시간>

close() 메소드

파일 출력 스트림을 닫으려면 close()을 사용할 수 있습니다. 방법. 메소드가 호출되면 FileOutputStream의 메소드를 사용할 수 없습니다. .

<시간>

FileOutputStream의 다른 메서드

메소드 설명
finalize() close() 메소드 호출
getChannel() FileChannel의 개체를 반환합니다. 출력 스트림과 연결
getFD() 출력 스트림과 관련된 파일 설명자를 반환합니다.

자세한 내용은 Java FileOutputStream(공식 Java 설명서)을 참조하십시오.


java

  1. 자바 최종 키워드
  2. 자바 instanceof 연산자
  3. 자바 상속
  4. 자바 중첩 정적 클래스
  5. 자바 익명 클래스
  6. 자바 싱글톤 클래스
  7. 자바 리플렉션
  8. 자바 ObjectOutputStream 클래스
  9. 자바 제네릭
  10. 자바 파일 클래스