我想对一个A文件进行操作,如A文件大小为2M。
我想从第2k的位置开始写1K的内容。
FileInputStream fileIn = new FileInputStream("test.tmp");
byte[] readData = new byte[1*1024];
fileIn.read(readData);
File reTempFile = null;
reTempFile = new File(path+"test.temp");

if(reTempFile.exists()){
  FileOutputStream fileOut = new FileOutputStream(reTempFile);
  fileOut.write(readData,0,readData.length);
  fileOut.flush();
  fileOut.close();
  System.out.println(readData.length);
}
我这样写的话,是可以写进去,但是test.tmp也从原来的1M大小变成1K了

解决方案 »

  1.   

    如果纯粹追加文件, 使用 new FileOutputStream(reTempFile, true);对效率要求不高, 可用RandomAccessFile
      

  2.   

    用RandomAccessFile,这个效率应该更高啊!要完成插入数据,必须自己处理数据移动的问题!!
      

  3.   

    int m = 1;
            int length = 10*m;
            File file = new File("1.txt");
            //file.length();
            MappedByteBuffer mb = new RandomAccessFile(file,"rw").getChannel().map(FileChannel.MapMode.READ_WRITE, file.length()/2,length);
            for(int i=0; i<length; i++){
                mb.put((byte)'x');            
            }
      

  4.   

    怎么把RandomAccessFile忘记了呢.晕