Advertisement
5_2007-2008 Input/ Output #174614

Append data to a file

In Java 1.1 you can just pass true as the second argument to this FileOutputStream constructor to indicate that you want to append data to the file: public FileOutputStream(String name, boolean append) throws IOException In Java 1.0, however, you must use the java.io.RandomAccessFile class that lets you read and write bytes from arbitrary locations in a file. This class implements DataInput and DataOutput so you have all the methods of DataInputStream and DataOutputStream available to you. To create a new random access file pass the name of the file and the mode to the constructor. The mode is either "r" (read-only) or "rw" (read and write). The length() method returns a long that tells you how many bytes there are in a file and the seek(long p) method lets you position the file pointer at a particular point in the file. Thus to start writing at the end of a RandomAccessFile raf, you first raf.seek(raf.length()). The following example demonstrates by appending the string "Kilroy was here!" to every file specified on the command line. (Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)

AI

AI 摘要: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

源代码
original-source
import java.io.*;
class AppendToAFile {
 public static void main (String args[]) {
  for (int i = 0; i < args.length; i++) {
   //First open the file you want to append to
  
   try {
    RandomAccessFile raf = new RandomAccessFile(args[i], "rw");
    // Position yourself at the end of the file
    raf.seek(raf.length());
    // Write the String into the file. Note that you must
    // explicitly handle line breaks.
    raf.writeBytes("\nKilroy was here!\n");
   }
   catch (IOException e) {
    System.out.println("Error opening file: " + e);
   }
   
  }
 } 
}
原始评论 (3)
从 Wayback Machine 恢复