Java的簡單理解(25)---(隨機訪問檔案)RandomAccessFile
RandomAccessFile
/**
* 檔案的分割
* 1. 分割的塊數 n塊
* 2. 每一塊的大小 blocksize
* 3. 最後:總的檔案大小 - (n - 1) * blocksize
*/
public void test(){
try {
File file = new File("E:/xp/test/a.txt");
RandomAccessFile rnd = new RandomAccessFile(file,"r");
rnd.seek(10);
byte[] car = new byte[1024];
int len = 0;
while ((len = rnd.read(car)) != -1) {
if (len >= 200) {
System.out.println(new String(car,0,120));
} else {
System.out.println(new String(car,0,len));
}
}
rnd.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
檔案分割的思路
- 第一步:分割的準備
- 塊數
- 確定每塊大小
- 每塊的名稱
- 第二步
- 分割
- 第幾塊,每塊的起點,實際大小
- 檔案分割
public class SplitFile {
// 檔案的路徑
private String filePath;
// 檔名
private String fileName;
// 檔案大小
private long length;
// 塊數
private int size;
// 每塊的大小
private long blockSize;
// 每塊的名稱
private List<String> blockPath;
public SplitFile() {
blockPath = new ArrayList<>();
}
public SplitFile(String filePath) {
this(filePath,1024);
}
public SplitFile(String filePath,long blockSize){
this();
this.filePath = filePath;
this.blockSize = blockSize;
init();
}
/**
* 初始化操作,計算塊數,確定檔名
*/
public void init() {
File src = null;
if (filePath == null || !((src = new File(filePath)).exists())){
return;
}
if (src.isDirectory()) {
return;
}
// 檔名
this.fileName = src.getName();
// 檔案的實際大小
this.length = src.length();
// 修正每塊大小
if (this.blockSize > length) {
this.blockSize = length;
}
// 確定塊數
size = (int) Math.ceil(length * 1.0 / this.blockSize);
}
/**
* 確定檔名
*/
public void initPathName(String destPath) {
for (int i = 0; i < size; i++){
this.blockPath.add(destPath + "/" + this.fileName + ".part" + i);
}
}
/**
* 檔案的分割
* @param destPath 分割檔案存放目錄
*/
public void split(String destPath){
// 確定檔案的路徑
initPathName(destPath);
long beginPos = 0;// 起始點
long actualBlockSize = blockSize;//實際大小
// 計算所有塊的大小
for (int i = 0; i < size; i++) {
if (i == size - 1){
actualBlockSize = this.length - beginPos;
}
spiltDetail(i,beginPos,actualBlockSize);
beginPos = beginPos + actualBlockSize;
}
}
/**
* 檔案的分割 輸入 輸出
* 檔案的拷貝
* @param idx 第幾塊
* @param beginPos 起始點
* @param actualBlockSize 實際大小
*/
public void spiltDetail(int idx,long beginPos,long actualBlockSize){
// 1. 建立源
File src = new File(this.filePath);
// 2. 目標檔案
File dest = new File(this.blockPath.get(idx));
// 3. 選擇流
RandomAccessFile raf = null;
BufferedOutputStream bos = null;
try {
raf = new RandomAccessFile(src,"r");
bos = new BufferedOutputStream(new FileOutputStream(dest));
// 讀取檔案
raf.seek(beginPos);
// 快取
byte[] flush = new byte[1024];
// 接收長度
int len = 0;
while (-1 != (len = raf.read(flush))) {
// 寫出
if (actualBlockSize - len >= 0) {
bos.write(flush,0,len);
actualBlockSize = actualBlockSize - len;
} else {
bos.write(flush,0, (int) actualBlockSize);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
合併檔案的兩種方法
1.
public void mergeFile1(String destPath) {
// 建立源
File dest = new File(destPath);
// 選擇流
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(dest,true)); // true: 追加而不是替換
for (int i = 0; i < this.blockPath.size(); i++) {
bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));
// 緩衝區
byte[] flush = new byte[1024];
// 接收長度
int len = 0;
while ((len = bis.read(flush)) != -1) {
bos.write(flush,0,len);
}
bos.flush();
bis.close();
bos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.
public void mergeFile2(String destPath) {
// 建立源
File file = new File(destPath);
// 選擇流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
SequenceInputStream sis = null;
// 建立一個容器
Vector<InputStream> vector = new Vector<>();
for (int i = 0; i < this.blockPath.size(); i++){
try {
vector.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
try {
bos = new BufferedOutputStream(new FileOutputStream(file,true));
sis = new SequenceInputStream(vector.elements());
byte[] flush = new byte[1024];
int len = 0;
while ((len = sis.read(flush)) != -1) {
bos.write(flush,0,len);
}
bos.flush();
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
相關文章
- java RandomAccessFile類(隨機訪問檔案)JavarandomMac隨機
- [java IO流]之 隨機訪問檔案(RandomAccessFile類)Java隨機randomMac
- 關於檔案讀寫使用RandomAccessFile類的一些簡單操作randomMac
- scala簡要:檔案訪問
- java中獲得檔案大小,用RandomAccessFile的lengh方法JavarandomMac
- Java斷點續傳(基於socket與RandomAccessFile的簡單實現)Java斷點randomMac
- Java的簡單理解(2)Java
- 檔案中隨機讀取行問題隨機
- 簡單的Bindservice服務獲取隨機數,需要在清單檔案中註冊service隨機
- 簡單的:Div隨機顏色隨機
- RAC中對DB引數檔案的簡單理解
- C 語言標頭檔案作用的簡單理解
- Java中如何使用隨機存取檔案RandomAcessFile類?Java隨機randomMac
- 訪問ASM中的檔案ASM
- 生成固定大小的隨機檔案隨機
- 超級簡單:在asp.net中限制下載檔案的訪問許可權ASP.NET訪問許可權
- GFS(谷歌檔案系統)的JAVA簡單實現谷歌Java
- JavaScriptFAQ(十九)——檔案訪問JavaScript
- 手機將PDF檔案壓縮的簡單方法
- java介面和抽象類的簡單理解Java抽象
- Java的簡單理解(22)---處理流Java
- 檔案共享伺服器的超簡單玩法教程,開啟網頁就能遠端訪問伺服器網頁
- 檔案和目錄的訪問控制(2)新增訪問控制
- 簡單的ANT配置檔案
- [Java]RandomAccessFile類詳解JavarandomMac
- Java IO2:RandomAccessFileJavarandomMac
- EJB 訪問檔案系統的疑惑
- Perl IO:隨機讀寫檔案隨機
- java io 多種檔案操作方式(位元組、字元、行、隨機)Java字元隨機
- 關於Java註解(annotation)的簡單理解Java
- 關於MongoDB的簡單理解(二)--Java篇MongoDBJava
- 簡單理解Java中的4種迴圈Java
- 最簡單的授權檔案
- 使用簡單的Python連線訪問MongoDBPythonMongoDB
- ES6最簡單的方式訪問MongoDBMongoDB
- PLSQL訪問網頁簡單案例(二)SQL網頁
- PLSQL訪問網頁簡單案例(一)SQL網頁
- C#實現的簡單的隨機抽號器C#隨機