I/O Streams的運用

import java.io.*;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Adler32;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipInputStream;

public class IOStreams {
//採用字節流,以行爲單位從一個文件讀取數據
  public static void ByteIn() {
    String byteSource = null;
    try {
      DataInputStream byteIn =
          new DataInputStream(
          new FileInputStream("c://student.dat"));
      while (byteIn.available() != 0) {
        byteSource += byteIn.readLine() + '/n';
      }
      byteIn.close();
    }
    catch (Exception e) {
      System.err.println("File input error");
    }
  }

//採用字符流,以行爲單位從一個文件讀取數據
  public static void CharIn() {
    String charSource = null;
    String s;
    try {
      BufferedReader charIn =
          new BufferedReader(
          new FileReader("c://student.dat"));
      while ( (s = charIn.readLine()) != null)
        charSource += s + "/n";
      charIn.close();
    }
    catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }

//採用字節流,接收鍵盤的輸入
  public static void KeyboardByteIn() {
    try {
      BufferedInputStream byteIn2 =
          new BufferedInputStream(System.in);
      System.out.println("Enter a line:");
      while (byteIn2.available() != -1)
        System.out.print( (char) byteIn2.read());
      System.out.println();
      byteIn2.close();
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

//採用字符流,接收鍵盤的輸入
  public static void KeyboardCharIn() {
    try {
      BufferedReader charIn2 =
          new BufferedReader(
          new InputStreamReader(System.in));
      System.out.println("Enter a line:");
      System.out.println(charIn2.readLine());
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }

  }

//採用字節流,從一個String對象中讀取數據
  public static void StringByteIn() {
    String byteSource = "String";
    try {
      StringBufferInputStream byteIn3 =
          new StringBufferInputStream(byteSource);
      int c;
      while ( (c = byteIn3.read()) != -1)
        System.out.print( (char) c);
      byteIn3.close();
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

  //採用字符流,從一個String對象中讀取數據
  public static void StringCharIn() {
    String charSource = "String";
    try {
      StringReader charIn3 = new StringReader(charSource);
      int c;
      while ( (c = charIn3.read()) != -1)
        System.out.print( (char) c);
      charIn3.close();
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

  //從內存取出格式化輸入
  public static void MemoryIn() {
    String byteSource = "abcde";
    try {
      DataInputStream byteIn4 =
          new DataInputStream(
          new ByteArrayInputStream(byteSource.getBytes()));
      while (byteIn4.available() != 0)
        System.out.print( (char) byteIn4.readByte());
    }
    catch (EOFException e) {
      System.out.println("End of stream");
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

//採用字節流,輸出到文件
  public static void ByteOut() {
    String byteSource = "";
    try {
      DataInputStream byteIn5 =
          new DataInputStream(
          new StringBufferInputStream(byteSource));
      PrintStream byteOut5 = new PrintStream(
          new FileOutputStream("D://IOStreamTestDemoByte.out"));
      while (byteIn5.available() != 0)
        byteOut5.println(byteIn5.readLine());
      byteOut5.close();
      byteIn5.close();
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  //採用字符流,輸出到文件
  public static void CharOut() {
    String charSource = "";
    String s;
    try {
      BufferedReader charIn5 =
          new BufferedReader(
          new StringReader(charSource));
      PrintWriter charOut5 =
          new PrintWriter(
          new BufferedWriter(
          new FileWriter("D://IOStreamTestDemoChar.out")));
      int lineCount = 1;
      while ( (s = charIn5.readLine()) != null)
        charOut5.println(lineCount++ +":" + s);
      charOut5.close();
      charIn5.close();
    }
    catch (EOFException e) {
      System.err.println(e.getMessage());
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }

  }

//通過GZip文件流壓縮文件
  public static void GZip() {
    try {
      //打開需壓縮文件作爲文件輸入流
      BufferedInputStream fin =
          new BufferedInputStream(
          new FileInputStream("D://IOStreamTestDemo.in"));
      //建立gzip壓縮輸出流
      GZIPOutputStream gzout =
          new GZIPOutputStream(
          new FileOutputStream("D://IOStreamTestDemo.gzip"));
      //設定讀入緩衝區尺寸
      byte[] buf = new byte[1024];
      int num;
      fin.read(buf);
      while ( (num = fin.read(buf)) != -1) {
        gzout.write(buf, 0, num);
      }
      //關閉流,必須關閉所有輸入輸出流.保證輸入輸出完整和釋放系統資源
      gzout.close();
      fin.close();
    }
    catch (IOException e) {
      System.out.println(e);
    }
  }

  //通過GZip文件流解壓縮文件
  public static void UnGZip() {
    try {
      //建立gzip解壓工作流
      GZIPInputStream gzin =
          new GZIPInputStream(
          new FileInputStream("D://IOStreamTestDemo.gzip"));
      //建立解壓文件輸出流
      DataOutputStream byteOut8 =
          new DataOutputStream(
          new FileOutputStream("D://IOStreamTestDemoUngzip.out"));
      byte[] buf = new byte[1024];
      int num;
      while ( (num = gzin.read(buf, 0, buf.length)) != -1) {
        byteOut8.write(buf, 0, num);
      }
      gzin.close();
      byteOut8.close();
    }
    catch (IOException e) {
      System.out.println(e);
    }
  }

//運用Zip對多份文件進行壓縮
  public static void Zip() {
    try {
      FileOutputStream f =
          new FileOutputStream("d://data.zip");
      CheckedOutputStream checkOutput =
          new CheckedOutputStream(f, new Adler32());
      ZipOutputStream zipOut =
          new ZipOutputStream(
          new BufferedOutputStream(checkOutput));
      File file = new File("D://data");
      String fileName;
      if (file.isDirectory()) {
        String[] files = file.list();
        for (int i = 0; i < files.length; i++) {
          System.out.println(i + "、Writing file:" + files[i]);
          fileName = file.getAbsolutePath() +
              System.getProperty("file.separator") + files[i];
          BufferedReader reader =
              new BufferedReader(
              new FileReader(fileName));
          zipOut.putNextEntry(new ZipEntry(fileName));
          int oneByte;
          while ( (oneByte = reader.read()) != -1) {
            zipOut.write(oneByte);
          }
          reader.close();
        }
        zipOut.close();
        System.out.println("Checksum:" +
                           checkOutput.getChecksum().getValue());
      }
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

//運用Zip對壓縮文件進行解壓並寫回到原來的目錄
  public static void Unzip() {
    try
    {
      System.out.println("Reading file");
      FileInputStream fileInput =
          new FileInputStream("d://data.zip");
      CheckedInputStream checkIn =
          new CheckedInputStream(fileInput, new Adler32());
      ZipInputStream zipIn =
          new ZipInputStream(
          new BufferedInputStream(checkIn));
      ZipEntry zipEn;
      String fileName;
      while ( (zipEn = zipIn.getNextEntry()) != null)
      {
        fileName = zipEn.toString();
        System.out.println("Reading file:" + zipEn);
        String separator = System.getProperty("file.separator");
        int pos = fileName.lastIndexOf(separator);
        String path = fileName.substring(0, pos);
        File filePath = new File(path);
        if (!filePath.exists())
          filePath.mkdir();
        DataOutputStream writer =
            new DataOutputStream(
            new BufferedOutputStream(
            new FileOutputStream(fileName)));
        int oneByte;
        while ( (oneByte = zipIn.read()) != -1)
          writer.write(oneByte);
        writer.close();
      }
      System.out.println("Checksum:" +
                         checkIn.getChecksum().getValue());
      zipIn.close();
    }
    catch (IOException e)
    {
      System.err.println(e.getMessage());
    }
  }

  public static void main(String[] args) {
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章