logo

Java FileInputStream 클래스

Java의 FileInputStream 클래스는 파일에서 바이트 형식의 데이터를 읽는 데 사용됩니다. 이미지나 오디오 파일과 같은 바이너리 데이터를 읽는 데 이상적입니다. 텍스트 파일을 읽으려면 다음을 사용하는 것이 좋습니다. FileReader.

  • 직접 접속: 버퍼링 없이 디스크에서 파일 내용을 직접 읽습니다.
  • 플랫폼 독립적: 모든 운영 체제에서 작동할 수 있습니다.

선언

FileInputStream 클래스는 입력스트림 클래스는 파일에서 원시 바이트 데이터를 읽는 방법을 상속한다는 의미입니다.

공개 클래스 FileInputStream은 InputStream을 확장합니다.



예: 파일에서 데이터를 읽는 FileInputStream 클래스입니다.

Java
import java.io.*; public class Geeks{    public static void main(String[] args){    // Use try-with-resources to automatically close the  // stream  try (FileInputStream fi  = new FileInputStream('file1.txt')) {  // Display file channel information  System.out.println('Channel: '  + fi.getChannel());  // Display file descriptor  System.out.println('File Descriptor: '  + fi.getFD());  // Show available bytes in the stream  System.out.println('Number of remaining bytes: '  + fi.available());  // Skip first few bytes  fi.skip(4);  System.out.println('File Contents:');  // Read and print file content  int ch;  while ((ch = fi.read()) != -1) {  System.out.print((char)ch);  }  }  catch (FileNotFoundException e) {  System.out.println(  'File not found: Ensure 'file1.txt' exists in the working directory.');  }  catch (IOException e) {  System.out.println(  'An error occurred while reading the file: '  + e.getMessage());  }  } } 

산출:  

스크린샷' title=산출

생성자 FileInputStream 클래스

1. FileInputStream(문자열 이름)

지정된 이름을 가진 파일에서 읽을 입력 파일 스트림을 만듭니다. 

FileInputStream fi = new FileInputStream('example.txt');

2. FileInputStream(파일 파일)

지정된 File 객체에서 읽을 입력 파일 스트림을 만듭니다. 

파일 f = new File('example.txt');
FileInputStream fi = new FileInputStream(f);

3. FileInputStream(파일 설명자 fdobj)

지정된 파일 설명자에서 읽을 입력 파일 스트림을 만듭니다. 

파일설명자 fd = FileDescriptor.in;
FileInputStream fi = new FileInputStream(fd); 

다음 내용으로 프로젝트 디렉터리에 file.txt라는 파일을 만듭니다.

이게 내 첫 번째 코드야
이게 내 두 번째 코드야

Java
import java.io.*; public class Geeks {  public static void main(String[] args)  {  // Use try-with-resources to automatically close the stream  try (FileInputStream fi  = new FileInputStream('file1.txt')) {  // Display file channel information  System.out.println('Channel: '  + fi.getChannel());  // Display file descriptor  System.out.println('File Descriptor: '  + fi.getFD());  // Illustrating available method  System.out.println('Number of remaining bytes: '  + fi.available());  // Illustrating skip() method  fi.skip(4);  System.out.println('File Contents:');  // Reading characters from FileInputStream  int ch;  while ((ch = fi.read()) != -1) {  System.out.print((char)ch);  }  }  catch (FileNotFoundException e) {  System.out.println(  'File not found: Ensure 'file1.txt' exists in the working directory.');  }  catch (IOException e) {  System.out.println(  'An error occurred while reading the file: '  + e.getMessage());  }  } } 

산출:  

산출' loading='lazy' title=산출

자바의 방법 FileInputStream 클래스

행동 양식 수행된 작업 
사용 가능() 이 입력 스트림에서 읽거나 건너뛸 수 있는 남은 바이트 수의 추정치를 반환합니다.
닫다() 이 파일 입력 스트림을 닫고 스트림과 관련된 모든 시스템 리소스를 해제합니다.
마무리() 더 이상 참조가 없을 때 이 파일 입력 스트림의 닫기 메소드가 호출되는지 확인합니다. 
getChannel() 이 파일 입력 스트림과 연관된 고유한 FileChannel 객체를 반환합니다. 
getFD() 이 FileInputStream이 사용하는 파일 시스템의 실제 파일에 대한 연결을 나타내는 FileDescriptor 객체를 반환합니다.
읽다() 이 입력 스트림에서 1바이트의 데이터를 읽습니다.
읽기(바이트[] b)이 입력 스트림에서 최대 b.length 바이트의 데이터를 바이트 배열로 읽습니다. 
읽기(바이트[] b int off int len)이 입력 스트림에서 최대 len 바이트의 데이터를 바이트 배열로 읽습니다.
건너뛰다() 입력 스트림에서 n바이트의 데이터를 건너뛰고 삭제합니다.
퀴즈 만들기