자바 코드
공개 클래스 readfromfile { /*** 파일을 바이트로 읽습니다. 종종 그림, 사운드, 이미지 및 기타 파일과 같은 이진 파일을 읽는 데 사용됩니다. */ public static void readfilebybytes (String filename) {file file = 새 파일 (filename); inputStream in = null; try {System.out.println ( "바이트로 파일 내용을 읽고 한 번에 하나의 바이트를 읽으십시오 :"); // 한 번에 하나의 바이트를 읽습니다. int tempbyte; while ((tempbyte = in.read ())! = -1) {System.out.write (tempbyte); } in.close (); } catch (ioexception e) {e.printstacktrace (); 반품; } try {system.out.println ( "바이트로 파일 내용을 읽고 한 번에 여러 바이트를 읽습니다 :"); // 한 번에 여러 바이트를 읽습니다. [] tempbytes = 새로운 바이트 [100]; int byteread = 0; in = new FileInputStream (filename); readfromfile.showavailableBytes (in); // 바이트 배열에 여러 바이트를 읽고, Byteread는 한 번에 읽는 바이트 수입니다 ((byteread = in.read (tempbytes))! = -1) {system.out.write (tempbytes, 0, byteread); }} catch (예외 e1) {e1.printstacktrace (); } 마침내 {if (in! = null) {try {in.close (); } catch (ioexception e1) {}}}} / *** 문자의 파일 읽기, 텍스트, 숫자 등의 파일을 읽는 데 종종 사용되는 파일* / public static void readfilebychars (string filename) {file file = 새 파일 (filename); 독자 독자 = null; try {system.out.println ( "문자의 파일 내용을 읽고 한 번에 하나의 바이트를 읽으십시오 :"); // 시간에 하나의 문자를 읽습니다. reader = new inputStreamReader (new FileInputStream (file)); int tempchar; ((tempchar = reader.read ())! = -1) {// Windows의 경우, 두 문자 /r /n이 함께 있으면 Newline을 나타냅니다. // 그러나이 두 문자가 별도로 표시되면 라인은 두 번 변경됩니다. // 따라서 블록 /r 또는 블록 /n. 그렇지 않으면 더 많은 빈 표지판이 있습니다. if (((char) tempchar)! = '/r') {system.out.print ((char) tempchar); }} reader.close (); } catch (예외 e) {e.printstacktrace (); } try {system.out.println ( "문자의 파일 내용을 읽고 한 번에 여러 바이트를 읽습니다 :"); // 한 번에 여러 문자를 읽습니다. char [] tempchars = new char [30]; int charread = 0; reader = new inputStreamReader (new FileInputStream (filename)); // 문자 배열에 여러 문자를 읽고, charread는 한 번에 읽는 문자 수입니다 ((charread = reader.read (tempchars))! = -1) {// 블록/r은 if ((charread == tempchars.length) && (tempchars [tempchars.length -1]! = '/r')) {tempchars. } else {for (int i = 0; i <charread; i ++) {if (tempchars [i] == '/r') {계속; } else {System.out.print (Tempchars [i]); }}}}}}} catch (예외 e1) {e1.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioexception e1) {}}}} / ** * 동작 단위로 파일 읽기, 종종 행 지향 * / public static void readfilebylines (string filename) {file file = new File (filename); bufferedReader reader = null; try {System.out.println ( "행동 단위에서 파일 내용을 읽고 한 번에 한 줄을 읽으십시오 :"); reader = new bufferedReader (New Filereader (파일)); 문자열 tempstring = null; int line = 1; // 파일의 끝이 될 때까지 ((tempstring = reader.readline ())! = null) {// 줄 번호 system.out.println ( "line" + line + ":" + tempstring); 라인 ++; } reader.close (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioException e1) {}}}} / *** 무작위 읽기 파일 내용* / public static void readFileByRandomAccess (String filename) {randomaccessfile randomfile = null; try {system.out.println ( "랜덤 파일 내용을 읽습니다 :"); // 랜덤 액세스 파일 스트림을 엽니 다. // 파일 길이, 바이트 수 긴 filelength = randomfile.length (); // 파일 읽기 시작 위치 int int beginindex = (filelength> 4)? 4 : 0; // 읽기 파일의 시작 위치를 시작 인덱스 위치로 이동합니다. randomfile.seek (beginindex); 바이트 [] 바이트 = 새로운 바이트 [10]; int byteread = 0; // 한 번에 10 바이트를 읽습니다. 파일 내용이 10 바이트 미만인 경우 나머지 바이트를 읽으십시오. // 한 번에 읽은 바이트 수를 Byteread while ((byteread = randomfile.read (bytes))! = -1) {system.out.write (bytes, 0, byteread); }} catch (ioexception e) {e.printstacktrace (); } morne {if (randomfile! = null) {try {randomfile.close (); } catch (ioexception e1) {}}}} / *** 입력 스트림에 남은 바이트 수를 표시합니다* / private static void showavailableBytes (inputStream in) {try {system.out.println ( "현재 바이트 입력 스트림의 바이트 수는 :" + in.available (); } catch (ioexception e) {e.printstacktrace (); }} public static void main (String [] args) {String filename = "c : /temp/newtemp.txt"; readfromfile.readfilebybytes (filename); readfromfile.readfilebychars (filename); readfromfile.readfilebylines (filename); readfromfile.readfilebyrandomaccess (filename); }}자바 코드
공개 클래스 부록 { / ** * 메소드 append 파일 : randomAccessfile * / public static void actendmethoda (String filename, String content) {try {// random ac // 파일 길이, 바이트 수 긴 filelength = randomfile.length (); // 파일 쓰기 포인터를 파일 끝으로 이동합니다. randomfile.seek (filelength); randomfile.writeBytes (컨텐츠); randomfile.close (); } catch (ioexception e) {e.printstacktrace (); }} /** * 메소드 B 추가 파일 : filewriter * /public static void accendmethodb (String filename, string content) {try {// filewriter를 열면 생성자의 두 번째 매개 변수는 파일을 추가 된 양식 filewriter writer = New Filewriter (filename, true)로 작성하는 것이 참입니다. Writer.write (내용); Writer.close (); } catch (ioexception e) {e.printstacktrace (); }} public static void main (String [] args) {String filename = "c : /temp/newtemp.txt"; 문자열 내용 = "새로운 부록!"; // Append File AppendTofile.appendMethoda (filename, content); AppendTofile.appendMethoda (Filename, "Append End. /n"); // 파일 내용 표시 readfrile.ReadFileByLines (filename); // Append File AppendTofile.appendMethodb (filename, content); AppendTofile.appendMethodb (filename, "append end. /n"); // 파일 내용 표시 readfrile.ReadFileByLines (filename); }}위의 것은 편집자가 가져온 Java 읽기 및 쓰기 파일 (간단한 예)의 전체 내용입니다. 모두가 wulin.com을 지원하기를 바랍니다