Java

Java IO流(RandomAccessFile)

2019-10-03  本文已影响0人  一亩三分甜

RandomAccessFile:该类不是IO体系中的子类。而是直接继承自Object。但是它是IO包中成员,因为它具备读和写功能。内部封装了一个数组,而且通过指针对数组的元素进行操作。可以通过getFilePointer获取指针位置,同时可以通过seek改变指针的位置。其实完成读写的原理就是内部封装了字节输入流和输出流。通过构造函数可以看出,该类只能操作文件。而且操作文件还有模式:只读,读写rw等。而且该对象的构造函数要操作的文件不存在,会自动创建,如果存在则不会覆盖。

如果模式为只读r,不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常。

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException
    {
//        writeFile();
        readFile();
    }
    public static void readFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
        byte[] buf = new byte[6];
        raf.read(buf);
        String name = new String(buf);
        int age = raf.readInt();
        System.out.println("name="+name);
        System.out.println("age="+age);
        raf.close();
    }
    public static void writeFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
        raf.write("李四".getBytes());
        raf.writeInt(97);
        raf.write("王五".getBytes());
        raf.writeInt(99);
        raf.close();
    }
}
//输出
name=李四
age=97

seek

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException
    {
//        writeFile();
        readFile();
    }
    public static void readFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
        byte[] buf = new byte[6];
        //调整对象中指针
        raf.seek(10);
        raf.read(buf);
        String name = new String(buf);
        int age = raf.readInt();
        System.out.println("name="+name);
        System.out.println("age="+age);
        raf.close();
    }
    public static void writeFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
        raf.write("李四".getBytes());
        raf.writeInt(97);
        raf.write("王五".getBytes());
        raf.writeInt(99);
        raf.close();
    }
}
//输出
name=王五
age=99

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException
    {
//        writeFile(); 
//        readFile();
        writeFile_2();
    }
    public static void writeFile_2() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
        raf.seek(10*0);
        raf.write("周七".getBytes());
        raf.writeInt(103);
        raf.close();
    }
    public static void readFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
        byte[] buf = new byte[6];
        //调整对象中指针
//        raf.seek(10);
        //跳过指定的字节数
        raf.skipBytes(10);
        raf.read(buf);
        String name = new String(buf);
        int age = raf.readInt();
        System.out.println("name="+name);
        System.out.println("age="+age);
        raf.close();
    }
    public static void writeFile() throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
        raf.write("李四".getBytes());
        raf.writeInt(97);
        raf.write("王五".getBytes());
        raf.writeInt(99);
        raf.close();
    }
}
Snip20191003_2.png
上一篇 下一篇

猜你喜欢

热点阅读