public static void readBigFile(File file) throws IOException {
String fileName = file.getAbsolutePath();
RandomAccessFile randomFile = null;
randomFile = new RandomAccessFile(fileName, "r");
long fileLength = randomFile.length();
System.out.println("文件大小:" + fileLength);
long start = 0;
if (fileLength > 300) {
start = (long) (Math.random() * 50) + (fileLength - 300);
}
randomFile.seek(start);
// byte[] bytes = new byte[512];
int byteread = 0;
String currentLing = randomFile.readLine();
Log.e("random", currentLing);
String nextLine = randomFile.readLine();
Log.e("random", nextLine);
if (randomFile != null) {
randomFile.close();
}
}
String fileName = file.getAbsolutePath();
RandomAccessFile randomFile = null;
randomFile = new RandomAccessFile(fileName, "r");
long fileLength = randomFile.length();
System.out.println("文件大小:" + fileLength);
long start = 0;
if (fileLength > 300) {
start = (long) (Math.random() * 50) + (fileLength - 300);
}
randomFile.seek(start);
// byte[] bytes = new byte[512];
int byteread = 0;
String currentLing = randomFile.readLine();
Log.e("random", currentLing);
String nextLine = randomFile.readLine();
Log.e("random", nextLine);
if (randomFile != null) {
randomFile.close();
}
}
本文介绍了一种通过RandomAccessFile类来随机访问并读取大文件的方法,这种方法能够有效地处理文件长度超过限定值的情况,避免一次性加载整个文件到内存中导致的性能问题。
1008

被折叠的 条评论
为什么被折叠?



