zoukankan      html  css  js  c++  java
  • RandomAccessFile读取文本简介

    RandomAccessFile类的常用的操作方法
    1、public  RandomAccessFile(File file, String mode)throws FileNotFoundException  构造方法  接收File类的对象,指定操作路径,但是在设置时需要设置模式:"r": 只读、"w": 只写、"rw": 读写。
    2、public RandomAccessFile(String name, String mode) throws FileNotFoundException 构造方法 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径。
    3、public void close() throws IOException   关闭操作
    4、public int read(byte[] b) throws IOException 将内容读取到一个byte数组之中
    5、public final byte readByte()  throws IOException 读取一个字节
    6、public final int readInt()  throws IOException从文件中读取整型数据。
    7、public void seek(long pos) throws IOException 设置读指针的位置。
    8、public final void writeBytes(String s) throws IOException 将一个字符串写入到文件之中,按字节的方式处理。
    9、public final void writeInt(int v) throws IOException 将一个int型数据写入文件,长度为4位。
    10、public int skipBytes(int n) throws IOException 指针跳过多少个字节。
    构造:public RandomAccessFile(File file, String mode)  throws FileNotFoundException
    实例化此类的时候需要传递File类,告诉程序应该操作的是哪个文件,之后有一个模式,文件的打开模式,常用的两种模式:
    r:读模式
    w:只写
    rw:读写,如果使用此模式,如果此文件不存在,则会自动创建。 
     
    用RandomAccessFile正序读取每一行文本
    public class everyline {
    
    	public static void main(String[] args) throws IOException, IOException {
    		// TODO Auto-generated method stub
    		 RandomAccessFile rf = new RandomAccessFile("E:\databike.txt", "r");
    
    		 String line=null;
    		 while((line=rf.readLine())!=null)
    		 {
    			 //System.out.println(line);
    			 System.out.println(new String(line.getBytes("ISO-8859-1"), "gbk"));
    		 }
    	}
    
    }
    

    读取任意位置的数据

        /** 
         * 读的方法 
         * @param path 文件路径 
         * @param pointe 指针位置 
         * **/  
        public static void randomRed(String path,int pointe){  
            try{  
                //RandomAccessFile raf=new RandomAccessFile(new File("D:\3\test.txt"), "r");  
                /** 
                 * model各个参数详解 
                 * r 代表以只读方式打开指定文件 
                 * rw 以读写方式打开指定文件 
                 * rws 读写方式打开,并对内容或元数据都同步写入底层存储设备 
                 * rwd 读写方式打开,对文件内容的更新同步更新至底层存储设备 
                 *  
                 * **/  
                RandomAccessFile raf=new RandomAccessFile(path, "r");  
                //获取RandomAccessFile对象文件指针的位置,初始位置是0  
                System.out.println("RandomAccessFile文件指针的初始位置:"+raf.getFilePointer());  
                raf.seek(pointe);//移动文件指针位置  
                byte[]  buff=new byte[1024];  
                //用于保存实际读取的字节数  
                int hasRead=0;  
                //循环读取  
                while((hasRead=raf.read(buff))>0){  
                    //打印读取的内容,并将字节转为字符串输入  
                    System.out.println(new String(buff,0,hasRead));    
                }  
            }catch(Exception e){  
                e.printStackTrace();  
            }        
              
        }  
    

    追加数据

    /** 
     * 追加方式 
     * 写的方法 
     * @param path 文件路径 
     * ***/  
    public static void randomWrite(String path){  
        try{  
            /**以读写的方式建立一个RandomAccessFile对象**/  
            RandomAccessFile raf=new RandomAccessFile(path, "rw");  
              
            //将记录指针移动到文件最后  
            raf.seek(raf.length());  
            raf.write("我是追加的 
    ".getBytes());  
              
        }catch(Exception e){  
            e.printStackTrace();  
        }  
          
    }  
    

    任意位置插入数据

    /** 
         * 实现向指定位置 
         * 插入数据 
         * @param fileName 文件名 
         * @param points 指针位置 
         * @param insertContent 插入内容 
         * **/  
        public static void insert(String fileName,long points,String insertContent){  
            try{  
            File tmp=File.createTempFile("tmp", null);  
            tmp.deleteOnExit();//在JVM退出时删除  
              
            RandomAccessFile raf=new RandomAccessFile(fileName, "rw");  
            //创建一个临时文件夹来保存插入点后的数据  
            FileOutputStream tmpOut=new FileOutputStream(tmp);  
            FileInputStream tmpIn=new FileInputStream(tmp);  
            raf.seek(points);  
            /**将插入点后的内容读入临时文件夹**/  
              
            byte [] buff=new byte[1024];  
            //用于保存临时读取的字节数  
            int hasRead=0;  
            //循环读取插入点后的内容  
            while((hasRead=raf.read(buff))>0){  
                // 将读取的数据写入临时文件中  
                tmpOut.write(buff, 0, hasRead);  
            }  
              
            //插入需要指定添加的数据  
            raf.seek(points);//返回原来的插入处  
            //追加需要追加的内容  
            raf.write(insertContent.getBytes());  
            //最后追加临时文件中的内容  
            while((hasRead=tmpIn.read(buff))>0){  
                raf.write(buff,0,hasRead);  
            }  
            }catch(Exception e){  
                e.printStackTrace();  
            }  
        }  
    

      
      我们可以用RandomAccessFile这个类,来实现一个多线程断点下载的功能,用过下载工具的朋友们都知道,下载前都会建立两个临时文件,一个是与被下载文件大小相同的空文件,另一个是记录文件指针的位置文件,每次暂停的时候,都会保存上一次的指针,然后断点下载的时候,会继续从上一次的地方下载,从而实现断点下载或上传的功能,有兴趣的朋友们可以自己实现下。

     
     
  • 相关阅读:
    A20的板子笔记
    RT-Thread信号量的基本操作
    RT-Thread的线程间同步
    RT-Thread多线程导致的临界区问题
    RT-Thread的CPU使用率计算
    RT-Thread 线程的让出
    车牌识别LPR(八)-- 字符识别
    车牌识别LPR(七)-- 字符特征
    车牌识别LPR(六)-- 字符分割
    车牌识别LPR(五)-- 一种车牌定位法
  • 原文地址:https://www.cnblogs.com/owenma/p/8723283.html
Copyright © 2011-2022 走看看