Java implementation of file read and write with compressed instances

  • 2020-04-01 03:25:46
  • OfStack

This paper describes the implementation method of Java to read and write and compress files through an example, the specific code is as follows:


package com.toone.iform.action.common;

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Enumeration; 
import java.util.Random; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipException; 
import java.util.zip.ZipFile;
public class TestFileIO {
  static String s = File.separator;
 
  private static void testInput() {
    //There is a Welcome. Java file under disk d. now read in bytes:
   int a = 0;
    // int counter=0;     
   FileInputStream f11;
    //The input stream
   try {
      f11 = new FileInputStream("D:" + s + "Welcome.java");
      while ((a = f11.read()) != -1)
        System.out.print((char) a); //In this case, the output is in bytes. Chinese characters cannot be output normally, because one Chinese character is two bytes.
       System.out.println("nn--------------------------------------------------n");
 
      FileReader f12 = new FileReader("D:" + s + "Welcome.java");
      while ((a = f12.read()) != -1)
        System.out.print((char) a);//Here is a character output, Chinese characters can be normal output
       System.out.println("nn--------------------------------------------------n");
 
      f11.close();//Close the file after reading it
      f12.close();//Close the file after reading it
      } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block       
       e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block      
     e.printStackTrace();
    }
  }
 
  private static void testOutput() {
    //There is a Welcome. Java file under disk d. now read in bytes:
   int a = 0;
    //The output stream
   File f21 = new File("D:" + s + "testFile" + s + "test1.txt");
   //Define a new file f21, then determine if it exists in this directory, and if it does not, create it.
   if (!f21.exists()) {
      f21.getParentFile().mkdirs();
      try {
        f21.createNewFile();
        //Copy the contents of "welcome.java" to f21
        FileOutputStream fos = new FileOutputStream(f21);
        FileInputStream fis = new FileInputStream("D:" + s + "Welcome.java");//Read in the "welcome.java" file
        while ((a = fis.read()) != -1)
          fos.write(a);//Write the read in memory to fos, and now you get test1.txt is to copy welcome.java
        //Writer class
        FileWriter f22 = new FileWriter("D:" + s + "testFile" + s + "test2.txt");
        for (int i = 0; i < 65535; i++)
          f22.write(i);//Write to test2.txt. It can also be seen from here that the statement that determines whether the file exists or not in line 35-38 above can also be omitted.
        //Writes a string to a file
        FileWriter f23 = new FileWriter("D:" + s + "testFile" + s + "test3.txt");
        f23.write("Hello, world!");
 
        fos.close();
        fis.close();
        f22.close();
        f23.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
      }
    }
  }
 
  private static void testBufferring() {
    //There is a Welcome. Java file under disk d. now read in bytes:
   int a = 0, counter = 0;
    //Buffered characters for efficient writing
   // BufferedWriter f31=new BufferedWriter(newFileWriter("D"+s+"testFile"+s+"test4.txt"));    
   BufferedWriter f31;
    try {
      f31 = new BufferedWriter(new FileWriter("D:" + s + "testFile" + s
          + "test4.txt"));
      for (int i = 1; i <= 100; i++) {
        f31.write(String.valueOf(new Random().nextInt(100)) + " ");
        if (i % 10 == 0)
          f31.newLine();
      }
      f31.flush();//The refresh buffer
      f31.close();
 
      BufferedReader f32 = new BufferedReader(new FileReader("D:" + s
          + "testFile" + s + "test4.txt"));
      String s32;
      System.out.println(" The output file f32 Content: ");
      while ((s32 = f32.readLine()) != null)
        System.out.println(s32);
      f32.close();
      System.out.println("n--------------------------------------------------n");
    } catch (IOException e) {
      // TODO Auto-generated catch block       
     e.printStackTrace();
    }
  }
 
  private static void testZip() {
    try {
      File f1 = new File("D:/test.zip");
      File f2 = new File("D:/testFile/testzip");
      ZipFile zf = new ZipFile(f1);
      testZipToUnzip(zf, f2);
 
    } catch (IOException e) {
      // TODO Auto-generated catch block
       e.printStackTrace();
    }
  }
 
  //Unzip the zipfile into file
  public static void testZipToUnzip(ZipFile zipfile, File file) {
    ZipEntry zentry = null;
    File zipout;
    InputStream zis = null;
    FileOutputStream fos = null;
    Enumeration e = zipfile.entries();//Zipfile directory
    while (e.hasMoreElements()) {
      zentry = (ZipEntry) e.nextElement();
      System.out.println(zentry.getName());//What are the files under zipfile? But why not output in order?
      //Put the unzipped file into the file folder:
      zipout = new File(file + s + zentry.getName());
 
      if (!zentry.isDirectory()) {
        try {
          zis = zipfile.getInputStream(zentry);
          if (!zipout.exists())
            zipout.getParentFile().mkdirs();
          fos = new FileOutputStream(zipout);
          byte[] b = new byte[1024];
          int length;
          while ((length = zis.read(b)) > 0) {
            fos.write(b, 0, length);
          }
          fos.close();
          zis.close();
        } catch (ZipException e1) {
          // TODO Auto-generated catch block           
         e1.printStackTrace();
        } catch (IOException e1) {
          // TODO Auto-generated catch block          
         e1.printStackTrace();
        }
      }
    }
  }
 
  public static void main(String[] args) throws ZipException {
    // TODO Auto-generated method stub
    testInput();
    testOutput();
    testBufferring();
    testZip();
  }
}

Related articles: