欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现文件读写与压缩实例

程序员文章站 2024-02-27 19:56:33
本文通过实例讲述了java对文件读写与压缩的实现方法,具体代码如下: package com.toone.iform.action.common; impo...

本文通过实例讲述了java对文件读写与压缩的实现方法,具体代码如下:

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() {
    // d盘下有个welcome.java文件,现在按字节读入:     
   int a = 0;
    // int counter=0;     
   fileinputstream f11;
    // 输入流     
   try {
      f11 = new fileinputstream("d:" + s + "welcome.java");
      while ((a = f11.read()) != -1)
        system.out.print((char) a); // 这里是按字节输出,中文字符无法正常输出,因为一个中文字符时两个字节。       
       system.out.println("\n\n--------------------------------------------------\n");
 
      filereader f12 = new filereader("d:" + s + "welcome.java");
      while ((a = f12.read()) != -1)
        system.out.print((char) a);// 这里是按字符输出,中文字符都可以正常输出      
       system.out.println("\n\n--------------------------------------------------\n");
 
      f11.close();// 读完之后要关闭文件      
      f12.close();// 读完之后要关闭文件    
      } catch (filenotfoundexception e) {
      // todo auto-generated catch block       
       e.printstacktrace();
    } catch (ioexception e) {
      // todo auto-generated catch block      
     e.printstacktrace();
    }
  }
 
  private static void testoutput() {
    // d盘下有个welcome.java文件,现在按字节读入:    
   int a = 0;
    // 输出流     
   file f21 = new file("d:" + s + "testfile" + s + "test1.txt");
   // 定义一个新的文件f21,然后判断在这一目录下是否存在,如果不存在,则创建之。    
   if (!f21.exists()) {
      f21.getparentfile().mkdirs();
      try {
        f21.createnewfile();
        // 将“welcome.java”的内容复制到f21         
        fileoutputstream fos = new fileoutputstream(f21);
        fileinputstream fis = new fileinputstream("d:" + s + "welcome.java");// 读入“welcome.java”文件         
        while ((a = fis.read()) != -1)
          fos.write(a);// 将读入的内存写到fos中,现在得到的test1.txt就是复制welcome.java的
        // writer类        
        filewriter f22 = new filewriter("d:" + s + "testfile" + s + "test2.txt");
        for (int i = 0; i < 65535; i++)
          f22.write(i);//写入到test2.txt中。由这里也可以看出,上面35-38行判断文件是否存在的语句也可以不要。         
        // 向文件中写入字符串         
        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() {
    // d盘下有个welcome.java文件,现在按字节读入:     
   int a = 0, counter = 0;
    // 缓冲字符,实现高效写入     
   // 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();// 刷新缓冲      
      f31.close();
 
      bufferedreader f32 = new bufferedreader(new filereader("d:" + s
          + "testfile" + s + "test4.txt"));
      string s32;
      system.out.println("输出文件f32的内容:");
      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();
    }
  }
 
  // 将压缩包zipfile解压到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的目录
    while (e.hasmoreelements()) {
      zentry = (zipentry) e.nextelement();
      system.out.println(zentry.getname());// zipfile下有哪些文件?可是为什么不按顺序输出??
      // 将解压后的文件放到file文件夹下:      
      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();
  }
}