이 기사는 주로 Java InputStream 스트림의 사용을 소개합니다.
(1) FileInputStream : 서브 클래스, 데이터를 읽기위한 채널
사용 단계 :
1. 대상 파일 가져 오기 : 새 파일 ()
2. 채널 생성 : 새 FileInputString ()
3. 데이터 읽기 : read ()
4. 릴리스 리소스 : Close ()
// 기본적으로 가져올 일부 패키지 import java.io.file; import java.io.fileInputStream; import java.io.ioException;
public static void main (string [] args)은 ioexception {// todo 자동 생성 메소드 스터브 // 효과를 볼 수있는 메소드를 호출합니다. test1();System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.(2) 데이터를 읽는 세 가지 방법
1. 직접 읽기 (한 번에 하나의 바이트 만)
int date = fileInputStream.Read (); char date3 = (char) fileInputStream.Read ();
// public static void test1 () 인쇄 ioexception {// (1) 대상 파일 파일 파일을 가져옵니다. 파일 파일 = 새 파일 ( "c : //users//joke//desktop//demo1.java"); // (2) 대상 파일 경로를 기반으로 채널 생성 : new FileInputStream (파일) (파일) (3); 데이터 : read (); int date = fileInputStream.read (); // 여기는 int type int date2 = fileInputStream.Read (); // char 날짜 3 = (char) fileInputStream.Read (); // show system.out.println (date+"//"+date2+"//"+date3); // (4) resource fileInputStream.close ();} 릴리스2. 루프 만 사용하십시오 (ine 효율)
for (int i = 0; i <file.length (); i ++) {system.out.print ((char) fileInputStream.Read ()); } // 메소드 2 루프 트래버스 public static void test2 ()는 ioexception을 던지려고 {// 시간이 오래된 시작 시간을 통해 테스트 효율성 = system.currenttimeMillis (); file file = new File ( "c : //users//joke//desktop//demo1.java"); fileInputStream (fore for for for for for for Fore); 0; i <file.length (); i ++) {system.out.print ((char) fileInputStream.read ());} fileInputStream.close ();3.byte [] 버퍼 (지정된 바이트 수를 읽을 수 있으며 전체 파일을 읽을 수 없습니다)
바이트 [] bt = 새로운 바이트 [1024]; int count = fileInputStream.Read (bt); System.out.println (새 문자열 (bt, 0, count));
//Method 3 create a buffer (can only read the specified size, not read a complete file) public static void test3() throws IOException{File file = new File("C://Users//joke//Desktop//Demo1.java");FileInputStream fileInputStream = new FileInputStream(file);//Create a buffer to speed up reading of data, and determine the byte size to byte byte [] bt = new byte [1024]; // read () byte int count = fileInputStream.read (bt); system.out.println (count); // 바이트 수를 표시 read system.out.println (새 문자열 (bt, 0, count)); // 바이트를 문자열로 변환하여 fileInputStream.close ();} 표시합니다.4. 버퍼와 루프를 결합합니다. 버퍼는 일반적으로 1024의 배수로 설정됩니다. 버퍼 세트가 클수록 읽기 효율이 높아집니다.
바이트 [] bt = 새로운 바이트 [1024]; int count = 0; while ((count = fileInputStream.Read (bt))! = -1) {System.out.println (new String (bt, 0, count)); } // 메소드 4 루프는 버퍼 (고 효율) public static void test4 ()와 결합되어 ioexception {// time long starttime = system.currentTimeMillis (); currentTimeMillis ()에 의해 테스트 효율성을 던집니다. 버퍼는 일반적으로 1024의 배수로 설정됩니다. 이론적으로 버퍼 세트가 클수록 읽기 효율 바이트 [] bt = new byte [1024]; int count = 0; // retuds -1. int count = 0; //가 더 높아지면 (count = fileInputStream.read (bt)) {// String type display (count tranges start on on in bt) System.out.println (new String (bt, 0, count));} fileInputStream.close (); Long Endtime = System.CurrentTimeMillis (); System.out.println ( "파일을 읽는 데 걸리는 시간 :"+(EndTime-StartTime));}모모는 말했다 :
위의 두 번째 및 네 번째 방법을 비교하면 방법 4의 효율이 비교적 높기 때문에 4 개의 권장 방법이 사용됩니다.
여기서 우리는 직접 예외를 던지고 던지는 것 외에도 사용할 수 있습니다.
try{ }cater{ }finally{ }
예외를 처리합니다
위의 내용은 귀하에게 소개 된 Java IO 스트림의 입력 스트림 인 InputString ()을 사용하는 것입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!