• 每天多一点之字符流、java压缩文件


    package com.demo.zip;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    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.io.OutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;
    
    /**
     * 
     * @Description 测试zip压缩
     * @author HackerD
     * @version 1.0
     * @CreateAt 2013-5-22 下午4:11:09
     */
    public class TestZipFile {
        private int[] no = { 1, 3, 4, 5, 7, 9, 12 };
        private double[] scores = { 77.4, 67.5, 88.3, 78.8, 98.5, 78.4, 76.4 };
    
        /**
         * 
         * @Description 将人数学号成绩按照格式:人数-学号1;学号2...-成绩1;成绩2... 写入文本文件
         * @author HackerD   
         * @CreateAt   2013-5-22 下午7:40:49  
         * @version V1.0   
         * @ModificationHistory<BR>   
         * Date         Author       Version      Description <BR>
         * ----------------------------------------------
         *
         */
        public void writeData() {
            FileWriter fw = null;
            BufferedWriter bfw = null;
            try {
                fw = new FileWriter("Score.txt");
                bfw = new BufferedWriter(fw);
                String noStr = "";
                String scoreStr = "";
                // 拼凑学号跟成绩
                for (int i = 0; i < no.length; i++) {
                    noStr += no[i] + ";";
                    scoreStr += scores[i] + ";";
                }
                // 写入文件
                bfw.write(no.length + "-" + noStr.substring(0, noStr.length() - 1)
                        + "-" + scoreStr.substring(0, scoreStr.length() - 1));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bfw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 
         * @Description 根据写入的格式从Score.txt中找出最高成绩和对应学号
         * @author HackerD   
         * @CreateAt   2013-5-22 下午7:41:01  
         * @version V1.0   
         * @ModificationHistory<BR>   
         * Date         Author       Version      Description <BR>
         * ----------------------------------------------
         *
         */
        public void findMaxScore() {
            FileReader fr = null;
            BufferedReader bfr = null;
            String info = "";
            String temp = "";
            double maxScore = 0;
            int pos = 0;
            try {
                fr = new FileReader("Score.txt");
                bfr = new BufferedReader(fr);
    
                // 读取信息存放在info中
                while ((temp = bfr.readLine()) != null) {
                    info += temp;
                }
    
                // 拆分信息
                String[] infoarr = info.split("-");
                // 拆分出成绩
                String[] scores = infoarr[2].split(";");
                // 拆分出学号
                String[] no = infoarr[1].split(";");
    
                // 找出最高成绩
                for (int i = 0; i < scores.length; i++) {
                    if (Double.parseDouble(scores[i]) > maxScore) {
                        maxScore = Double.parseDouble(scores[i]);
                        pos = i;
                    }
                }
                // 打印结果
                System.out.println("最高成绩为:" + maxScore + ";学号为:" + no[pos]);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    bfr.close();
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 
         * @Description 将Score.txt压缩Score.zip
         * @author HackerD   
         * @CreateAt   2013-5-22 下午7:41:13  
         * @version V1.0   
         * @ModificationHistory<BR>   
         * Date         Author       Version      Description <BR>
         * ----------------------------------------------
         *
         */
        public void scoreZip() {
            try {
                InputStream is = new FileInputStream("Score.txt");
                OutputStream os = new FileOutputStream("Score.zip");
                ZipOutputStream zf = new ZipOutputStream(os);
                // 添加条目
                zf.putNextEntry(new ZipEntry("Score.txt"));
    
                int n = 0;
                // 写入压缩包
                while ((n = is.read()) != -1) {
                    zf.write(n);
                }
    
                zf.close();
                os.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 
         * @Description 将Socre.zip解压到test文件夹里
         * @author HackerD   
         * @CreateAt   2013-5-22 下午7:41:25  
         * @version V1.0   
         * @ModificationHistory<BR>   
         * Date         Author       Version      Description <BR>
         * ----------------------------------------------
         *
         */
        public void scoreUnzip() {
            try {
                ZipFile zf = new ZipFile("Score.zip");
                // 返回指定 ZIP 文件条目。
                ZipEntry zEntry = zf.getEntry("Score.txt");
                // 返回输入流以读取指定 ZIP 文件条目的内容。
                InputStream is = zf.getInputStream(zEntry);
                OutputStream os = new FileOutputStream("test/Score.txt");
                int n = 0;
                // 解压
                while ((n = is.read()) != -1) {
                    os.write(n);
                }
    
                os.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            TestZipFile testZipFile = new TestZipFile();
            testZipFile.writeData();
            testZipFile.findMaxScore();
            testZipFile.scoreZip();
            testZipFile.scoreUnzip();
        }
    
    }
  • 相关阅读:
    10:简单密码
    08:字符替换
    07:配对碱基链
    05:输出亲朋字符串
    18:等差数列末项计算
    09:密码翻译
    用最通俗的话说23种设计模式之代理模式
    Android学习之 UI效果
    精确到时分秒的jQuery插件例子
    Eclipse 常用快捷键
  • 原文地址:https://www.cnblogs.com/hackerd/p/3093668.html
Copyright © 2020-2023  润新知