Java: reading strings from a random access file with buffered input

后端 未结 7 1832
醉话见心
醉话见心 2020-12-14 12:40

I\'ve never had close experiences with Java IO API before and I\'m really frustrated now. I find it hard to believe how strange and complex it is and how hard it could be to

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 13:31

    I wrote this code to read utf-8 using randomaccessfiles

    //File: CyclicBuffer.java
    public class CyclicBuffer {
    private static final int size = 3;
    private FileChannel channel;
    private ByteBuffer buffer = ByteBuffer.allocate(size);
    
    public CyclicBuffer(FileChannel channel) {
        this.channel = channel;
    }
    
    private int read() throws IOException {
        return channel.read(buffer);
    }
    
    /**
     * Returns the byte read
     *
     * @return byte read -1 - end of file reached
     * @throws IOException
     */
    public byte get() throws IOException {
        if (buffer.hasRemaining()) {
            return buffer.get();
        } else {
            buffer.clear();
            int eof = read();
            if (eof == -1) {
                return (byte) eof;
            }
            buffer.flip();
            return buffer.get();
        }
    }
    }
    //File: UTFRandomFileLineReader.java
    
    
    public class UTFRandomFileLineReader {
    private final Charset charset = Charset.forName("utf-8");
    private CyclicBuffer buffer;
    private ByteBuffer temp = ByteBuffer.allocate(4096);
    private boolean eof = false;
    
    public UTFRandomFileLineReader(FileChannel channel) {
        this.buffer = new CyclicBuffer(channel);
    }
    
    public String readLine() throws IOException {
        if (eof) {
            return null;
        }
        byte x = 0;
        temp.clear();
    
        while ((byte) -1 != (x = (buffer.get())) && x != '\n') {
            if (temp.position() == temp.capacity()) {
                temp = addCapacity(temp);
            }
            temp.put(x);
        }
        if (x == -1) {
            eof = true;
        }
        temp.flip();
        if (temp.hasRemaining()) {
            return charset.decode(temp).toString();
        } else {
            return null;
        }
    }
    
    private ByteBuffer addCapacity(ByteBuffer temp) {
        ByteBuffer t = ByteBuffer.allocate(temp.capacity() + 1024);
        temp.flip();
        t.put(temp);
        return t;
    }
    
    public static void main(String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("/Users/sachins/utf8.txt",
                "r");
        UTFRandomFileLineReader reader = new UTFRandomFileLineReader(file
                .getChannel());
        int i = 1;
        while (true) {
            String s = reader.readLine();
            if (s == null)
                break;
            System.out.println("\n line  " + i++);
            s = s + "\n";
            for (byte b : s.getBytes(Charset.forName("utf-8"))) {
                System.out.printf("%x", b);
            }
            System.out.printf("\n");
    
        }
    }
    }
    

提交回复
热议问题