码迷,mamicode.com
首页 > 数据库 > 详细

文件操作——RandomAccessFile

时间:2015-05-12 18:27:12      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 文件操作——RandomAccessFile

 构建RandomAccessFile
Java提供了一个可以对文件随机访问的操作,访问包括读和写操作。该类名为RandomAccessFile。该类的读写是基于指针的操作。
1. 只读模式

RandomAccessFile在对文件进行随机访问操作时有两个模式,分别为只读模式(只读取文件数据),和读写模式(对文件数据进行读写)。
只读模式:
在创建RandomAccessFile时,其提供的构造方法要求我们传入访问模式:
RandomAccessFile(File file,String mode)

RandomAccessFile(String filename,String mode)
其中构造方法的第一个参数是需要访问的文件,而第二个参数则是访问模式:
r”:字符串”r”表示对该文件的访问是只读的。
2. 读写模式

创建一个基于文件访问的读写模式的RandomAccessFile我们只需要在第二个参数中传入”rw”即可
RandomAccessFile raf = new RandomAccessFile(file,”rw”);
那么这时在使用RandomAccessFile对该文件的访问就是又可读又可写的。

字节数据读写操作

1. read()方法

RandomAccessFile提供了一个可以从文件中读取字节的方法:
int read()
该方法会从RandomAccessFile当前指针位置读取一个byte(8位) 填充到int的低八位, 高24位为0, 返回值范围正数: 0~255, 如果返回-1表示读取到了文件末尾EOF(EOF:End Of File)! 每次读取后自动移动文件指针, 准备下次读取。

 

2. read(byte[] d)方法

RandomAccessFile提供了一个可以从文件中批量读取字节的方法:
int read(byte[] b)
该方法会从文件中尝试最多读取给定数组的总长度的字节量,并从给定的字节数组第一个位置开始,将读取到的字节顺序存放至数组中,返回值为实际读取到的字节量 。
3. write(int d)方法

RandomAccessFile提供了一个可以向文件中写出字节的方法:
void write(int d)
该方法会根据当前指针所在位置处写入一个字节,是将参数int的”低8位”写出。
4. write(byte[] d)方法

RandomAccessFile提供了一个可以向文件中写出一组字节的方法:
void write(byte[] d)
该方法会根据当前指针所在位置处连续写出给定数组中的所有字节,与该方法相似的还有一个常用方法:
void write(byte[] d,int offset,int len)

该方法会根据当前指针所在位置处连续写出给定数组中的部分字节,这个部分是从数组的offset处开始,连续len个字节。

offset + len < 数组的长度

5. close方法

RandomAccessFile在对文件访问的操作全部结束后,要调用close()方法来释放与其关联的所有系统资源。
void close()
例如:
RandomAccessFile raf = new RandomAccessFile(file,”rw”);
…..//读写操作
raf.close();//访问完毕后要关闭以释放系统资源

/**
 * 创建RandomAccessFile
 * 两种模式:只读、读写
 * @author Administrator
 *
 */
public class TestRandomAccess {
    public static void main(String[] args) throws FileNotFoundException {
        /*
         * 读取1.txt 文件
         */
        RandomAccessFile raf = new RandomAccessFile("1.txt","r");
        
//        File fi =new File("1.txt");
//        raf = new RandomAccessFile("fi","r");
        
        /*
         * 创建读写
         * FileNotFounfException
         * 读写异常会在创建RandomAccessFile时候跑出
         * 两种情况:
         * 1.只读时,若文件不存在,则抛出
         * 2.读写时,若文件不存在  ,则自动创建该文件 ,若创建不成功则抛出
         * 
         */
        RandomAccessFile raf2 =new RandomAccessFile("1.bat","rw");
    }
}
/**
 * 向文件中写入数据
 * @author Administrator
 *
 */
class TestRandomAccessFile2{
        public static void main(String[] args) throws IOException {
            //创建基于raf.bat 文件读写的RandomAccessFile
            RandomAccessFile raf =new RandomAccessFile("2.dat","rw");
            /*
             * void write(int d)
             * 向文件中写入给定的int值的“低8位”
             */
            
            int d=1;
            raf.write(d);
            //操作完毕后要关闭释放资源
        
            raf.close();

        }
    
}

 

class TestRandomAccessFile3{
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf =new RandomAccessFile("raf.bat","r");
        /*
         * int read()
         * 从文件中读取1个字节,并以int形式返回
         * 这个int值只有底8位有效0-255之间
         * 若返回的int值为-1 则说明EOF(end of File)达到文件末尾了
         */
        int a = raf.read();
        System.out.println(a);
    
        int c = raf.read();
        System.out.println(c);
        raf.close();

    }
}
public class TestFile2 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("raf2.txt", "rw");
        //1
        String str ="大家好!才是真的好!";
        //将字符串按照系统默认的编码集转换为对应的字节
        byte[] arr =str.getBytes("gbk");
        System.out.println(arr.length);
        
        raf.write(arr);
        raf.close();
    }
}
/**
 * 将控制台输入的内容写入文件
 * 1.首先Scanner获取用户在控制台输入的内容
 * 2.在使用RandomAccessFile 将数据写入文件
 * @author Administrator
 */
class TestTxt{
    public static void main(String[] args) throws IOException {
       /*
        * 实施步骤    
        */
        Scanner scanner =new Scanner(System.in);
        String line = scanner.nextLine();
        RandomAccessFile raf = new RandomAccessFile("xxx.txt", "rw");
        byte[] arr= line.getBytes();
        raf.write(arr);
        raf.close();
        scanner.close();
    }
}

/**
 * 使用RandomAccessFile 实现文件复制
 * @author Administrator
 *
 */
class TestRandomAccessFileDemo2{
    public static void main(String[] args) throws IOException {
        /*
         * 复制文件: 先从原文件读取字节,将这些字节写入另一个文件中
         * 1. 创建一个RandomAccessFile用于读取源文件
         * 2. 创建一个RandomAccessFile用于写复制后的文件
         * 3. 循环读取源文件中的所有字节
         * 4. 将每一个字节写入复制后的文件中
         * 5. 将两个RandomAccessFile关闭
         */
        RandomAccessFile raf1= new RandomAccessFile("fu1.txt", "r");
        RandomAccessFile raf2= new RandomAccessFile("fu2.txt", "rw");
        
        long start = System.currentTimeMillis();
        int d=-1;
        while((d=raf1.read())!=-1){
            raf2.write(d);
        }
        long end = System.currentTimeMillis();
        
        System.out.println("复制完毕:"+(end-start)+"ms");
        raf1.close();
        raf2.close();
        
        
    }
}
/**
 * 使用批量读写来降低读写次数,提高读写效率
 * 复制文件
 * @author Administrator
 *
 */
class TestRandomAccessFileDemo6{
    public static void main(String[] args) throws IOException {
        RandomAccessFile src = new RandomAccessFile("fu1.txt", "r");
        RandomAccessFile des = new RandomAccessFile("fu3.txt", "wr");
        long start = System.currentTimeMillis();
        //10K
        byte[] buf = new byte[1024*10];
        int len =-1;  //每次读取到的实际字节量
        while((len=src.read(buf))!=-1){
            des.write(buf,0,len);
        }
        long end = System.currentTimeMillis();
        src.close();
        des.close();
        System.out.println("复制时间:"+ (end-start)+"ms");
        
    }
}

 

文件操作——RandomAccessFile

标签:

原文地址:http://www.cnblogs.com/manue1/p/4497997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!