ラインに応じた大きなファイルのJavaの実現
string file = " + file.separators +" a.txt "; ((((s = raf.readline())!= null){system.out.println(s);} raf.close(); fis.close();
インターネットで人気のあるバージョンは信頼できません
BufferedInputStreamとBufferedOutputStreamを考慮して、このコードを読み取ります。または、NioのFileChannelを使用してください。
file infile = new file( "d:// erser"); "d://to.txt"); fileoutputStream(Outfile).getChannel(); } outfilechannel.close();
ランダム読み取りファイルコンテンツ
パブリッククラスReadFromFile { /***バイトでファイルを読み取ります。これは、写真、サウンド、画像、その他のファイルなどのバイナリファイルの読み取りによく使用されます。 */ public void readfilebytes(string filename){file file(filename); (file); printStackTrace();} try {system.out.println( "バイトでファイルコンテンツを読み取り、複数のバイトを読み取ります。 byteread = 0; {system.out.write(tempbytes、0、byteread);}} {exception e1){e1.printstacktrace();}最後に{if(in!= null){try {in.close();} catch(ioexception(ioexception e1){}} / ***ファイルをユニットとして読み取ります。これは、文字、その他のタイプのファイルでよく使用される文字でよく使用されます。ファイル=新しいファイル(ファイル名); file)) //ただし、これら2つの文字が個別に表示されると、2回変更されます。 //したがって、シールド/r、またはシールド/n。それ以外の場合、空の行がたくさんあります。 if(((((((((((((((((((((((((((((((((((((((()))))))))))」)」」{System.out.print((char)tempchar);}} reader.close();} catch(例外E) {e.printstacktrace();} try {system.out.println( "ユニットのファイルコンテンツをユニットとして読み取り、一度に複数のバイトを読み取ります://複数の文字char [] tempchars = new char [30] ; int charread = 0 0; != -1){//またシールド/rは表示されません(charread == tempchars.length)&&(tempchars [tempchars.length-]!= '/r'){system.out.print(tempchars); } else {for(int i = 0; i <charread; i ++){if(tempchars [i] == '/r'){continue;} else {system.out.print(tempchars [i]); }}}}}}}}}} catch(exception e1){e1.printstacktrace();} fluly {if(reader!= null){try {reader.close();} catch(ioexception){}} /** *ファイルを使用して、一般的に-lowing void readfilebylines(string filename)のラインで使用されます。 println(動作ユニットは、ファイルのコンテンツを一度に読み取りますファイルが終了するまで、一度に行。 Tempstring); line ++;} reader.close();} catch(ioexception e){e.printstacktrace();} finall e1){}} / ***ランダムにファイルをランダムに読み取ります。 /ランダムファイルの読み取り方法に応じて、ファイル(filename、 "r")を開きます。 (); File int beginex =(filelents> 4:0; beginindex位置にファイル。 randomfile.seek(begining); // bytereadを読むバイト数(byteread = randomfile.read(bytes)){system.out.write(bytes、0、byteread);}}}}}}}}) );}最後に{if(randomfile!= null){try {randomfile.close();} catch(ioexception e1){}}} / ***入力ストリームの左の単語を表示しますvoid showavaabilitybytes(inputstream in){try {system.out.println( "現在のバイト入力ストリームのバイト数は次のとおりです。 Main(] string filename = "/temp/newtemp.txt";
ファイルの最後にコンテンツを追加します
public class appendtofile { / ** *メソッド追加ファイル:randomaccessfileを使用 * / public static void methoda(string filename、string content){//ランダム訪問のテキストピースフローを開き、ランダムアクセスファイル= new randomaccessfile( filename、 "rw"); randomfile.seek(filelength); void appentmethodb(// fileerを開き、ファイルwriter = new filewriter(filename、true)を書きます.close();} catch(ioexception e){e.printstacktrace();} public static void main(string [] args){c c:/temp/n ewtemp.txt "; append! "; //メソッド追加ファイルAppendTofile.AppendMethoda(fileName、content); appendtofile.AppendMethoda(" append end。 /n "); //ファイルのコンテンツreadfromfile.readfilebylines(filename); //のコンテンツのコンテンツを表示する方法B追加ファイルappenttofile.appendmethodb(filename、content);
上記は、この記事のすべての内容です。