真才实料才是最真

Dream have no limit

统计

积分与排名

good link

最新评论

IO,Zip package

package logic;

import java.io.*;
import java.util.zip.*;

public class javaIOTest {

 static final int BUFFER = 2048;

 public static void main(String [] args) throws IOException
 {
     //格式化输入输出
  /*
   * 为了加强IO操作的性能,这里使用BufferedOutputStream/BufferedInputStream来进行缓冲,
   * 其中必须有close()方法(当然,调用flush()方法也行),否则缓冲区内容不能刷新清空
   */
  System.out.println("------------------------DataOutputStream,DataInputStream,FileOutputStream,FileInputStream----------------------------------");
     DataOutputStream d = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("a.txt")));
     d.writeDouble(3.414);
     d.writeBoolean(false);
     d.writeByte('1');
     d.writeFloat((float) 2.22323);
     d.writeUTF("That was PI");
     d.close();       
     DataInputStream d2 = new DataInputStream (new BufferedInputStream(new FileInputStream("a.txt")));
     System.out.println(d2.readDouble());
     System.out.println(d2.readBoolean());
     System.out.println(d2.readByte());
     System.out.println(d2.readFloat());
     System.out.println(d2.readUTF());
     /*
      * PrintStream是字节流,它有处理raw byte的方法
      * PrintWriter是字符流,它没有处理raw byte的方法
      */
     System.out.println("------------------------printStream----------------------------------");
     PrintStream printStream = new PrintStream(new FileOutputStream(new File("pi.txt")));
     printStream.print("PI = ");       
     printStream.println(Math.PI);        
     printStream.close();
     System.out.println("------------------------PrintWriter----------------------------------");
     PrintWriter printWriter=null;
       try{
        //初始化对象
        printWriter=new PrintWriter(
            new FileOutputStream("m.txt"));
        printWriter.println("字符类型");//添加内容
        printWriter.println(1.22);
        printWriter.print("PI = ");
        printWriter.println(Math.PI);
        printWriter.flush();//刷新内存
       }catch(Exception e){
        e.printStackTrace();//用trycatch捕捉异常如有则输出产生异常错误所作
       }
       if(printWriter!=null)
        try{
         printWriter.close();
         }catch(Exception e){}//判断对象不为空关闭
   
    /*
     * InputStreamReader 是字节流通向字符流的桥梁
     */
     System.out.println("------------------------InputStreamReader----------------------------------");
     System.out.println("Pls input :");
     InputStreamReader isr = new InputStreamReader(System.in);
     BufferedReader br = new BufferedReader(isr);
    try
    {
        String inputLine = br.readLine();
        System.out.println(inputLine);
    }
    catch(IOException ioe)
    {
     System.out.println("Error: "+ioe);
    }
     System.out.println("------------------------InputStreamReader,OutputStreamWriter----------------------------------");
     System.out.println("按回车后再按Ctrl+z结束输入,可输入中文");
    try
    {
     InputStreamReader isr1 = new InputStreamReader (new FileInputStream (FileDescriptor.in));
     OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("output.log"));
     int is;
     while ((is=isr1.read()) != -1)osw.write(is);
     isr1.close();
     osw.close();
    }
    catch(FileNotFoundException e)
    {
     System.out.println(e);
    }
    catch(IOException e)
    {
     System.out.println(e);
    }
    catch(Exception e)
    {
     System.out.println(e);
    }
    System.out.println("------------------------StreamTokenizer----------------------------------");
    try {
    
        FileReader fr = new FileReader("simple.txt");
        StreamTokenizer st = new StreamTokenizer(fr);
        st.ordinaryChar('/');
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
          if (st.ttype == StreamTokenizer.TT_NUMBER) {
            System.out.println("數字:" + Double.toString(st.nval));
          }
          else if (st.ttype == StreamTokenizer.TT_WORD) {
            System.out.println("字串:" + st.sval);
          }
          else {
            System.out.println("其他:" + (char) st.ttype);
          }
        }
        fr.close();
      }
      catch (Exception e) {
        System.out.println("發生了" + e + "例外");
     
      }
      System.out.println("------------------------StringReader----------------------------------");
      System.out.println(getUnicodeFromChar('我')); //将字符转换成unicode编码,测试“我”<->25105
      System.out.println("------------------------StringWriter----------------------------------");
      System.out.println(getCharFromUnicode(25105)); //将unicode编码转换成字符,测试25105<->“我”
      System.out.println("------------------------zip----------------------------------");
      try {
           BufferedInputStream origin = null;
           FileOutputStream dest = new
             FileOutputStream("myfigs.zip");
           ZipOutputStream out = new ZipOutputStream(new
             BufferedOutputStream(dest));
           //out.setMethod(ZipOutputStream.DEFLATED);
           byte data[] = new byte[BUFFER];
           // get a list of files from current directory
           File f = new File(".");
           String files[] = f.list();

           for (int i=0; i < files.length; i++) {
              System.out.println("Adding: "+files[i]);
              System.out.println("aaa" + files.length);
              FileInputStream fi = new
                FileInputStream(files[i]);
              origin = new
                BufferedInputStream(fi, BUFFER);
              ZipEntry entry = new ZipEntry(files[i]);
              out.putNextEntry(entry);
              int count;
              while((count = origin.read(data, 0,
                BUFFER)) != -1) {
                 out.write(data, 0, count);
              }
              origin.close();
           }
           out.close();
        } catch(Exception e) {
           e.printStackTrace();
        }

 }

// 将unicode编码转换成字符
   public static char getCharFromUnicode(int unicode) {
     StringWriter sw = new StringWriter();
     sw.write(unicode);
     return sw.getBuffer().charAt(0);
   }

// 将字符转换成unicode编码
   public static int getUnicodeFromChar(char ch) {
     StringReader sr = new StringReader(new String(new char[] {ch}));
     try {
       return sr.read();
     }
     catch (Exception ex) {
       System.out.println(ex.toString());
       return -1;
     }
   } 

}

posted on 2007-09-03 15:08 gavinkin555 阅读(270) 评论(0)  编辑 收藏 引用 所属分类: JAVA

只有注册用户登录后才能发表评论。