1.使用RandomAccessFile寫檔案資料(一位元組)
file.write(int d);——>int低八位寫出
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* java.io.RandomAccessFile
* 用于讀寫檔案資料的類
* RAF讀寫檔案資料總是在指針目前位置進行讀或寫,
* 并且讀寫後指針會自動後移。
* 指針是指向檔案資料位置的标記(底層實作)。
* @author soft01
*
*/
public class RandomAccessFile_write {
public static void main(String[] args) throws IOException {
/*
* 第二個參數是讀寫模式,常用的有:
* "r":隻讀模式,該模式要求讀取的檔案必須存在
* "rw":讀寫模式,該模式若檔案不存在會自動建立
* read write
*/
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
/*
* void write(int d)
* 向檔案中寫入1個位元組,寫的是給定int值對應2進制的“低八位”
* vvvvvvvv
* 00000000 00000000 00000000 00000001
*/
raf.write(1);;
System.out.println("寫出完畢");
//讀寫完畢最終要close
raf.close();
}
}
2.使用RandomAccessFile讀檔案資料(一位元組)
file.read()——>讀取一位元組
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 讀取一個位元組
* @author soft01
*
*/
public class RandomAccessFile_read {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
/*
* 讀取一個位元組,并以int形式傳回
* 若傳回值為-1表示讀取到了檔案末尾
*/
int d = raf.read();
System.out.println(d);
//由于檔案隻有一次位元組,再次讀取會傳回-1
d = raf.read();
System.out.println(d);//-1
raf.close();
}
}
3.使用RandomAccessFile複制檔案資料(一位元組複制)
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CopyDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile src = new RandomAccessFile("0.png","r");
RandomAccessFile desc = new RandomAccessFile("1.png","rw");
long start = System.currentTimeMillis();
int d=-1;
while((d=src.read())!=-1) {
desc.write(d);
}
long end = System.currentTimeMillis();
System.out.println("複制完畢!耗時"+(end-start));
src.close();
desc.close();
}
}
4.使用RandomAccessFile複制檔案資料(多位元組複制)
write(byte[] data,int offset,int len)——>多位元組複制
read(byte[] data)——>将數組中的所有位元組讀出
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 提高每次讀寫的資料
* @author soft01
*
*/
public class CopyDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile src = new RandomAccessFile("0.png","r");
RandomAccessFile desc = new RandomAccessFile("1.png","rw");
/*
* RAF提供了批量讀寫位元組的方法:
* int read(byte[] data)
* 一次性讀取給定位元組數組總長度的位元組量并将讀到的位元組存入到該數組中。
* 傳回值為實際讀取到的位元組量,若傳回值為-1表示本次沒有讀取到任何位元組(檔案末尾讀取)
*/
byte[] data = new byte[1024*10];//10k
int len = -1;//每次實際讀取到的位元組數
long start = System.currentTimeMillis();
while((len=src.read(data))!=-1) {
/*
* write(byte[] data)
* 一次性将給定位元組數組中的所有位元組寫出
*
* write(byte[] data,int offset,int len)
* 将給定位元組數組從下标為offset處的連續len一次性寫出
*/
desc.write(data,0,len);
}
long end = System.currentTimeMillis();
System.out.println("複制完畢!耗時:"+(end-start));
}
}
5.使用RandomAccessFile在檔案裡寫字元串
write(byte[] data)——>寫字元串
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 使用RandomAccessFile寫字元串
* @author soft01
*
*/
public class RAF_write {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
String str = "hello";
byte[] data = str.getBytes("UTF-8");//裡面可以不填,也可以填編碼方式
raf.write(data);
System.out.println("寫出完畢");
raf.close();
}
}
6.使用RandomAccessFile讀取檔案内容
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAF_read {
public static void main(String[] args) throws IOException {
/*
* 将raf.txt檔案中的内容讀取出來
*/
RandomAccessFile raf = new RandomAccessFile("raf.txt","r");
byte[] data = new byte[100];
int len = raf.read(data);
/**
* String(byte[] data)
* 将給定的位元組數組中所有位元組按照系統預設的字元集轉換為對應的字元串
*
* String(byte[] data,int offset,int len)
* 将給定的位元組數組中從下标為offset處開始的連續len個位元組
* 按照按照系統預設的字元集轉換為對應的字元串
*/
String str = new String(data,0,len,"utf-8");
System.out.println(str);
raf.close();
}
}
7.使用RandomAccessFile讀寫基本類型資料以及對于指針的操作
file.getFilePointer()——>擷取目前RandomAccessFile的指針位置
file.seek(0);——>移動RandomAccessFile的指針位置
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RAF讀寫基本類型資料以及RAF對于指針的操作
* @author soft01
*
*/
public class RAF_wr_type {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
/*
* long getFilePointer()
* 擷取檔案指針位置
*/
long pos = raf.getFilePointer();
System.out.println("pos:"+pos);//0
int imax = Integer.MAX_VALUE;
/*
* int最大值的2進制形式:
* vvvvvvvv
* 01111111 11111111 11111111 11111111
*/
raf.write(imax>>>24);
System.out.println("pos:"+raf.getFilePointer());
raf.write(imax>>>16);
System.out.println("pos:"+raf.getFilePointer());
raf.write(imax>>>8);
raf.write(imax);
/*
* 一次性寫出4個位元組,将給定的int值寫出
*/
raf.writeInt(imax);
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());
//操作指針
raf.seek(0);
System.out.println("pos:"+raf.getFilePointer());
int d = raf.readInt();
System.out.println(d);
System.out.println("pos:"+raf.getFilePointer());
//讀取long
//1:移動指針到long值的第一個位元組的所在位置
raf.seek(8);
//2:連續讀取8個位元組還原為該long值
long l = raf.readLong();
System.out.println(l);
System.out.println("pos:"+raf.getFilePointer());
double dou = raf.readDouble();
System.out.println(dou);
System.out.println("pos:"+raf.getFilePointer());
raf.close();
}
}
8.使用RandomAccessFile解析emp.dat檔案,将10個員工資訊輸出
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 完成簡易記事本程式,使用檔案流
* 解析emp.dat檔案,将10個員工資訊輸出
* @author soft01
*
*/
public class Test {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("emp.dat","r");
for(int i=0;i<10;i++) {
//讀取員工姓名 32位元組的字元串
String name = readString(raf,32);
//讀取年齡
int age = raf.readInt();
//讀取性别
String gender = readString(raf,10);
//讀取工資
int salary = raf.readInt();
//讀取入職日期
String hiredate = readString(raf,30);
System.out.println(name+","+age+","+gender+","+","+salary+","+hiredate);
}
/*for(int i=0;i<10;i++) {
//讀取員工姓名 32位元組的字元串
byte[] data = new byte[32];
raf.read(data);
String name = new String(data).trim();
System.out.println("name:"+name);
System.out.println("pos:"+raf.getFilePointer());
//讀取年齡
int age = raf.readInt();
System.out.println("age:"+age);
System.out.println("pos:"+raf.getFilePointer());
//讀取性别
data = new byte[10];
raf.read(data);
String gender = new String(data).trim();
System.out.println("gender:"+gender);
System.out.println("pos:"+raf.getFilePointer());
//讀取工資
int salary = raf.readInt();
System.out.println("salary:"+salary);
System.out.println("pos:"+raf.getFilePointer());
//讀取入職日期
data = new byte[30];
raf.read(data);
String hiredate = new String(data).trim();
System.out.println("hiredate"+hiredate);
System.out.println("pos:"+raf.getFilePointer());
}*/
raf.close();
}
public static String readString(RandomAccessFile raf,int len) throws IOException {
byte[] date = new byte[len];
raf.read(date);
return new String(date).trim();
}
}