I/O Streams的运用

原创 2006年06月06日 15:29:00

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) {
  }
}

相关文章推荐

《unix高级环境编程》高级 I/O——STREAMS(流)

STREAMS(流)是系统 V 提供的构造内核设备驱动程序和

【I/O】Character Streams

本文目录: Character Streams Reader read() read(char[] buf, int offset, int count) ...

【I/O】Byte Streams

本文目录: Byte Streams InputStream read() read(byte[] buf, int offset, int count) ...

overlapped I/O 运用详解

重叠IO overlapped I/O 运用详解 2009年02月21日 星期六 下午 07:54 I/O设备处理必然让主程序停下来干等I/O的完成, 对这个问题有 方法一...

简单的多路复用编程(I/O)----select()和poll函数的运用

多路复用一般用于I/O操作可能会被阻塞的情况,对可能会有阻塞的I/O的管道、网路进行编程。下面我们来看个例子来说明如何使用两个多路复用函数。         本实例中主要实现通过调用select()...

重叠IO overlapped I/O 运用详解

I/O设备处理必然让主程序停下来干等I/O的完成, 对这个问题有 方法一:使用另一个线程进行I/O。这个方案可行,但是麻烦。                即 CreateThread(…………)...

带缓存的文件I/O

  • 2017年07月07日 17:02
  • 15KB
  • 下载

深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP

前言 本章我们要讲解的是S.O.L.I.D五大原则JavaScript语言实现的第2篇,开闭原则OCP(The Open/Closed Principle )。 开闭原则的描述是: Soft...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:I/O Streams的运用
举报原因:
原因补充:

(最多只允许输入30个字)