I want to read the text file and after that get the offset to which file is read.I tried the following program but the thing is i dont want to use RandomAccessFile,how can i do that.
RandomAccessFile access = null;
try {
access = new RandomAccessFile(file, "r");
if (file.length() < addFileLen) {
access.seek(file.length());
} else {
access.seek(addFileLen);
}
} catch (Exception e) {
e.printStackTrace();
}
String line = null;
try {
while ((line = access.readLine()) != null) {
System.out.println(line);
addFileLen = file.length();
}
If you want to read a file continuously you can do the following. This works by not actually reading the end of the file. The problem you have is that you might not have a complete line or even a complete multi-byte character at the end.
class FileUpdater {
private static final long MAX_SIZE = 64 * 1024;
private static final byte[] NO_BYTES = {};
private final FileInputStream in;
private long readSoFar = 0;
public FileUpdater(File file) throws FileNotFoundException {
this.in = new FileInputStream(file);
}
public byte[] read() throws IOException {
long size = in.getChannel().size();
long toRead = size - readSoFar;
if (toRead > MAX_SIZE)
toRead = MAX_SIZE;
if (toRead == 0)
return NO_BYTES;
byte[] bytes = new byte[(int) toRead];
in.read(bytes);
readSoFar += toRead;
return bytes;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.