Java IO操作——RandomAccessFileランダム読み書きファイル操作クラスの使用を簡単に理解する

4130 ワード

学習目標
RandomAccessFileクラスの役割を把握する
RandomAccessFileで指定された場所のデータを読み込むことができます
RandomAccessFileクラスの主な機能は、ランダム読み出し機能を完了し、指定された場所の内容を読み取ることができます.
以前のFileクラスはファイル自体に対してのみ操作されていましたが、ファイルの内容を操作する場合はRandomAccessFileクラスを使用します.これはランダム読み出しクラスに属し、1つのファイルで指定された場所のデータをランダムに読み込むことができます.たとえば、ファイルに次の3つのデータが保存されているとします.
zhangsan, 30
lisi, 31
wangwu, 32
ファイルには、すべてのコンテンツがバイトごとに格納されているため、一定の保存場所があります.
RandomAccessFileクラスの一般的な操作方法
1、public RandomAccessFile(File file,String mode)throws FileNotFoundException構築方法Fileクラスのオブジェクトを受信し、操作パスを指定しますが、設定時にモード「r」:読み取り専用、「w」:書き込みのみ、「rw」:読み書きを設定する必要があります.
2、public RandomAccessFile(String name,String mode)throws FileNotFoundException構築方法Fileクラスオブジェクトを使用してファイルを表すのではなく、固定されたファイルパスを直接入力します.
3、public void close()throws IOExceptionクローズ操作
4、public int read(byte[]b)throws IOException 1つのbyte配列にコンテンツを読み込む
5、public final byte readByte()throws IOException 1バイトを読み込む
6、public final int readInt()throws IOExceptionファイルから整数データを読み込む.
7、public void seek(long pos)throws IOExceptionは読取ポインタの位置を設定する.
8、public final void writeBytes(String s)throws IOExceptionは1つの文字列をファイルに書き込み、バイトで処理する.
9、public final void writeInt(int v)throws IOExceptionはint型データをファイルに書き込み、長さは4ビットである.
10、public int skipBytes(int n)throws IOExceptionポインタは何バイトスキップしますか.
構築:public RandomAccessFile(File file,String mode)throws FileNotFoundException
このようなインスタンス化の場合、Fileクラスを渡し、どのファイルを操作すべきかをプログラムに伝え、その後、1つのモード、ファイルのオープンモード、よく使われる2つのモードがあります.
r:読み出しモード
w:書くだけ
rw:読み書き、このモードを使用すると、このファイルが存在しない場合、自動的に作成されます.
import java.io.File ;
import java.io.RandomAccessFile ;
public class RandomAccessFileDemo01{
	//          ,         
	public static void main(String args[]) throws Exception{
		File f = new File("d:" + File.separator + "test.txt") ;	//         
		RandomAccessFile rdf = null ;		//   RandomAccessFile    
		rdf = new RandomAccessFile(f,"rw") ;//     ,       ,     
		String name = null ;
		int age = 0 ;
		name = "zhangsan" ;			//       8
		age = 30 ;					//       4
		rdf.writeBytes(name) ;		//          
		rdf.writeInt(age) ;			//          
		name = "lisi    " ;			//       8
		age = 31 ;					//       4
		rdf.writeBytes(name) ;		//          
		rdf.writeInt(age) ;			//          
		name = "wangwu  " ;			//       8
		age = 32 ;					//       4
		rdf.writeBytes(name) ;		//          
		rdf.writeInt(age) ;			//          
		rdf.close() ;				//   
	}
};
以上で書き込みが完了し、各データの長さは12ビットである.
書き終わったら、データの読み取りを開始します.書くときは文字列を書くことができ、読むときはバイトで読み取る必要があります.
import java.io.File ;
import java.io.RandomAccessFile ;
public class RandomAccessFileDemo02{
	//          ,         
	public static void main(String args[]) throws Exception{
		File f = new File("d:" + File.separator + "test.txt") ;	//         
		RandomAccessFile rdf = null ;		//   RandomAccessFile    
		rdf = new RandomAccessFile(f,"r") ;//           
		String name = null ;
		int age = 0 ;
		byte b[] = new byte[8] ;	//   byte  
		//          ,             
		rdf.skipBytes(12) ;		//          
		for(int i=0;i<b.length;i++){
			b[i] = rdf.readByte() ;	//       
		}
		name = new String(b) ;	//       byte       
		age = rdf.readInt() ;	//     
		System.out.println("        -->   :" + name + ";  :" + age) ;
		//          
		rdf.seek(0) ;	//          
		for(int i=0;i<b.length;i++){
			b[i] = rdf.readByte() ;	//       
		}
		name = new String(b) ;	//       byte       
		age = rdf.readInt() ;	//     
		System.out.println("        -->   :" + name + ";  :" + age) ;
		rdf.skipBytes(12) ;	//          
		for(int i=0;i<b.length;i++){
			b[i] = rdf.readByte() ;	//       
		}
		name = new String(b) ;	//       byte       
		age = rdf.readInt() ;	//     
		System.out.println("        -->   :" + name + ";  :" + age) ;
		rdf.close() ;				//   
	}
};

ファイルの内容を操作したい場合は、RandomAccessFileで完了できます
まとめ:
1.RandomAccessFileクラスの役割を理解すればよい.
2、RandomAccessFileクラスは操作が面倒なので、IOでは専用の入出力操作が提供されます.