라인에 따라 큰 파일의 자바 실현
String file = " + file.separators +"a.txt "; ((((s = raf.readline ())! = null) {System.out.println (s);} raf.close (); fis.close ();
인터넷에서 인기있는 두 가지 버전은 신뢰할 수 없습니다.
BucheredInputStream 및 BufferedOutputStream을 고려하여 BYTE 판독 값을 읽는 것은 너무 간단하고 비 수감 작업에 적합합니다. 또는 High and Hairpin에 더 적합한 Nio의 Filechannel을 사용하십시오.
file infile = "d : // error"); "d : //to.txt"; FileoutputStream (Outfile) .getChannel (); } outfilechannel.close ();
무작위 읽기 파일 내용
공개 클래스 readfromfile { /*** 파일을 바이트로 읽습니다.이 파일은 종종 그림, 사운드, 이미지 및 기타 파일과 같은 이진 파일을 읽는 데 사용됩니다. */ public static void readfilebytes (string filename) {file file = inputStream in = null을 한 번에 읽으십시오. (파일); int int while; printStacktrace (); return;} try {system.out.println ( "바이트에서 파일 내용을 읽고 한 번에 여러 바이트를 읽습니다. Byteread = 0; in = new FileInputStream (filename). showavailabytes (in); {System.out.write (Tempbytes, 0, Byteread);}}} {Exception e1) {e1.printstacktrace ();} finall {if (in! = null) {try {in.close ();} catch (ioException e1) {}}} / *** 문자로 자주 사용되는 단위로 파일을 읽습니다. 파일 = 새 파일 (filename); 파일)) int int int while; // 그러나이 두 문자가 별도로 표시되면 두 번 변경됩니다. // 따라서 Shield/r 또는 Shield/n. 그렇지 않으면 빈 줄이 많이 있습니다. if ((((((((((((((()))))))! = '/r'))) {System.out.print ((char) tempchar);}}}}} reader.close () catch (예외 e) {e.printstacktrace ();} try {system.out.println ( "단위의 파일 내용을 단위로 읽고 한 번에 여러 바이트를 읽습니다 : // 여러 문자 읽기 char [] tempchars = new Char [30] int charRead = 0; 새 FileInputStream (filename). ! = -1) {// Shielded/r은 (charread == tempchars.length) && (tempchars [tempchars.length -]! = '/r') {system.out.print (tempchars); } else {for (int i = 0; i <charread; i ++) {if (tempchars [i] == '/r') {계속;} else {system.out.print (tempchars [i]); }}}}}}}}} catch (Exception e1) {e1.printstacktrace ();} 최종 {if (reader! = null) {try {reader.close ();} catch (ioexception) {}} /** * 파일을 찍는 경우, -loking 라인에서 일반적으로 사용되는 파일을 형식화하십시오. println (행동 단위에 의해 파일의 내용을 읽으십시오 파일이 파일 끝으로 읽을 때까지 한 번의 줄입니다. tempstring); line ++;} reader.close ();} catch (ioException e) {e.printstacktrace ();} 최종 {if (reader! = null) {try {reader.close ();} catch (ioxception e1) {}} / *** 무작위로 파일을 무작위로 읽습니다.* / public static void readfilebyrandomAccess (String filename) {randomaccessfile randomfile = null (system.out.println; / onse a one. randomfile = new randomaccessfile (filename, "r")에 따라 랜덤 액세스의 파일 흐름에 액세스하십시오. (); // 파일 int beginex = (filelength> 4); 파일을 시작 인덱스 위치로 파일. randomfile. seek (byte) [] byte = 0; // BYTEREAD while (byteread = randomfile.read (bytes)) {system.out.write (bytes, 0, byteread)}) {e.printstacktrace );} 마지막으로 {if (randomfile! = null) {try {randomfile.close ();} catch (ioexception e1) {}}} / *** 남은 단어를 입력 스트림에 표시합니다 void showavaabilityBytes (inputStream in) {try {system.out.println ( "현재 바이트 입력 스트림의 바이트 수는 다음과 같습니다. main [] args) {string filename = "readfromfile.readfilebylines (filename);
파일 끝에 컨텐츠를 추가하십시오
공개 클래스 부록 { / ** * 메소드 추가 파일 : randomAccessfile * / public static void methoda (String filename, String content) {// 랜덤 방문 텍스트 조각 흐름 열기, randomAccessFile RandomFile = new RandomAccessFile을 읽고 쓰십시오. filename, "rw"); // byte long filelelenger = randomfile.length (); randomfile.seek (filelength); randomfile.writebytes (content);} catch (ioexception e) {e.printstacktrace (); AppendMethodb (String filename, String Content) {// 제작자의 두 번째 매개 변수는 Filewriter = New Filewriter (Filename, True); .close ();} catch (ioexception e) {e.printstacktrace ();} public static void main (String [] args) {String filename = "c :/temp/n ewtemp.txt"; Append! "; // 메소드 추가 파일 AppendTofile.appendMethoda (FileEname, Content); AppendTofile.appendMethoda ("Append End. /N "); // 파일 내용을 표시 readfromfile.ReadFileByLines (filename); // 방법 B 추가 파일 부록.
위는이 기사의 모든 내용입니다.