RandomAccessFile例子和浅析

private void testUpload(String filepath) {
  int pieceSize = 3072; // 每片大小
  int pieceNum = 1; // 切片数量
  int uploadedNum = 0; // 已上传切片数量
  long fileSize;

  String localPath = filepath + "/GooglePinyinInstaller.exe";
  File inFile = new File(localPath);
  fileSize = inFile.length();
  pieceNum = (int) (fileSize - 1) / pieceSize + 1; // 计算切片数量
  
  
  Log.i(TAG, "Pice count:" + pieceNum);
  

  // 开始切片
  try {
   // 打开要分割的文件
   RandomAccessFile inn = new RandomAccessFile(inFile, "r");

   // 合并的文件
   File outFile = new File(filepath + "/GooglePinyinInstaller2.exe");
   RandomAccessFile outt = new RandomAccessFile(outFile, "rw");

   long endSize = 0;
   long beginSize = 0;
   byte[] bytes = new byte[pieceSize];
   for (uploadedNum = 0; uploadedNum < pieceNum; uploadedNum++) {
    // 当前切片结束位置
    if (uploadedNum == pieceNum-1) {
     bytes = new byte[(int) (fileSize-uploadedNum * pieceSize)];
    }
    
    // 写入新文件    
    inn.readFully(bytes);
    outt.write(bytes);


    // 等待一阵子
    //Thread.sleep(100);

    // 下一切片开始位置
    beginSize = endSize+1;

   }
   outt.close();
   inn.close();

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  filepath = null;
  Log.d(TAG, "uploadedNum:" + uploadedNum);

 }

 

 

 

说明

1、  这个类依我看来,用来处理字节流(byte)是很好的,如果用来处理字符(串)或其他数据类型,比如int、long,我试了感觉效果并不好,尤其是处理中文字符串的时候,那简直就是一个灾难,你会又碰上纠缠不清的乱码!

2、 seek(long pos)方法

是在读取的时候用来设置读取到哪一个字节的,比如在例子中有5,10,15,20字节,在byte数组中分别对应0、1、2、3位置,同样在文件file = new RandomAccessFile(f, “rw”);中,也对应着0、1、2、3位置,所以如果设置file.seek(1);就表示通过file.readByte()读取的时候,读取的是第1位置的数据,也就是10了。

3、  getFilePointer()方法

在通过上面说的seek(long pos)设置后,getFilePointer()得到的就是当前文件中的字节位置,也就是所说的偏移量了。比如在这个例子中,getFilePointer()的值就是1.

4、文件模式

“r” 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。

“rw” 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。

“rws” 打开以便读取和写入,对于 “rw”,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。

“rwd”   打开以便读取和写入,对于 “rw”,还要求对文件内容的每个更新都同步写入到底层存储设备。

附录

voidclose()
关闭此随机访问文件流并释放与该流关联的所有系统资源。
FileChannelgetChannel()
返回与此文件关联的唯一 FileChannel 对象。
FileDescriptorgetFD()
返回与此流关联的不透明文件描述符对象。
longgetFilePointer()
返回此文件中的当前偏移量。
longlength()
返回此文件的长度。
intread()
从此文件中读取一个数据字节。
intread(byte[] b)
将最多 b.length 个数据字节从此文件读入 byte 数组。
intread(byte[] b, int off, int len)
将最多 len 个数据字节从此文件读入 byte 数组。
booleanreadBoolean()
从此文件读取一个 boolean。
bytereadByte()
从此文件读取一个有符号的八位值。
charreadChar()
从此文件读取一个字符。
doublereadDouble()
从此文件读取一个 double。
floatreadFloat()
从此文件读取一个 float。
voidreadFully(byte[] b)
将 b.length 个字节从此文件读入 byte 数组,并从当前文件指针开始。
voidreadFully(byte[] b, int off, int len)
将正好 len 个字节从此文件读入 byte 数组,并从当前文件指针开始。
intreadInt()
从此文件读取一个有符号的 32 位整数。
StringreadLine()
从此文件读取文本的下一行。
longreadLong()
从此文件读取一个有符号的 64 位整数。
shortreadShort()
从此文件读取一个有符号的 16 位数。
intreadUnsignedByte()
从此文件读取一个无符号的八位数。
intreadUnsignedShort()
从此文件读取一个无符号的 16 位数。
StringreadUTF()
从此文件读取一个字符串。
voidseek(long pos)
设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
voidsetLength(long newLength)
设置此文件的长度。
intskipBytes(int n)
尝试跳过输入的 n 个字节以丢弃跳过的字节。
voidwrite(byte[] b)
将 b.length 个字节从指定 byte 数组写入到此文件,并从当前文件指针开始。
voidwrite(byte[] b, int off, int len)
将 len 个字节从指定 byte 数组写入到此文件,并从偏移量 off 处开始。
voidwrite(int b)
向此文件写入指定的字节。
voidwriteBoolean(boolean v)
按单字节值将 boolean 写入该文件。
voidwriteByte(int v)
按单字节值将 byte 写入该文件。
voidwriteBytes(String s)
按字节序列将该字符串写入该文件。
voidwriteChar(int v)
按双字节值将 char 写入该文件,先写高字节。
voidwriteChars(String s)
按字符序列将一个字符串写入该文件。
voidwriteDouble(double v)
使用 Double 类中的 doubleToLongBits 方法将双精度参数转换为一个 long,然后按八字节数量将该 long 值写入该文件,先定高字节。
voidwriteFloat(float v)
使用 Float 类中的 floatToIntBits 方法将浮点参数转换为一个 int,然后按四字节数量将该 int 值写入该文件,先写高字节。
voidwriteInt(int v)
按四个字节将 int 写入该文件,先写高字节。
voidwriteLong(long v)
按八个字节将 long 写入该文件,先写高字节。
voidwriteShort(int v)
按两个字节将 short 写入该文件,先写高字节。
voidwriteUTF(String str)
使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值