RandomAccessFile 是 Android 中提供的一個類,它允許你在文件的任意位置讀取或寫入數據。這個類在處理大文件時非常有用,因為它不需要一次性將整個文件加載到內存中。你可以在應用中使用 RandomAccessFile 來實現以下功能:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = new File("path/to/your/file.txt");
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
long fileSize = randomAccessFile.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
// 從文件的指定位置開始讀取
long position = 50;
randomAccessFile.seek(position);
while ((bytesRead = randomAccessFile.read(buffer)) != -1) {
// 處理讀取到的數據
String data = new String(buffer, 0, bytesRead);
System.out.println(data);
}
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = new File("path/to/your/file.txt");
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
long fileSize = randomAccessFile.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
// 從文件的指定位置開始寫入
long position = 50;
randomAccessFile.seek(position);
String dataToWrite = "This is a new line of text.";
byte[] dataBytes = dataToWrite.getBytes();
randomAccessFile.write(dataBytes);
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = new File("path/to/your/file.txt");
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
long fileSize = randomAccessFile.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
// 從文件的指定位置開始更新
long position = 50;
randomAccessFile.seek(position);
String dataToWrite = "This is an updated line of text.";
byte[] dataBytes = dataToWrite.getBytes();
// 將原數據讀取到緩沖區
randomAccessFile.read(buffer);
// 將緩沖區的數據寫入到指定位置
System.arraycopy(dataBytes, 0, buffer, 0, dataBytes.length);
randomAccessFile.write(buffer, 0, dataBytes.length);
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = new File("path/to/your/file.txt");
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
long fileSize = randomAccessFile.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
// 從文件的指定位置開始刪除
long position = 50;
randomAccessFile.seek(position);
// 將后面的數據寫入到當前位置,從而實現刪除操作
String remainingData = readRemainingData(randomAccessFile, position + 10);
randomAccessFile.write(remainingData.getBytes());
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String readRemainingData(RandomAccessFile randomAccessFile, long position) throws IOException {
StringBuilder remainingData = new StringBuilder();
long fileSize = randomAccessFile.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
randomAccessFile.seek(position);
while (position < fileSize) {
bytesRead = randomAccessFile.read(buffer);
if (bytesRead == -1) {
break;
}
remainingData.append(new String(buffer, 0, bytesRead));
position += bytesRead;
}
return remainingData.toString();
}
}
這些示例展示了如何在 Android 應用中使用 RandomAccessFile 來讀取、寫入、更新和刪除文件的一部分。你可以根據自己的需求調整代碼以滿足你的應用場景。