zoukankan      html  css  js  c++  java
  • 关于RandomAccess

    package special;
    
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    /**
     * 随机访问流:
     * 
     * 此类不属于任何一个输入流和输出流
     * 直接继承自Object实现的类
     * 可以对文件随机的进行读和写!
     * @author mzy
     *
     *
     * JDK:
     * 此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。
     * 存在指向该隐含数组的光标或索引,称为文件指针;
     * 输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
     * 如果随机访问文件以读取/写入模式创建,则输出操作也可用;
     * 输出操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。
     * 写入隐含数组的当前末尾之后的输出操作导致该数组扩展。
     * 该文件指针可以通过 getFilePointer 方法读取,并通过 seek 方法设置。 
     * 
     * 通常,如果此类中的所有读取例程在读取所需数量的字节之前已到达文件末尾,则抛出 EOFException(是一种 IOException)。
     * 如果由于某些原因无法读取任何字节,而不是在读取所需数量的字节之前已到达文件末尾,则抛出 IOException,而不是 EOFException。
     * 需要特别指出的是,如果流已被关闭,则可能抛出 IOException。 
     */
    public class RandomAccessFileDemo {
    	// 第二个参数为mode:一共四种模式:
    	// 模式常用的 "r" 和 "rw"
    	// r read
    	// rw read and write
    	/* RandomAccessFile(File file, String mode) */
    	/* RandomAccessFile(String name, String mode) */
    	public static void main(String[] args) throws IOException {
    		write();
    		read();
    	}
    
    	private static void read() throws IOException {
    		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
    		
    		byte a = raf.readByte();
    		System.out.println("byte:" + a);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		int b = raf.readInt();
    		System.out.println("int:" + b);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		char c = raf.readChar();
    		System.out.println("char:" + c);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		boolean d = raf.readBoolean();
    		System.out.println("boolean:" + d);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		float e = raf.readFloat();
    		System.out.println("float:" + e);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		double f = raf.readDouble();
    		System.out.println("double:" + f);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		long g = raf.readLong();
    		System.out.println("long:" + g);
    		System.out.println("当前指针位置为:" + raf.getFilePointer());
    		
    		String h = raf.readUTF();
    		
    		// 2 + 3 + 3
    		System.out.println("UTF:" + h);
    		System.out.println("当前的指针位置为:" + raf.getFilePointer());
    		
    		raf.seek(5);
    		char ch = raf.readChar();
    		System.out.println("定位指针为5: ch = " + ch);
    		
    		
    	}
    
    	private static void write() throws IOException {
    		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
    		
    		raf.writeByte(100);
    		raf.writeInt(1000);
    		raf.writeChar('b');
    		raf.writeBoolean(true);
    		raf.writeFloat(12.34F);
    		raf.writeDouble(12.48);
    		raf.writeLong(123456789);
    		raf.writeUTF("中国");
    		
    		// 不提供flush方法
    		raf.close();
    	}
    }
    


  • 相关阅读:
    Python全栈-第四课 学习笔记
    Python全栈-第三课 学习笔记
    Python全栈-第二课 学习笔记
    计算机网络技术-交换基础和Vlan间路由 学习笔记
    Linux运维-04系统管理和操作命令
    Linux运维-03远程连接
    Linux运维-02操作系统和系统安装(虚拟机)
    Linux运维-01硬件组成概念介绍
    计算机网络技术-路由-OSPF 学习笔记
    python基础 day17 模拟博客园登录
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053442.html
Copyright © 2011-2022 走看看