RandomAcessFile的使用

  今天在网上搜了下java.io.RandomAcessFile的使用方法,收益良多。下面将搜到的几个例子放进来,希望与大家分享,顺便加深一下记忆。

1.java.io.RandomAcessFile的简单使用,见代码

import java.io.*;

public class IOStreamDemo {
 public static void main(String[] args) {
  try {
   RandomAccessFile rf1 = new RandomAccessFile("d:\\jeru.txt", "rw");
   for (int i = 0; i < 10; i++) {
    rf1.writeBytes("xixi,this is line " + i + "\r\n");
   }
   //rf1.close();
   int i = 0;
   String record = new String();
   RandomAccessFile rf2 = new RandomAccessFile("d:\\jeru.txt", "rw");
   rf2.seek(rf2.length());
   rf2.writeBytes("Hello, Mr. Shao:lala,append line" + "\r\n");
   rf2.close();
   RandomAccessFile rf3 = new RandomAccessFile("d:\\jeru.txt", "r");
   while ((record = rf3.readLine()) != null) {
    i++;
    System.out.println("Value " + i + ":" + record);
   }
   rf3.close();
  } catch (Exception e) {
  }
 }
}
class TestGO {  
  
    /** 
     * @param args 
     * @throws IOException 
     */ 
    public static void main(String[] args) throws IOException {  
        File f = new File("C:/1.test");  
        if (!f.exists()) {  
            f.createNewFile();  
        }  
        RandomAccessFile rf1 = new RandomAccessFile(f, "rw");  
        RandomAccessFile rf2 = new RandomAccessFile(f, "rw");  
        rf1.setLength(1000 * 1024);  
 
        rf1.seek(0);  
        rf2.seek(100);  
 
        rf1.writeBoolean(false);  
        rf2.writeBoolean(true);  
 
        rf1.seek(0);  
        rf2.seek(100);  
 
        System.out.println(rf1.readBoolean());  
        System.out.println(rf2.readBoolean());  
    }  
}

 

2.java读写隐藏文件:

如果使用:
FileOutputStream out = new FileOutputStream(file);
就会报错:拒绝访问
java.io.FileNotFoundException: **.txt (拒绝访问。)  
at java.io.FileOutputStream.open(Native Method)

即使我们设置写权限也一样。可以看到java调用本地方法open,

所以就不能调用open方法,而改调用openAppend方法,就是追加:
所以我们可以使用new FileOutputStream(file, true)  来实现写入,但是只能追加写入,而不能进行修改操作。
   

Java代码   
  1. publicvoid appendDatatoHiddenFile(String file) {   
  2.        try {   
  3.            FileOutputStream out = new FileOutputStream(file, true);   
  4.            PrintStream p = new PrintStream(out);   
  5.            for (int i = 0; i < 10; i++)   
  6.               p.println("hello: " + i );   
  7.        } catch (FileNotFoundException e) {   
  8.            e.printStackTrace();   
  9.        }   
  10.     }   
  11.    
publicvoid appendDatatoHiddenFile(String file) {
       try {
           FileOutputStream out = new FileOutputStream(file, true);
           PrintStream p = new PrintStream(out);
           for (int i = 0; i < 10; i++)
              p.println("hello: " + i );
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
    }
 


那么如何任意操作“隐藏文件”呢?使用使用RandomAccessFile
简单介绍一下RandomAccessFile:

输入流FileInputStream和输出流 FileOutputStream,实现的是对磁盘文件的顺序读写,而且读写要分别创建不同对象。相比之下RandomAccessFile类则可对文件实现随机读写操作。

如某个文件有30个字节,读取数据过程中,从20-30读取,用skip( )//跳过方法。
RandomAccessFile类,即可以充当输入也可充当输出流。可以看作节点流。 RandomAccessFile对象的文件位置指针遵循下面的规律;新建RandomAccessFile对象的文件位置指针位于文件的开头处;每次读写操作之后,文件位置的指针都相应后移到读写的字节数;可以通过getFilePointer方法来获得文件位置指针的位置,通过seek方法来设置文件指针的位置。


Api代码   
  1. RandomAccessFile常用方法:    
  2. 1.         RandomAccessFile (”路径+文件名”, String“rw”/”r”)两个参数   
  3. 2.         Void close( )    
  4. 3.         Long length( )    
  5. 4.         Void seek( )    
  6. 5.         Long getFilePointer( )获得当前指针位置,默认为0  
  7. 6.         Int read( )从文件当前位置读取一个字节   
  8. 7.         int read (byte[]b)    
  9. 8.         int read (byte[]b,int off,int len)    
  10. 9.         Final boolean readBoolean( )从文件当前位置读取boolean类型的一个字节  boolean在内存占1/8  
  11. 10.     Final_ char readChar( )从文件中读取2个字节。   
  12. 11.     Final int readInt( )从文件中读取4个字节。   
  13. 12.     ##Final String readLine( )从文件中读取一行后转为String。   
  14. 13.     Void write(byte[]b)将字节数组B中的数据写到文件中。   
  15. 14.     Void write(byte[]b,int off,int len)将 len 个字节从指定字节数组写入到此文件,并从偏移量 off 处开始。   
  16. 15.     Void write(int b)将指定的数据写到文件中。   
  17. 16.     Final void writeBoolean(boolean v)将boolean类型的值按单字节的形式写到文件中01  
  18. 17.     Final void writeChar(int v)将char值按2个字节写入到文件中   
  19. 18.     Final void writeChars(String s)将字符串按字符方式写入到文件中   
  20. 19.     Final void writeInt(int v)按四个字节将 int 写入该文件,先写高字节   
  21. 20.     skipBytes(long i):从前往后拨弄指示器的位置,就是跳过多少个字节读取数据。   
  22. 21.     Void seek(long p): 对指示器作决定性的定位,用于从后往前拨弄指示器的位置。对于seek方法,拥有skipBytes( )的功能,但seek( )在使用过程非常影响系统的开销。只有万不得已的情况下使用。  
RandomAccessFile常用方法: 
1.         RandomAccessFile (”路径+文件名”, String“rw”/”r”)两个参数
2.         Void close( ) 
3.         Long length( ) 
4.         Void seek( ) 
5.         Long getFilePointer( )获得当前指针位置,默认为0
6.         Int read( )从文件当前位置读取一个字节
7.         int read (byte[]b) 
8.         int read (byte[]b,int off,int len) 
9.         Final boolean readBoolean( )从文件当前位置读取boolean类型的一个字节  boolean在内存占1/8
10.     Final_ char readChar( )从文件中读取2个字节。
11.     Final int readInt( )从文件中读取4个字节。
12.     ##Final String readLine( )从文件中读取一行后转为String。
13.     Void write(byte[]b)将字节数组B中的数据写到文件中。
14.     Void write(byte[]b,int off,int len)将 len 个字节从指定字节数组写入到此文件,并从偏移量 off 处开始。
15.     Void write(int b)将指定的数据写到文件中。
16.     Final void writeBoolean(boolean v)将boolean类型的值按单字节的形式写到文件中0或1
17.     Final void writeChar(int v)将char值按2个字节写入到文件中
18.     Final void writeChars(String s)将字符串按字符方式写入到文件中
19.     Final void writeInt(int v)按四个字节将 int 写入该文件,先写高字节
20.     skipBytes(long i):从前往后拨弄指示器的位置,就是跳过多少个字节读取数据。
21.     Void seek(long p): 对指示器作决定性的定位,用于从后往前拨弄指示器的位置。对于seek方法,拥有skipBytes( )的功能,但seek( )在使用过程非常影响系统的开销。只有万不得已的情况下使用。



来个列子:
  

Java代码   
  1. publicvoid randomAccessFile(String file) throws Exception {   
  2.       RandomAccessFile f = new RandomAccessFile(file, "rw");   
  3.       System.out.println("File.lelngth:" + (f.length()) + "B");   
  4.       System.out.println("File PointPosition:" + f.getFilePointer());   
  5.       f.seek(f.length());   
  6.       f.writeBoolean(true);   
  7.       f.writeBoolean(false);   
  8.       f.writeChar('a');   
  9.       f.writeChars("hello!");   
  10.       System.out.println("File Length;" + (f.length()) + "B");   
  11.   
  12.       f.seek(0);   
  13.       System.out.println(f.readBoolean());   
  14.       System.out.println(f.readBoolean());   
  15.          
  16.       System.out.println(f.readLine());   
  17.       f.close();   
  18.    }   
 publicvoid randomAccessFile(String file) throws Exception {
       RandomAccessFile f = new RandomAccessFile(file, "rw");
       System.out.println("File.lelngth:" + (f.length()) + "B");
       System.out.println("File PointPosition:" + f.getFilePointer());
       f.seek(f.length());
       f.writeBoolean(true);
       f.writeBoolean(false);
       f.writeChar('a');
       f.writeChars("hello!");
       System.out.println("File Length;" + (f.length()) + "B");
 
       f.seek(0);
       System.out.println(f.readBoolean());
       System.out.println(f.readBoolean());
       
       System.out.println(f.readLine());
       f.close();
    }
 

在来个处理中文的例子:
   

Java代码   
  1. publicvoid chinesewrit(String toAppend) {   
  2.     
  3.        try {   
  4.            // 写入   
  5.            int i = 0;   
  6.            String record = new String();   
  7.            String toCn = null;   
  8.            // 处理中文问题   
  9.            toCn = new String(toAppend.getBytes("GBK"), "ISO8859_1");   
  10.     
  11.            RandomAccessFile rf = new RandomAccessFile("c:\\aaa.txt""rw");   
  12.            rf.seek(rf.length());   
  13.            rf.writeBytes(toCn + "\n");   
  14.            rf.close();   
  15.            // 读取   
  16.            RandomAccessFile rf2 = new RandomAccessFile("c:/aaa.txt""r");   
  17.            String outCn = null;   
  18.            while ((record = rf2.readLine()) != null) {   
  19.               i++;   
  20.               // 处理中文问题   
  21.               outCn = new String(record.getBytes("ISO8859_1"), "GBK");   
  22.     
  23.               System.out.println("Line " + i + ":" + outCn);   
  24.            }   
  25.            rf2.close();   
  26.        } catch (Exception e) {   
  27.            e.printStackTrace();   
  28.        }   
  29.     }   

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值