RandomAccessFile应用

来源:互联网 发布:js怎样隐藏鼠标指针 编辑:程序博客网 时间:2024/06/07 19:21

RandomAccessFile往磁盘上读数据和写数据

package cn.lf.day0831;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Scanner;import org.junit.Test;public class IODemo3 {    @Test    public void test() throws IOException{        String dir1 = "./test"; //目录        File dir2 = createFile(dir1, "1"); //创建目录        String fileUrl = "./test/a.txt";        File file = createFile(fileUrl, "2"); //创建文件        //System.out.println("请输入你的内容:");        //String str = new Scanner(System.in).next();        //writeToRandomAccessFile(file, str); //写数据        //读数据        String string = readToRandomAccessFile(file);        System.out.println(string);    }    /**     * @param fileUrl:文件路径     * @param model:1---目录,2---文件     * @return     * @throws IOException     */    //创建文件或者目录    public File createFile(String fileUrl, String model) throws IOException{        File file = new File(fileUrl);        //获取文件绝对路径        System.out.println(file.getAbsolutePath());        //判断文件是否已经存在        if (file.exists()) {            return file;        }        if ("1".equals(model)) {  //创建目录            /*             * public boolean mkdirs()             * 返回:             *  当且仅当已创建目录以及所有必需的父目录时,返回 true;否则返回 false              * */            file.mkdirs();        }else if("2".equals(model)){ //创建文件            /*             * public boolean createNewFile()throws IOException             * 返回:如果指定的文件不存在并成功地创建,则返回 true;如果指定的文件已经存在,             * 则返回 false              * */            file.createNewFile();          }        //判断文件是否存在        if (!file.exists()) {            System.out.println("该目录或文件不存在!");            return null;        }        return file;    }    /**     * @param file:文件     * @param content:内容     * @throws IOException     */    //写文件    public void writeToRandomAccessFile(File file, String content) throws IOException{        if (file == null) {            System.out.println("file为空!");        }        //public class RandomAccessFile         //extends Object        //implements DataOutput, DataInput, Closeable        //此类的实例支持对随机访问文件的读取和写入。        //第一个参数file:指定文件        //第二个参数String:指定读写模式,r:只读模式;rw:读写模式        RandomAccessFile raf = new RandomAccessFile(file, "rw");        //以字节数组的形式写文件        //public byte[] getBytes()            //我的理解:把String转为字符数组            //官方理解:使用平台的默认字符集将此 String 编码为 byte 序列,            //并将结果存储到一个新的 byte数组中。            //eg:String str = null            //byte[] b = str.getBytes();        byte[] b = content.getBytes();        //获取文件的长度        long size = file.length();        //移动指针,使再次写入的内容追加        /*         * public void seek(long pos) throws IOException         *设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。         * */        raf.seek(size);        //public void write(byte[] b) throws IOException        raf.write(b); //写文件        raf.close(); //关闭流    }    /**     * @param file     * @return     * @throws IOException     */    //读数据    public String readToRandomAccessFile(File file) throws IOException{        String str = null;        RandomAccessFile raf  = new RandomAccessFile(file, "rw");        int b = -1;        byte[] bs = new byte[1024];        int length = (int)file.length();          // public int read(byte[] b,int off,int len)throws IOException        // 参数:        // b - 读入数据的缓冲区。        // off - 写入数据的数组 b 中的初始偏移量。        // len - 读取的最多字节数。        // 返回:        // 读入缓冲区的总字节数,如果由于已到达文件的末尾而不再有数据,则返回 -1。         b=raf.read(bs,0,length);          //Arrays.toString(bs);        // public String(byte[] bytes)        //我的理解:将byte数组转成字符串        // 官方解释:通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。        // 参数:        // bytes - 要解码为字符的 byte        str = new String(bs);        return str;    }}
原创粉丝点击