RandomAccessFile的用法【转】
                        
                            时间:2021-07-01 10:21:17
                            帮助过:2人阅读
							                        
                     
                    
                    
                    
    public static void randomRed(String path,int pointe){  
        try{  
            
            
            RandomAccessFile raf=new RandomAccessFile(path, "r");  
            
            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();  
        }  
          
          
          
    }  
测试代码
Java代码  
 
- public static void main(String[] args) {  
-     String path="D:\\3\\test.txt";         
-      int seekPointer=20;  
-      randomRed(path,seekPointer);
-     
-     
- }  
运行效果:
Java代码  
 
- RandomAccessFile文件指针的初始位置:0  
- is a teacher  
- hadoop is perfect  
功能two,追加数据,代码如下
Java代码  
 
- public static void randomWrite(String path){  
-     try{  
-         
-         RandomAccessFile raf=new RandomAccessFile(path, "rw");  
-           
-         
-         raf.seek(raf.length());  
-         raf.write("我是追加的 \r\n".getBytes());  
-           
-     }catch(Exception e){  
-         e.printStackTrace();  
-     }  
-       
- }  
测试代码
Java代码  
 
- public static void main(String[] args) {  
-     String path="D:\\3\\test.txt";         
-      
-     
-      randomWrite(path);
-     
- }  
运行效果:

功能three,任意位置插入数据,代码如下
Java代码  
 
-     public static void insert(String fileName,long points,String insertContent){  
-         try{  
-         File tmp=File.createTempFile("tmp", null);  
-         tmp.deleteOnExit();
-           
-         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();  
-         }  
-     }  
测试代码
Java代码  
 
- public static void main(String[] args) {  
-         String path="D:\\3\\test.txt";         
-          
-         
-         
-          insert(path, 33, "\nlucene是一个优秀的全文检索库");  
-     }  
运行效果:

至此,RandomAccessFile类的几个功能,散仙在代码中已给出实现了,现在回到本文开始前的提的那个需求,用RandomAccessFile类就可以轻而易举的完成了,另外需要注意的是,向指定位置插入数据,是散仙自己改造的功能,RandomAccessFile并不直接支持,需要新建一个缓冲区临时空间,存数据,然后在写,因为一旦数据量上了级别,在任意位置插入数据,是很耗内存的,这个也就是为什么hadoop的HDFS文件系统,只支持append的方式,而没有提供修改的操作。
另外我们可以用RandomAccessFile这个类,来实现一个多线程断点下载的功能,用过下载工具的朋友们都知道,下载前都会建立两个临时文件,一个是与被下载文件大小相同的空文件,另一个是记录文件指针的位置文件,每次暂停的时候,都会保存上一次的指针,然后断点下载的时候,会继续从上一次的地方下载,从而实现断点下载或上传的功能,有兴趣的朋友们可以自己实现下。
RandomAccessFile的用法【转】
标签: