zoukankan      html  css  js  c++  java
  • Java文件 ---RandomAccessFile示例

    RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了。这些记录的大小不必相同;但是其大小和位置必须是可知的。但是该类仅限于操作文件

    MulitWriteFile类:程序入口——写入数据、读取数据

    public class MulitWriteFile {
        
        static File file = new File("RandomAccessFile.txt");
    
        public static void main(String[] args) {
            
            /* 与线程结合向文件中写入数据 */
            if (file.exists()) {
                file.delete();
            }
            new WriteFile(file, 5).start();
            new WriteFile(file, 3).start();
            new WriteFile(file, 1).start();
            new WriteFile(file, 4).start();
            new WriteFile(file, 2).start();
            
            /* 读取文件指定位置的数据 */
            RandomAccessFile raf = null;
            try {
                raf = new RandomAccessFile(file, "r");
                raf.seek(300);
                byte[] str = new byte[20];
                raf.read(str);
                String in = new String(str);
                System.out.println(in);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (raf != null) {
                        raf.close();
                    }
                } catch (Exception e) {
                }
            }
        }
    }

    WriteFile类(线程):RandomAccessFile用法——向文件指定位置写入数据

    public class WriteFile extends Thread {
    
        /** 要写入的文件 */
        File file;
        /** 要插入的区块(位置) */
        int block;
        /** 一个区块的长度 */
        int L = 100;
        
        /**
         * File
         *            |***        |+++
         * |-----------***---------+++--------------------------|
         *         |---
         */
        
        /**
         * 1            2            3            4            5(2)
         * |------------|------------|------------|------------|------------|
         * 0xL            1xL
         * 
         * @param f
         * @param b
         */
    
        public WriteFile(File f,int b){
            file = f;
            block = b;
        }
        
        @Override
        public void run() {
            RandomAccessFile raf = null;
            try {
                // 得到需要读写的文件
                raf = new RandomAccessFile(file, "rw");
                // 将指针移动到指定位置
                raf.seek((block-1)*L);
                // 从指定位置开始写入数据
                raf.writeBytes("This is block"+block);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (raf != null) {
                    try {
                        raf.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
  • 相关阅读:
    最小割树
    POJ2774 很长的信息
    决战 状压dp
    confd + Nacos | 无代码侵入的配置变更管理
    阿里云应用高可用服务 AHAS 流控降级实现 SQL 自动防护功能
    Web应用托管服务(Web+)隐藏的十个上云最佳姿势
    Java 函数优雅之道
    探索云网络技术前沿,Sigcomm 2019 阿里云参会分享
    MaxCompute 最新特性介绍 | 2019大数据技术公开课第三季
    阿里巴巴大数据产品最新特性介绍 | 2019大数据技术公开课第四季
  • 原文地址:https://www.cnblogs.com/xiaobaizhiqian/p/7757720.html
Copyright © 2011-2022 走看看