Javaコード
Public Class ReadfromFile { /***バイトでファイルを読み取ります。これは、写真、サウンド、画像、その他のファイルなどのバイナリファイルの読み取りによく使用されます。 */ public static void readfilebytes(string filename){file file = new file(filename); inputstream in = null; try {System.out.println( "ファイルコンテンツをバイトで読み取り、一度に1バイトを読み取ります:"); // = new fileinputStream(file); int tempbyte; while((tempbyte = in.read())!= -1){system.out.write(tempbyte); } in.close(); } catch(ioexception e){e.printstacktrace();戻る; } try {system.out.println( "ファイルコンテンツをバイトで読み取り、一度に複数のバイトを読み取ります:"); //一度に複数のバイトを読み取りますbyte [] tempbytes = new byte [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){}}}} / ***文字のファイルを読み取り、テキスト、数字などのファイルを読むためによく使用されます。 Reader reader = null; try {System.out.println( "ファイルコンテンツを文字で読み取り、一度に1つのバイトを読み取ります:"); //一度に1つの文字を読み取りreader = new inputStreamReader(new FileInputStream(file)); int tempchar; ((tempchar = reader.read())!= -1){// Windowsの場合、2つの文字 /r /nが一緒にいるとき、それらは新しいラインを表します。 //ただし、これら2つの文字が個別に表示されると、線が2回変更されます。 //したがって、block /r、またはblock /n。そうでなければ、さらに多くの空白の兆候があります。 if(((char)tempchar)!= '/r'){system.out.print((char)tempchar); }} reader.close(); } catch(Exception 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){// blocked/rは(charread == tempchars.length)&&(tempchars.length -1]!= '/r'){system.out.print(system.out.print); } 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 e1){}}}} / ** *行動ユニットのファイルを読み取ります。row指向 * / public static void readfilebylines(string filename){file file = new file(filename); bufferedreader reader = null; try {System.out.println( "動作単位のファイルコンテンツを読み取り、一度に1行全体を読み取ります:"); reader = new BufferedReader(new fileReader(file)); string tempstring = null; int line = 1; // nullがファイルの最後になるまで一度に1行で読み取ります((tempstring = reader.readline()!= null){// show line number system.out.println( "line" + line + ":" + temptring); line ++; } 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( "ファイルコンテンツをランダムに読み取ります:"); //ランダムアクセスファイルのストリームを開き、読み取り専用ランダムファイル= new RandomAccessFile(filename、 "r"); //ファイルの長さ、バイト数long filelength = randomfile.length(); // file int beginindex =(filelength> 4)の読み取りの位置を開始しますか? 4:0; //読み取りファイルの開始位置をbeginindex位置に移動します。 randomfile.seek(beginindex); byte [] bytes = new byte [10]; int byteread = 0; //一度に10バイトを読み取ります。ファイルコンテンツが10バイト未満の場合は、残りのバイトを読み取ります。 //((byteread = randomfile.read(bytes))!= -1){system.out.write(bytes、0、byteread); }} catch(ioexception e){e.printstacktrace(); }最後に{if(randomfile!= null){try {randomfile.close(); } catch(ioException e1){}}}} / ***入力ストリームに残っているバイト数を表示* / private static void showavabaiblebytes(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); }}Javaコード
public class appendttofile { / ** *メソッドAppendファイル:RandomAccessFile *を使用 * / public static appendmethoda(string filename、string content){try {//ランダムアクセスファイルストリームを開き、ランダムアクセスファイル= new RandomAccessFile(filename、 "rw"); //ファイルの長さ、バイト数long filelength = randomfile.length(); //ファイルの最後に書き込みファイルポインターを移動します。 randomfile.seek(filelength); randomfile.writebytes(content); randomfile.close(); } catch(ioexception e){e.printstacktrace(); }} /** *メソッドb追加ファイル:ファイルライターを使用 * /public static void appendmethodb(string filename、string content){// filewriterを開くと、コンストラクターの2番目のパラメーターは、apped form filewriter writer = new filewriter(filename、true); writer.write(content); writer.close(); } catch(ioexception e){e.printstacktrace(); }} public static void main(string [] args){string filename = "c:/temp/newtemp.txt";文字列content = "new Append!"; // appendtofile.appendmethoda(filename、content)を追加するファイルを追加します。 appendtofile.appendmethoda(filename、 "append end。 /n"); //ファイルのコンテンツreadfromfile.readfilebylines(filename)を表示します。 // appendtofile.appendmethodb(filename、content)を追加するファイルを追加します。 appendtofile.appendmethodb(filename、 "append end。 /n"); //ファイルのコンテンツreadfromfile.readfilebylines(filename)を表示します。 }}上記は、編集者からもたらされたJavaの読み取りと書き込みファイル(簡単な例)の完全な内容です。誰もがwulin.comをサポートすることを願っています〜