Random read and write 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 location in the file to read and write data. The program can insert data in a random file without destroying other data in that file. In addition, the program can also update or delete previously stored data without rewriting the entire file.
The RandomAccessFile class is a direct subclass of the Object class and contains two main constructors used to create objects of RandomAccessFile, as shown in the table.
It should be noted that mode represents the operation status of the random read and write file created, and its values include:
r: means to open the file in read-only mode.
rw: means to open a file in a read-write manner. Use this mode to implement read-write operations at the same time using only one object.
The following table lists commonly used methods and descriptions of the RandowAccessFile class.
[Example] Imitate the system log and 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 InputSt reamReader(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, a file of ep10_12.log is created in the directory. The content entered at each run will be added at the end of the file content.
Compression processing of files in Java
The Java.util.zip package provides classes that can process compression and decompression of files, which are inherited from the byte stream classes OutputSteam and InputStream. Among them, GZIPOutputStream and ZipOutputStream can compress data into GZIP and Zip formats respectively, and GZIPInpputStream and ZipInputStream can restore the compressed data.
The general steps for writing files to compressed files are as follows:
Generates a 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. ZipEntry (String FileName) is used to generate a compressed entry object.
Use putNextEntry(ZipEntry entry) to add the compressed entry to the compressed file.
Write the file contents to this compressed file.
Use closeEntry() to end the current compression entry and continue with the next compression entry.
The general steps for reading a file from a compressed file are as follows:
Generates a compressed 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 into "ep10_13.zip", and then decompress 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{ Fi leOutputStream a=new FileOutputStream("ep10_13.zip" ); //Processing compressed file ZipOutputStream out=new ZipOutputStream(new BufferedOutputStream(a)); for(int i=0;i<args.length;i++){ // Processing each file entered in the command line System. out.println("Writing file"+args[i]); BufferedInputStream in=new BufferedInputStream(new FileInputStream(args[i])); out.putNextEntry(new ZipEntr y(args[i])); //Set ZipEntry object int b; while((b=in.read())!=-1) out.write(b); //Read from the source file and write in.close() into the compressed file; } out.close (); //Decompress 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 the entry System.out.println("Reading file"+z.getName()); //Show 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 of ep10_13.zip in the program directory, and use decompression software (such as WinRAR, etc.) to open it. At the command prompt, the program run results are shown in the figure: