0

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();

                    }
Rookie
  • 8,660
  • 17
  • 58
  • 91
  • If you don't want to use RandomAccessFile, what would you prefer? – JRideout Dec 26 '11 at 12:53
  • 1
    http://docs.oracle.com/javase/tutorial/essential/io/file.html Find info on ByteChannels – Kris Dec 26 '11 at 12:54
  • And what is the reason why you _don't_ want to use RandomAccessFile, if it does the job? – fge Dec 26 '11 at 12:59
  • The thing is that i am monitoring the log file which is generated continously and that log file is using Rolling file appender which is supposed to create back up file and RandomAcess file is hindering that process . http://stackoverflow.com/questions/3612115/java-rolling-file-creation-fails-when-attempting-to-read-simultaneously – Rookie Dec 26 '11 at 13:28

1 Answers1

1

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;
    }    
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I would like to get the data as line as i want to parse the whole log line ans send it to mail according t the severity specified.Dont you think this way there will be extra overhead doin that. – Rookie Dec 27 '11 at 06:30
  • The file is not guaranteed to have whole lines so you cannot assume there is be whole lines. Instead you need to scan the data yourself and save the bytes without a new line for the next read. There is a small overhead but its much the same as using BufferedReader and very small compared with the cost of reading IO. – Peter Lawrey Dec 27 '11 at 09:57