Java's random Read and write to files and compression processing operations _java

Source: Internet
Author: User
Tags system log

Random reading and writing of files in Java
The package provides a Randomaccessfile class for random file creation and access. Using this class, you can jump to and write data anywhere in the file. A program can insert data into a random file without destroying other data in that file. In addition, the program can update or delete previously stored data without overwriting the entire file.

The Randomaccessfile class is a direct subclass of the object class, and contains two main constructs for creating Randomaccessfile objects, as shown in the table.

It should be noted that mode represents the operating state of the randomly read and write files that are created, and their values include:
R: Indicates that the file is opened as read-only.
RW: Means to open the file as read and write, using this mode to simultaneously read and write with only one object.

The following table lists the common methods and descriptions for the Randowaccessfile class.

The example simulates the system log and writes the data to the end of the file.

Class ep10_12{public
  static void Main (String args[]) throws ioexception{
    try{BufferedReader in=new
      BufferedReader (New InputStreamReader (;
      String s=in.readline ();
      Randomaccessfile myfile=new randomaccessfile ("Ep10_12.log", "RW"); (Myfile.length ()); Move to end of file
      myfile.writebytes (s+ "\ n");//write Data
      myfile.close ();
    catch (IOException e) {}

After the program is run, a Ep10_12.log file is created in the directory, and the content entered at each run is added at the end of the file.

Compressed processing of files in Java
The package provides classes that can process the compression and decompression of files, which inherit from the Outputsteam and InputStream of the byte stream. where Gzipoutputstream and Zipoutputstream can compress data into both GZIP and ZIP formats, Gzipinpputstream and Zipinputstream can restore compressed data.

The general steps for writing files to a compressed file are as follows:
Builds the compressed class object associated with the compressed file to be generated.
Compressed files usually contain more than one file, each file to be added is called a compression entry, and a compressed entry object is generated using ZipEntry (String FileName).
Use Putnextentry (ZipEntry entry) to add a compressed entry to the compressed file.
Writes the contents of the file to this compressed file.
Use Closeentry () to end the current compression entry and continue to the next compression entry.

The general steps for reading a file from a compressed file are as follows:
Builds the compressed class object associated with the compressed file to be read.
Use Getnextentry () to get the next compression entry.

"Example" enter several file names, compress all files into "", and extract and display them from the compressed file.

Import java.util.*;
Import*; Class ep10_13{public static void Main (String args[]) throws ioexception{FileOutputStream a=new fileoutputstream ("E ");
    Processing compressed Files Zipoutputstream out=new zipoutputstream (New Bufferedoutputstream (a));
      for (int i=0;i<args.length;i++) {//to handle each file entered by the command line System.out.println ("Writing file" +args[i));
      Bufferedinputstream in=new Bufferedinputstream (New FileInputStream (args[i)); Out.putnextentry (New ZipEntry (args[i));
      Sets the ZipEntry object int b; while (( ())!=-1) Out.write (b);
    Read from the source file and write In.close () to the compressed file;
    } out.close ();
    Extract the file and display System.out.println ("Reading file");
    FileInputStream d=new FileInputStream ("");
    Zipinputstream inout=new Zipinputstream (new Bufferedinputstream (d));

    ZipEntry Z; 
    while ((Z=inout.getnextentry ())!=null) {//Get access System.out.println ("Reading file" +z.getname ());  while (( ())!=-1) System.out.write (x);
    System.out.println ();
  } inout.close ();


After running, create a compressed file in the program directory, use the decompression software (such as WinRAR, etc.), you can open it. At the command prompt, the program runs the results as shown in the figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.