Home>

Random read and write of files in java

The java.io package provides the randomaccessfile class for the creation and access of random files.Using this class,You can jump to any position in the file to read and write data.The program can insert data in a random file,Without destroying the other data of the file.In addition, the program can also update or delete previously stored data,Without having to rewrite the entire file.

The randomaccessfile class is a direct subclass of the object class,Contains two main constructors used to create randomaccessfile objects, as shown in the table.

have to be aware of is,mode indicates the operation status of the created random read and write files,Its values ​​include:

r:Open the file in read-only mode.

rw:open the file in read-write mode,With this mode, only one object can be used for reading and writing at the same time.

The following table lists the methods and descriptions commonly used by the randowaccessfile class.

[Example] Mimic system log,Write data to the end of the file.

import java.io. *;
class ep10_12 {
  public static void main (string args []) throws ioexception {
    try {
      bufferedreader in=new bufferedreader (new inputstreamreader (system.in));
      string s=in.readline ();
      randomaccessfile myfile=new randomaccessfile ("ep10_12.log", "rw");
      myfile.seek (myfile.length ());//Move to the end of the file
      myfile.writebytes (s + "\ n");//write data
      myfile.close ();
    }
    catch (ioexception e) {}
  }
}

After the program is run, an ep10_12.log file is created in the directory. Each time you run the program, the content you enter is added to the end of the file.

Compression of files in java

The java.util.zip package provides classes that handle file compression and decompression,They inherit from the bytestream classes outputsteam and inputstream. Among them, gzipoutputstream and zipoutputstream can compress the data into gzip and zip formats, respectively, and gzipinpputstream and zipinputstream can restore the compressed data.

The general steps to write a file to a compressed file are as follows:

Generates a compression class object associated with the compressed file to be generated.

A compressed file usually contains more than one file.Each file to be added is called a compressed entry,Use zipentry (string filename) to generate a compressed entry object.

Use putnextentry (zipentry entry) to add the compressed entry to the compressed file.

Write the contents of the file to this compressed file.

Use closeentry () to end the current compressed entry,Continue to the next compressed entry.

The general steps to read a file from a compressed file are as follows:

Generates a compression class object associated with the compressed file to be read.

Use getnextentry () to get the next compressed entry.

[Example] Enter several file names,Compress all files to "ep10_13.zip", then extract and display from the compressed file.

import java.io. *;
import java.util. *;
import java.util.zip. *;
class ep10_13 {
  public static void main (string args []) throws ioexception {
    fileoutputstream a=new fileoutputstream ("ep10_13.zip");
    //Process compressed files
    zipoutputstream out=new zipoutputstream (new bufferedoutputstream (a));
    for (int i=0;i<args.length;i ++) {//Process each file entered on the command line
      system.out.println ("writing file" + args [i]);
      bufferedinputstream in=new bufferedinputstream (new fileinputstream (args [i]));
      out.putnextentry (new zipentry (args [i]));//Set zipentry object
      int b;
      while ((b=in.read ())! =-1)
        out.write (b);//Read from the source file,Write to compressed file
      in.close ();
    }
    out.close ();
    //Unzip the file and display
    system.out.println ("reading file");
    fileinputstream d=new fileinputstream ("ep10_13.zip");
    zipinputstream inout=new zipinputstream (new bufferedinputstream (d));
    zipentry z;
    while ((z=inout.getnextentry ())!=null) {//Get entry
      system.out.println ("reading file" + z.getname ());//Display the initial file name
      int x;
      while ((x=inout.read ())! =-1)
        system.out.write (x);
      system.out.println ();
    }
    inout.close ();
  }
}

After running, create a compressed file ep10_13.zip in the program directory,Use decompression software (such as winrar, etc.) to open it.At the command prompt,The program running results are shown in the figure:

  • Previous Android call stack trace instance analysis
  • Next C # method for obtaining IIS site and virtual directory information