`

如何在文件末尾写入新数据,适用JavaNIO

    博客分类:
  • java
 
阅读更多

转:http://stoneli88.iteye.com/blog/891288

在对文件进行写入操作时,我们经常会碰到的一个棘手的问题可能是:如何在一个已经有内容的文件末尾写入新数据而不覆盖掉原来的内容?现在本人介绍以下四种方法:

首先,假设在程序的当前目录中有一个文件“data.txt”,该文件中已经有内容,现在要进行的操作是在data.txt文件末尾定放字符串"Write in the end".

法一:

在FileOutputStream 或者 FileWriter 的构造器中加个参数 “true”,就如:

Java代码  收藏代码
  1. FileOutputStream fos =  new  FileOutputStream( "data.txt" , true );   
  2. //加个参数true表示在文件末尾写入,不加或者加上false表示在文件开头处定入   
  3. fos.write("Write in the end" .getBytes()); //用write(byte[] b)写入字符串   
  4. fos.close();//记得关闭文件流   


或者可以用FileWriter

Java代码  收藏代码
  1. FileWriter fos =  new  FileWriter( "data.txt" , true );  //同样加个参数true   
  2. fos.write("Write in the end" ); //该类有不同于FileOutputStream的write(String s)   
  3. fos.close();  



法二:

利用java.nio.channel包里面的FileChannel类,该类有个position(long newPosition),参数newPosition表示“计算从文件开始的字节数 ”该方法的功能就是把指针位置设置在文件的newPosition处,而文件的末尾我们可以用FileChannel.size()来得到!也就是我们用 position(FileChannel.size())
就可以把指针指向末尾,从而写入新数据,就如:

Java代码  收藏代码
  1.  FileChannel fc =  new  RandomAccessFile( "data.txt" "rw" ).getChannel();  // rw模式   
  2. //必须用RandomAccessFile("data.txt", "rw").getChannel();   
  3. //而不能用FileOutputStream.getChannel   
  4. fc.position(fc.size()); // //把指针移到文件末尾   
  5. fc.write(ByteBuffer.wrap("Write in the end " .getBytes()));  //写入新数据   
  6. fc.close()  
  7. //如果我们硬是要用FileOutputStream.getChannel,可以写成:   
  8. FileChannel fc = new  FileOutputStream( "data.txt" true ).getChannel();  //参数true也必须加上   
  9. fc.position(fc.size());  
  10. fc.write(ByteBuffer.wrap("Write in the end " .getBytes()));  
  11. fc.close();  



法三:

用RandomAccess类的seek(long pos)代替方法二的position(long newPosition),不多讲,看例题:

Java代码  收藏代码
  1. RandomAccessFile rf =  new  RandomAccessFile( "data.txt" "rw" );  
  2. rf.seek(rf.length());    //length()方法,而不是上面的size()   
  3. rf.writeChars("Write in the end" );  //wiriteChars写入字符串,写入的字符串中的每个字符在文件中都是占两个字节,比如write在文件中看到的是 w r i t e 。   


法四:
 
   利用FileChannel的 map(FileChannel.MapMode mode,long position,long size)方法创建一个MappedByteBuffer(内存映射文件),但是可“读/写”的MappedByteBuffer必须通过 RandomAccessFile.getChannel.map()来创建,因为FileChannel.MapMode(文件映射模式的类型)只有三 种:
1,MapMode.READ_ONLY      只读
2,MapMode.READ_WRITE     读/写
3,MapMode.PRIVATE        专用
而我们要在文件末尾写入的话就必须有到“写文件”,也就是要用到MapMode.READ_WRITE ,如果我们用FileInputStream或者FileOutputStream来获取通道的话都是“只读”或者“只写”,就没办法与 MapMode.READ_WRITE 搭配起来,所以我们只能通过RandomAccessFile的"rw"模式来与MapMode.READ_WRITE搭配。
map方法中的参数:
1.mode :根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 2.FileChannel.MapMode 类        中所定义的 READ_ONLY、READ_WRITE 或 PRIVATE 之一
3.position : 文件中的位置,映射区域从此位置开始;必须为非负数
4.size: 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE
有了position与size这两个参数,我们就可以把position设置为原文件的末尾,然后再写入size字节数
现在让我们一起来看下如何在文件末尾写入:

Java代码  收藏代码
  1. FileChannel fc =    new  RandomAccessFile( "data.txt" , "rw" ).getChannel();  
  2. long  length = fc.size();  //有来设置映射区域的开始位置   
  3. MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE,length,20 );  
  4. //由于要写入的字符串"Write in the end"占16字节,所以长度设置为20就足够了   
  5. mbb.put("Write in the end" .getBytes());  //写入新数据   

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics