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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值