Javaの読み取りと書き込みファイルの方法は、仕事に多くの用途があると思います。 Javaの読み取りおよび書き込みファイルメソッドを使用して、データ入力と出力を処理しましたが、これは非常に便利です。しかし、私の記憶は本当に心配です。多くの場合、それを書く方法を覚えていないので、私のJavaコードは本当に小さいので、もっと練習する必要があります。ここに概要があり、将来の側面に焦点を当てます。
Java読み取りファイル
パッケージの天才のばかドリーム。 import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.io.Reader;public class Javaio { / ** *オペレーティングシステム、GBKなどのデフォルトのエンコード方法、非UTF8 * * / / ** *ファイルコンテンツをバイトで読み取り、写真、画像、サウンドなどのバイナリファイルの読み取りによく使用されることがよくあります。 fileInputStream in = null; try {System.out.println( "ファイルをバイトで読み取り、一度に1つのバイトを読み取ります:"); in = new FileInputStream(file); int temp = 0; while((temp = in.read())!= -1){system.out.println(temp); } in.close(); } catch(ioexception e){e.printstacktrace();戻る ; } try {system.out.println( "ファイルをバイト単位で読み取り、一度に複数のバイトを読み取ります:"); byte [] temp = new byte [100]; int byteread = 0; in = new FileInputStream(file); javaio.showavailablebytes(in); while((byteread = in.read(temp))!= -1){system.out.write(temp、0、byteread); }} catch(例外e1){e1.printstacktrace(); }最後に{if(in!= null){try {in.close(); } catch(ioException e1){}}}}} / ** *文字のファイルを読み取り、テキスト、数字などのファイルを読むためによく使用されます。 Reader reader = null; try {System.out.println( "キャラクターのファイルコンテンツを読み取り、一度に1バイトで読み取ります:"); // inputStreamReaderクラス:バイトを文字readerに変換するためのブリッジ= new inputStreamReader(new FileInputStream(file)); int temp; while((temp = reader.read())!= -1){if(((char)temp)!= '/r'){system.out.println((char)temp); }} reader.close(); } catch(Exception e){e.printstacktrace(); } try {system.out.println( "ファイルコンテンツを文字で読み取り、一度に複数のバイトを読み取ります:"); char [] temp = new char [30]; int charread = 0; reader = new inputStreamReader(new FileInputStream(filename)); while((charread = reader.read(temp))!= -1){if((charread == temp.length)&&(temp [temp.length -1]!= '/r')){system.out.println(temp); } else {for(int i = 0; i <charread; i ++){if(temp [i] == '/r'){break; } else {system.out.println(temp [i]); }}}}}}}} catch(Exception e){e.printstacktrace(); }最後に{if(reader!= null){try {reader.close(); } catch(ioException e){}}}} / ***動作単位でファイルを読み取ります。行の読み取りによく使用されます。 bufferedreader reader = null; try {System.out.println( "動作単位でファイルコンテンツを読み取り、一度に1行全体を読み取ります:"); reader = new BufferedReader(new fileReader(file)); string temp = null; int line = 1; while((temp = reader.readline())!= null){system.out.println( "line" + line + ":" + temp); line ++; } reader.close(); } catch(ioexception e){e.printstacktrace(); }最後に{if(reader!= null){try {reader.close(); } catch(ioexception e){}}}} / ***ランダム読み取りファイルコンテンツ** / public static void readfilebyrandomaccess(string filename){randomaccessfile randomfile = null; try {system.out.println( "ランダムはファイルコンテンツを読み取ります"); randomfile = new RandomAccessFile(filename、 "r"); long filelength = randomfile.length(); int beginIndex =(filelength> 4?4:0); randomfile.seek(beginindex); byte [] bytes = new byte [10]; int byteread = 0; while((bytead = randomfile.read(bytes))!= -1){system.out.write(bytes、0、byteread); }} catch(ioexception e){e.printstacktrace(); }最後に{if(randomfile!= null){try {randomfile.close(); } catch(ioexception e){}}}} private static void showavabablebytes(inputstream in){try {system.out.println( "現在のバイト入力ストリームのバイト数は次のとおりです。 } catch(ioexception e){e.printstacktrace(); }} public static void main(string [] args){string filename = "e://baiyishaonian.txt"; javaio.readfilebybytes(filename); javaio.readfilebychar(filename); javaio.readfilebyline(filename); javaio.readfilebyrandomaccess(filename); }}Javaはファイルを書き込みます
パッケージの天才のばかドリーム。 java.io.bufferedwriter; Import java.io.file; import java.io.filenotfoundection; import java.io.fileoutputStream; Import java.io.filewriter; Import java.io.ioexception; Import java.io.OutputStreamterter; public class javaio2 {public void(pubil ioException {String Path = "e:// Genius Idiot Dream // Java"; file file = new file( "e:// Genius Idiot Dream // Java"、 "baiyishaonian.txt"); if(!file.exists()){try {file.createNewFile(); } catch(ioexception e){e.printstacktrace(); }} / *** javaにファイルを書き込む3つの方法** / fileoutputStream fos = null; bufferedwriter bw = null; filewriter fw = null; int value = 1000; try {fos = new fileoutputStream(new file(path+"fos.txt")); long begin = system.currenttimemillis(); for(int i = 1; i <= value; i ++){fos.write(5); } long end = system.currenttimemillis(); system.out.println( "fileoutputStreamのthecosttime is:" +(end-begin)); fos.close(); bw = new BufferedWriter(new outputStreamWriter(new FileOutputStream(new File(PATH+"BR.TXT"))、 "UTF8")); begin = system.currenttimemillis(); for(int i = 1; i <= value; i ++){bw.write(5); bw.newline(); } bw.close(); end = system.currenttimemillis(); system.out.println( "bufferedwriterのthecosttime is:" +(end-begin)); fw = new filewriter(path+"fw.txt"); begin = system.currenttimemillis(); for(int i = 1; i <= value; i ++){fw.write(5); } fw.close(); end = system.currenttimemillis(); system.out.println( "filewriterのthecosttime is:" +(end-begin)); } catch(例外e){// todo auto-enerated catch block e.printstacktrace(); }最後に{try {fos.close(); // fileoutputStream bw.close(); // bufferedwriter fw.close(); // filewriter} catch(例外e){e.printstacktrace(); }}}}Javaの読み取りと書き込みファイルの方法(推奨)の上記の要約は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。