# RandomAccessFile
> ## 基本操作
- 創建對象
```
/**
* file 為io.File類型
* mode 為模式為String類型,選擇模式,可選'r','w','rw'等模式對應‘讀’,‘寫’,‘讀寫’權限,
*
**/
new RandomAccessFile(file, mode)
```
- 查看長度(字節)
```
RandomAccessFile對象.length(); // 顯示RandomAccessFile對象文本的字節長度
```
- 查看當前指針位置(字節)
```
RandomAccessFile對象.getFilePointer(); // 顯示當前指針指向文本字節位置
```
- 設置指針位置(字節)
```
RandomAccessFile對象.seek(Long point) // 設置指針為long類型,指向文本具體字節數據
```
- 字符流讀寫
```
RandomAccessFile對象.readChar();
RandomAccessFile對象.writeChars(s); // s為String類型的寫入內容
```
- 字節流讀寫
```
RandomAccessFile對象.read(obj,begin,end); // obj為byte數組對象,begin為開始位置,end為結束位置
RandomAccessFile對象.write(b); // b 為byte類型的寫入內容
```
* [ ] 注意 write() 方法
該方法會從指定位置開始覆蓋寫入內容,并保持覆蓋前后文件大小一致, 例如 ABCD 從2的位置寫入 FF后結果為 AFFD;
> ## 應用
- 向指定文件追加內容(在文件末尾追加)
```
/*
* RandomAccessFile 向指定文件追加內容(在文件末尾追加)
*/
public void appendTest(String url) {
File file = new File(url);
String permision = "rw";
try {
if (!file.exists()) {
File parentFile = file.getParentFile();
if (parentFile.mkdirs()) {
file.createNewFile();
}
}
RandomAccessFile raf = new RandomAccessFile(file, permision);
raf.seek(raf.length());
System.out.println("raf.length:" + raf.length());
System.out.println("raf.getFilePointer:" + raf.getFilePointer());
String value = "這是追加內容!" + "\r\n";
raf.write(value.getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
```
- 從指定位置讀取內容
```
/*
* RandomAccessFile 從指定位置讀取內容
*/
public void readTest(String url) {
File file = new File(url);
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(20);// 設置指針位置
byte[] temp = new byte[1024];
int read = -1;
while ((read = raf.read(temp)) != -1) {
System.out.println(new String(temp, 0, read));
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
```
- 從指定位置插入指定的內容
```
/*
* RandomAccessFile 從指定位置插入指定的內容 注意: raf.write()
* 該方法會從指定位置開始覆蓋寫入內容,并保持覆蓋前后文件大小一致, 例如 ABCD 從2的位置寫入 FF后結果為 AFFD;
* 特別注意的是2是字節大小,所以當有中文字體被覆蓋時,可能使字體遭到破壞
*
* 因此要從指定位置插入指定的內容辦法解決辦法是:
* 1 先把指定位置后邊的內容寫入到一個臨時文件中;
* 2 然后在指定位置插入指定內容;
* 3 最后寫入臨時數據;
*/
public void insertTest(String url) {
try {
File file = new File(url);
File tempFile = File.createTempFile("tmp", null); //創建臨時文件
FileInputStream fileInputStream = new FileInputStream(tempFile);
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(20);
byte []buf = new byte[1024];
int read = -1;
while((read = raf.read(buf)) != -1) { //要插入的內容的地方后面內容寫入臨時文件
fileOutputStream.write(buf, 0, read);
}
raf.seek(20); //指針重新指到要插入的地方
String value = "插入de內容!!"+"\r\n";
raf.write(value.getBytes("UTF-8")); //插入內容
while((read = fileInputStream.read(buf)) != -1) { //插入臨時文件數據
raf.write(buf,0,read);
}
fileOutputStream.close();
fileInputStream.close();
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
```
- 第一章 java SE
- 1.1數據類型
- 1.2 流程控制語句
- 1.3 方法
- 1.4 面向對象三特性
- 1.5 對象數組與集合
- 1.6 數組和集合操作工具類
- 1.7 可變參數
- 1.8 String
- 1.9 StringBuilder
- 1.10 final&&finally&&finalize
- 1.11 抽象類與接口
- 1.12 基本數據類型的包裝類
- 1.13 泛型
- 1.14 內部類
- 1.15 throw & throws & try catch
- 1.16 線程
- 1.17 BeanUtils
- 1.18 java反射
- 1.19 序列化和反序列化
- 1.20 IO輸入輸出流
- 1.21 File
- 1.22 RandomAccessFile
- 1.23 第三方工具CommonsIO
- 1.24 java網絡傳輸
- 第二章 java EE
- 2.1 maven的配置
- 2.2 Cookie
- 2.3 EL表達式 JSTL
- 2.4 驗證相關
- 2.4.1 驗證碼
- 2.5 防重復提交
- 2.6 activeMq的使用
- 2.7 jtl的使用
- 2.8 Upload上傳文件
- 第三章 Spring相關
- 3.1 IOC/DI
- bean的生命周期
- bean的配置
- 3.2 Spring Aop
- 3.3 Spring Jdbc
- 3.4 事物相關
- 事物
- 事物的使用
- 3.5 MBG使用
- 第四章 解決問題方法
- 4.1 List轉換為Map
- 4.2 結果返回類
- 4.3 HSSF的使用
- 第五章 排序
- 5.1 冒泡排序
- 5.2 選擇排序
- 5.3 快速排序