IO作业

package com.qst.demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class IOWork {

public static void main(String[] args) {
    fileAnalyze();
}
//复制图片
public static void imgcopy() {
    FileInputStream in = null;
    BufferedInputStream bin = null;
    FileOutputStream out = null;
    BufferedOutputStream bout = null;
    try {
        in = new FileInputStream("D:/20140326120340890.jpg");
        bin = new BufferedInputStream(in);
        out = new FileOutputStream("E:/20140326120340890.jpg");
        bout = new BufferedOutputStream(out);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bin.read(b)) != -1) {
            bout.write(b, 0, len);

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            bin.close();
            in.close();
            bout.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
//读取图片并统计热度
public static void fileAnalyze() {

    File file = new File("D:/Users/renxoangyu/Desktop/view.log");
    FileReader read = null;
    BufferedReader reader = null;
    Map<String, Integer> map = new TreeMap<>();
    Map<String, Integer> map1 = new TreeMap<>();
    try {
        read = new FileReader(file);
        reader = new BufferedReader(read);
        String a = "";
        while ((a = reader.readLine())!=null) {
            System.out.println(a);
            String []b = a.split("  ");
            Date date = new Date(Long.parseLong(b[0]));
            DateFormat dateFormat = DateFormat.getDateTimeInstance();
            //输出结果为  2017-3-22 20:45:16
            String datestr = dateFormat.format(date);
            String d = datestr.split(" ")[0]+" "+b[2];
            String c = datestr+" "+b[2];
            if (map.containsKey(c)) {
                map.put(c, map.get(c)+1);
            } else {
                map.put(c, 1);
            }
            if (map1.containsKey(d)) {
                map1.put(d, map1.get(d)+1);
            } else {
                map1.put(d, 1);
            }
        }


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        try {
            reader.close();
            read.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    System.out.println(map);
    System.out.println(map1);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值