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 map = new TreeMap<>();
    Map 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);
}

}

你可能感兴趣的:(IO作业)