RandomAccessFile读取文本简介

RandomAccessFile类的经常使用的操做方法
一、public  RandomAccessFile(File file, String mode)throws FileNotFoundException  构造方法  接收File类的对象,指定操做路径,可是在设置时须要设置模式:"r": 只读、"w": 只写、"rw": 读写。
二、public RandomAccessFile(String name, String mode) throws FileNotFoundException 构造方法 再也不使用File类对象表示文件,而是直接输入了一个固定的文件路径。
三、public void close() throws IOException   关闭操做
四、public int read(byte[] b) throws IOException 将内容读取到一个byte数组之中
五、public final byte readByte()  throws IOException 读取一个字节
六、public final int readInt()  throws IOException从文件中读取整型数据。
七、public void seek(long pos) throws IOException 设置读指针的位置。
八、public final void writeBytes(String s) throws IOException 将一个字符串写入到文件之中,按字节的方式处理。
九、public final void writeInt(int v) throws IOException 将一个int型数据写入文件,长度为4位。
十、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"));
		 }
	}

}

读取任意位置的数据java

    /** 
     * 读的方法 
     * @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("我是追加的 \r\n".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这个类,来实现一个多线程断点下载的功能,用过下载工具的朋友们都知道,下载前都会创建两个临时文件,一个是与被下载文件大小相同的空文件,另外一个是记录文件指针的位置文件,每次暂停的时候,都会保存上一次的指针,而后断点下载的时候,会继续从上一次的地方下载,从而实现断点下载或上传的功能,有兴趣的朋友们能够本身实现下。
dom

相关文章
相关标签/搜索