1.java.io.RandomAccessFile:該類設(shè)計用來專門讀寫文件數(shù)據(jù)。其基于指針進行讀寫娶聘,即:總是在指針當(dāng)前位置讀或?qū)懽止?jié)蛤克。RAF有兩種常用創(chuàng)建模式:"r":只讀模式,"rw":讀寫模式距淫。
public class RandomAccessFileDemo {
? ? public static void main(String[] args) throws IOException {
? ? ? ? /*
? ? ? ? * RAF常用的構(gòu)造方法:
? ? ? ? * RandomAccessFile(String path,String mode)
? ? ? ? * RandomAccessFile(File file,String mode)
? ? ? ? * mode:操作模式笨觅,只讀或讀寫
? ? ? ? */
? ? ? ? RandomAccessFile raf
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./raf.dat","rw"
? ? ? ? ? ? );
? ? ? ? /*
? ? ? ? * void write(int d)
? ? ? ? * 寫出一個字節(jié)拦耐,將給定的int值對應(yīng)的2進
? ? ? ? * 制的“低八位”寫入文件
? ? ? ? *? ? ? ? ? ? ? ? ? ? ? ? ? ? vvvvvvvv
? ? ? ? * 00000000 00000000 00000001 00000001
? ? ? ? *
? ? ? ? * 11111111 11111111 11111111 11111111
? ? ? ? */
? ? ? ? raf.write(1);
? ? ? ? System.out.println("寫出完畢!");
? ? ? ? raf.close();
? ? }
}
2.讀取一個字節(jié)。
public class ReadDemo {
? ? public static void main(String[] args) throws IOException {
? ? ? ? RandomAccessFile raf
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./raf.dat","r"
? ? ? ? ? ? );
? ? ? ? /*
? ? ? ? * int read()
? ? ? ? * 讀取1個字節(jié)见剩,并以int形式返回杀糯。
? ? ? ? * 若返回值為-1.則表示讀取到了文件末尾。
? ? ? ? *? ? ? ? ? ? ? ? ? ? ? ? ? ? vvvvvvvv
? ? ? ? * 00000000 00000000 00000000 00000001
? ? ? ? */
? ? ? ? int d = raf.read();
? ? ? ? System.out.println(d);
? ? ? ? d = raf.read();
? ? ? ? System.out.println(d);
? ? ? ? raf.close();
? ? }
}
3.復(fù)制文件炮温。
public class CopyDemo {
? ? public static void main(String[] args) throws IOException {
? ? ? ? /*
? ? ? ? * 復(fù)制當(dāng)前目錄中的img.png
? ? ? ? */
? ? ? ? RandomAccessFile src
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./music.mp3","r"
? ? ? ? ? ? );
? ? ? ? RandomAccessFile desc
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./music_cp.mp3","rw"
? ? ? ? ? ? );
? ? ? ? int d = -1;//記錄每次讀取的字節(jié)數(shù)據(jù)
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? while((d = src.read())!=-1){
? ? ? ? ? ? desc.write(d);
? ? ? ? }
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println(
? ? ? ? ? ? "復(fù)制完畢!耗時:"+(end-start)+"ms");
? ? ? ? src.close();
? ? ? ? desc.close();
? ? }
}
4.提高每次讀寫的數(shù)據(jù)量火脉,減少實際讀寫的次數(shù),可以提高讀寫效率對于硬盤(磁盤)而言柒啤,隨機讀寫效率差是缺點。但是硬盤的塊讀寫效率還是可以保證的畸颅。隨機讀寫:單字節(jié)讀寫担巩。塊讀寫:一組一組字節(jié)讀寫。
public class CopyDemo2 {
? ? public static void main(String[] args) throws IOException {
? ? ? ? RandomAccessFile src
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./DG.exe","r"
? ? ? ? ? ? );
? ? ? ? RandomAccessFile desc
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "./DG_CP1.exe","rw"
? ? ? ? ? ? );
? ? ? ? /*
? ? ? ? * int read(byte[] data)
? ? ? ? * 一次性讀取給定的字節(jié)數(shù)組長度的字節(jié)量
? ? ? ? * 并存入到該數(shù)組中没炒,返回值為實際讀取到
? ? ? ? * 的字節(jié)量涛癌。若返回值為-1,則是文件末尾
? ? ? ? *
? ? ? ? * void write(byte[] data)
? ? ? ? * 一次性將給定的字節(jié)數(shù)組中所有字節(jié)寫出
? ? ? ? *
? ? ? ? * void write(byte[] data,int index,int len)
? ? ? ? * 將給定的字節(jié)數(shù)組從下標(biāo)index處開始的連續(xù)len
? ? ? ? * 個字節(jié)一次性寫出
? ? ? ? *
? ? ? ? * 1 byte = 8位2進制
? ? ? ? * 1 kb = 1024 byte
? ? ? ? * 1 mb = 1024 kb
? ? ? ? * 1 gb = 1024 mb
? ? ? ? * 1 tb = 1024 gb
? ? ? ? *
? ? ? ? */
? ? ? ? //10 kb
? ? ? ? byte[] buf = new byte[1024*10];
? ? ? ? int len = -1;//表示每次實際讀取到的字節(jié)量
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? while((len = src.read(buf))!=-1){
//? ? ? ? ? ? desc.write(buf);
? ? ? ? ? ? desc.write(buf,0,len);
? ? ? ? }
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("復(fù)制完畢!耗時:"+(end-start)+"ms");
? ? ? ? src.close();
? ? ? ? desc.close();
? ? }
}
5.讀寫基本類型數(shù)據(jù)送火,以及RAF指針的操作拳话。
public class RandomAccessFileDemo2 {
? ? public static void main(String[] args) throws IOException {
? ? ? ? RandomAccessFile raf
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "raf.dat","rw"
? ? ? ? ? ? );
? ? ? ? //獲取指針位置
? ? ? ? long pos = raf.getFilePointer();
? ? ? ? System.out.println("pos:"+pos);
? ? ? ? //向文件中寫入int最大值
? ? ? ? int max = Integer.MAX_VALUE;
? ? ? ? /*
? ? ? ? *? ? ? ? ? ? ? ? ? ? ? ? ? ? vvvvvvvv
? ? ? ? * 01111111 11111111 11111111 11111111
? ? ? ? * max>>>24
? ? ? ? * 00000000 00000000 00000000 01111111
? ? ? ? */
? ? ? ? raf.write(max>>>24);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? raf.write(max>>>16);
? ? ? ? raf.write(max>>>8);
? ? ? ? raf.write(max);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? /*
? ? ? ? * RAF提供了方便我們寫出基本類型的相關(guān)方法
? ? ? ? *
? ? ? ? */
? ? ? ? //一次性將給定的int值4字節(jié)全部寫出
? ? ? ? raf.writeInt(max);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? raf.writeLong(123L);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? raf.writeDouble(123.123);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? /*
? ? ? ? * void seek(long pos)
? ? ? ? * 移動指針到指定位置。
? ? ? ? */
? ? ? ? raf.seek(0);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? //讀取? EOF end of file
? ? ? ? int d = raf.readInt();
? ? ? ? System.out.println(d);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? //讀取long
? ? ? ? raf.seek(8);
? ? ? ? long l = raf.readLong();
? ? ? ? System.out.println("long:"+l);
? ? ? ? System.out.println("pos:"+raf.getFilePointer());
? ? ? ? double dou = raf.readDouble();
? ? ? ? System.out.println("double:"+dou);
? ? ? ? System.out.println("寫出完畢!");
? ? ? ? raf.close();
? ? }
}
6.讀取字符串种吸。
public class ReadStringDemo {
? ? public static void main(String[] args) throws IOException {
? ? ? ? RandomAccessFile raf
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "raf.txt","r"
? ? ? ? ? ? );
? ? ? ? byte[] data = new byte[(int)raf.length()];
? ? ? ? raf.read(data);
? ? ? ? /*
? ? ? ? * String提供了一組重載的構(gòu)造方法
? ? ? ? * 可以將給定的字節(jié)數(shù)組按照指定字符集還原為字符串
? ? ? ? */
? ? ? ? String str = new String(data,"GBK");
? ? ? ? System.out.println(str);
? ? ? ? raf.close();
? ? }
}
7.寫出字符串操作弃衍。
public class WriteStringDemo {
? ? public static void main(String[] args) throws IOException {
? ? ? ? /*
? ? ? ? * 在相對路徑中"./"可以不寫,不寫默認也是
? ? ? ? * 在當(dāng)前目錄中
? ? ? ? */
? ? ? ? RandomAccessFile raf
? ? ? ? ? ? = new RandomAccessFile(
? ? ? ? ? ? ? ? "raf.txt","rw"
? ? ? ? ? ? );
? ? ? ? /*
? ? ? ? * String提供的方法:
? ? ? ? * byte[] getBytes()
? ? ? ? * 將當(dāng)前字符串按照系統(tǒng)默認字符集轉(zhuǎn)換為
? ? ? ? * 一組字節(jié)坚俗。
? ? ? ? *
? ? ? ? * byte[] getBytes(String csn)
? ? ? ? * 將當(dāng)前字符串按照指定字符集轉(zhuǎn)換為
? ? ? ? * 一組字節(jié)镜盯。推薦使用這種方式轉(zhuǎn)換字符串
? ? ? ? * 因為按照系統(tǒng)默認字符集轉(zhuǎn)換會導(dǎo)致跨平
? ? ? ? * 臺出現(xiàn)亂碼問題
? ? ? ? *
? ? ? ? *
? ? ? ? * GBK:國標(biāo)編碼岸裙,中文占2字節(jié)
? ? ? ? * UTF-8:萬國碼,對unicode進行編碼,變長編碼集
? ? ? ? *? ? ? 英文1字節(jié)速缆,中文3字節(jié)
? ? ? ? * ISO8859-1:歐洲編碼集降允,不支持中文。
? ? ? ? */
? ? ? ? String str = "讓我再看你一遍艺糜,從南到北剧董。";
? ? ? ? byte[] data = str.getBytes("GBK");
? ? ? ? raf.write(data);
? ? ? ? raf.write(
? ? ? ? ? ? "像是北五環(huán)路,蒙住的雙眼破停。".getBytes("GBK")
? ? ? ? );
? ? ? ? System.out.println("寫出完畢!");
? ? ? ? raf.close();
? ? }
}
8.