Random access file

本文主要探讨了Java中如何使用RandomAccessFile进行随机读写文件,详细解释了其工作原理和常见操作,包括定位、读写数据等。通过示例代码RandomTest.java,读者将深入理解如何在实际项目中应用这一技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RandomTest.java

import java.io.*;                                                                                                                                                                                                    
import java.nio.*;                                                                                                                                                                                                   
import java.nio.file.*;                                                                                                                                                                                              
import java.nio.channels.*;                                                                                                                                                                                          
import static java.nio.file.StandardOpenOption.*;                                                                                                                                                                    
                                                                                                                                                                                                                     
public class RandomTest {                                                                                                                                                                                            
                                                                                                                                                                                                                     
    public static void main(String[] args) {                                                                                                                                                                         
                                                                                                                                                                                                                     
        Path file = Paths.get("file.txt");                                                                                                                                                                           
                                                                                                                                                                                                                     
        String s = "I was here!\n";                                                                                                                                                                                  
                                                                                                                                                                                                                     
        byte[] data = s.getBytes();                                                                                                                                                                                  
                                                                                                                                                                                                                     
        ByteBuffer out = ByteBuffer.wrap(data);                                                                                                                                                                      
                                                                                                                                                                                                                     
        ByteBuffer copy = ByteBuffer.allocate(12);                                                                                                                                                                   
                                                                                                                                                                                                                     
        try (FileChannel fc = (FileChannel.open(file, READ, WRITE))) {                                                                                                                                               
                                                                                                                                                                                                                     
            int nread;                                                                                                                                                                                               
                                                                                                                                                                                                                     
            do {                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                nread = fc.read(copy);                                                                                                                                                                               
                                                                                                                                                                                                                     
            } while (nread != -1 && copy.hasRemaining());                                                                                                                                                            
                                                                                                                                                                                                                     
            fc.position(0);                                                                                                                                                                                          
                                                                                                                                                                                                                     
            while (out.hasRemaining()) {                                                                                                                                                                             
                                                                                                                                                                                                                     
                fc.write(out);                                                                                                                                                                                       
                                                                                                                                                                                                                     
            }                                                                                                                                                                                                        
                                                                                                                                                                                                                     
            out.rewind();                                                                                                                                                                                            
                                                                                                                                                                                                                     
            long length = fc.size();                                                                                                                                                                                 
                                                                                                                                                                                                                     
            fc.position(length-1);                                                                                                                                                                                   
                                                                                                                                                                                                                     
            copy.flip();                                                                                                                                                                                             
                                                                                                                                                                                                                     
            while (copy.hasRemaining()) {                                                                                                                                                                            
                                                                                                                                                                                                                     
                fc.write(copy);                                                                                                                                                                                      
                                                                                                                                                                                                                     
            }     
                                                     
            while (out.hasRemaining()) {                                                                                                                                                                             
                                                                                                                                                                                                                     
                fc.write(out);                                                                                                                                                                                       
                                                                                                                                                                                                                     
            }                                                                                                                                                                                                        
                                                                                                                                                                                                                     
        }catch (IOException x) {                                                                                                                                                                                     
                                                                                                                                                                                                                     
            System.out.println("I/O Exception: " + x);                                                                                                                                                               
                                                                                                                                                                                                                     
        }                                                                                                                                                                                                            
                                                                                                                                                                                                                     
    }                                                                                                                                                                                                                
                                                                                                                                                                                                                     
}                                           

file.txt

I was here!                                                                                                                                                                                                          
j                                                                                                                                                                                                                    
jjjjjjjjj                                                                                                                                                                                                            
kkkkkkk                                                                                                                                                                                                              
kkkkkkj                                                                                                                                                                                                              
jjjjjjjj                                                                                                                                                                                                             
jjjjd                                                                                                                                                                                                                
                                                                                                                                                                                                                     
dddddddd                                                                                                                                                                                                             
ddddd                                                                                                                                                                                                                
aaaaaaa                                                                                                                                                                                                              
dddddd                                                                                                                                                                                                               
dddddd                                                                                                                                                                                                               
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
ddddddd                                                                                                                                                                                                              
sssssssss                                                                                                                                                                                                            
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
                                                                                                                                                                                                                     
kkkkk                                                                                                                                                                                                                
iiii                                                                                                                                                                                                                 
iijjjjjjjjjjI was here!I was here!                                                                                                                                                                                   
I was here!                     
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值