Random reading and writing of files in Java
The Java.io 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.
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 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 Java.util.zip 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 "Ep10_13.zip", and extract and display them 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 ("E
P10_13.zip ");
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 ((B=in.read ())!=-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 ("Ep10_13.zip");
Zipinputstream inout=new Zipinputstream (new Bufferedinputstream (d));
ZipEntry Z;
while ((Z=inout.getnextentry ())!=null) {//Get access System.out.println ("Reading file" +z.getname ()); while ((X=inout.read ())!=-1) System.out.write (x);
System.out.println ();
} inout.close ();
}
}
After running, create a ep10_13.zip 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: