Home > Java > javaTutorial > How to access randomly in Java's RandomAccessFile class

How to access randomly in Java's RandomAccessFile class

WBOY
Release: 2023-04-28 09:31:06
forward
1024 people have browsed it

1. Process

(1) Can act as an input stream or dilute an output stream

(2) Supports reading from the beginning of the file , write

(3) Support reading and writing (insertion) from any location

(4) The RandomAccessFile class needs to specify the access mode:

2. Example

    public void RandomAccessFile(String src, String srcMode, String dest, String destMode) {
        RandomAccessFile accessFile = null;
        RandomAccessFile accessFile1 = null;
        try {
            accessFile = new RandomAccessFile(new File(src), srcMode);
            accessFile = new RandomAccessFile(new File(dest), destMode);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = accessFile.read(bytes)) != -1) {
                accessFile1.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (accessFile != null)
                try {
                    accessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
 
            if (accessFile1 != null) {
                try {
                    accessFile1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
Copy after login

The above is the detailed content of How to access randomly in Java's RandomAccessFile class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template