diff --git a/CountMostImport.java b/CountMostImport.java new file mode 100644 index 0000000..9d5c56d --- /dev/null +++ b/CountMostImport.java @@ -0,0 +1,103 @@ +package com.CountMostImport; + +import java.io.*; +import java.util.*; + + +/** + * 递归读取目录下所有文件,统计import的类的个数 + * 这个版本没考虑“import java.io.*”这种带*号的情况,只是将这种情况视作一个类来看待。 + * 如果“import java.io.*”排名第9,且没有其它io包的类被引用。如何确定io包有哪几个类占据第9,第10? + */ +public class CountMostImport { + private HashMap hashMap = new HashMap(); //将所有类放到该字典里统计 + + /* + * 递归读取目录下所有文件 + */ + public void getfile(String filename) { + File file = new File(filename); + if (file.isDirectory()) { + File []files = file.listFiles(); //查看子目录下所有文件 + for (File ff : files) { + this.getfile(String.format("%s", ff)); //递归调用子目录下文件 + } + } + if (file.isFile()){ + countImport(String.format("%s", file)); //调用读取文件的函数 + } + } + + /* + * 统计文件中被引用的类的个数 + */ + public void countImport(String filename) { + try { + FileReader fin = new FileReader(filename); + BufferedReader bufferedReader = new BufferedReader(fin); //打开文件 + + String line = null; + while ((line = bufferedReader.readLine()) != null) { + line = line.trim(); + if (line.startsWith("import")) { //如果文件是引用类语句 + int begin = 6; + while (line.charAt(begin) == ' ') { + ++begin; + } + String classpath = line.substring(begin,line.length()-1); + if (hashMap.containsKey(classpath)) { //字典中已经有这个类了,次数+1 + int value = hashMap.get(classpath); + value++; + hashMap.put(classpath,value); + } else { //字典中没有这个类,把这个类加入字典中 + hashMap.put(classpath,1); + } + } + //如果遇到类声明了,则确认import语句块已经结束 + if (line.startsWith("public") || line.startsWith("class")) { + break; + } + } + bufferedReader.close(); + fin.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 字典按值排降序,将前十输出 + */ + public void sortAndPrintTop10() { + List> list = new ArrayList>(hashMap.entrySet()); + Collections.sort(list, new Comparator>() { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + if (o1.getValue() == o2.getValue()) { + return o1.getKey().compareTo(o2.getKey()); //如果次数一样,按字典序列排序 + } + return o1.getValue() - o2.getValue(); //降序排列 + } + }); + int cursor = 0; //计数器,只输出前十 + for (Map.Entry map: list) { + System.out.println(map.getKey()+" : "+map.getValue()); + cursor++; + if (cursor >= 10) { + break; + } + } + } + + public static void main(String[] args) { + CountMostImport countMostImport = new CountMostImport(); + if (args.length < 1) { + System.out.println("this program need 1 arguments"); + System.exit(0); //以后看情况决定是否选1 + } + countMostImport.getfile(args[0]); //输入文件名 + countMostImport.sortAndPrintTop10(); + } +} diff --git a/EffectiveLines.java b/EffectiveLines.java new file mode 100644 index 0000000..bcd79f9 --- /dev/null +++ b/EffectiveLines.java @@ -0,0 +1,59 @@ +package com.EffectiveLines; + +import java.io.*; + +/** + * 统计有效文件行数,不考虑多行注释 + */ +public class EffectiveLines { + private int couners; //统计有效行数的计数器 + + public EffectiveLines() { + this.couners = 0; + } + + /* + * 读入文件,统计有效行数 + * filename为文件名 + * 返回有效行数 + */ + public int readFile(String filename) { + File file = new File(filename); + try { + FileInputStream fileInputStream = new FileInputStream(file); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String line = null; + while((line = bufferedReader.readLine()) != null) { + int cursor = 0; //游标,遍历一行数据用 + line = line.replace(" ",""); //去掉所有空格 + if(line.length() == 0) { //全是空格的,不是有效行 + continue; + } + //去掉空格后,剩下的是注释的话,也不是有效行 + if(line.charAt(0)=='/' && (line.charAt(1)=='/' || line.charAt(1)=='*')) { + continue; + } + couners++; + } + bufferedReader.close(); + inputStreamReader.close(); + fileInputStream.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return this.couners; + } + + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("this program needs 1 arguments"); + System.exit(0); + } + EffectiveLines effectiveLines = new EffectiveLines(); + int answer = effectiveLines.readFile(args[0]); + System.out.println(answer); + } +} diff --git a/ExchangeRate.java b/ExchangeRate.java new file mode 100644 index 0000000..6409f6d --- /dev/null +++ b/ExchangeRate.java @@ -0,0 +1,164 @@ +package com.ExchangeRate; + +import jxl.Cell; +import jxl.Sheet; +import jxl.Workbook; +import jxl.read.biff.BiffException; +import jxl.write.Label; +import jxl.write.WritableSheet; +import jxl.write.WritableWorkbook; +import jxl.write.WriteException; +import jxl.write.biff.RowsExceededException; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * 从网站下载30天的所有人民币汇率数据,存入webData.xls + * 分析表格数据: + * 读取该表格文档,计算一个月内人民币汇率中间价,并输出至analysis.xls + * 需要导入jxl.jar包 + */ +public class ExchangeRate { + + private double dollar; //美元汇率 + private double euro; //欧元汇率 + private double hk_dollar; //港元汇率 + + public ExchangeRate() { + this.dollar = 0; + this.euro = 0; + this.hk_dollar = 0; + } + + /* + *从网站下载30天的所有人民币汇率数据,存入webData.xls + */ + public void getWebData() { + InputStream inputStream = null; + FileOutputStream fileOutputStream = null; + URLConnection urlConnection = null; + + Calendar calendar = Calendar.getInstance(); + Date endDate = calendar.getTime(); //当前日期 + calendar.add(Calendar.DATE,-30); + Date startDate = calendar.getTime(); //30天前日期 + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //日期格式 + String startDateStr = simpleDateFormat.format(startDate); + String endDateStr = simpleDateFormat.format(endDate); + +// System.out.println(startDateStr); +// System.out.println(endDateStr); + + //发送“中国外汇管理局”的导出表单请求 + String url = "http://www.safe.gov.cn/AppStructured/view/project_exportRMBExcel.action?projectBean.startDate=" + + startDateStr + "projectBean.endDate=" + endDateStr + "queryYN=true"; + System.out.println(url); + try { + urlConnection = new URL(url).openConnection(); + inputStream = urlConnection.getInputStream(); + fileOutputStream = new FileOutputStream("webData.xls"); //注意,因为周末没有数据,所以里面不是30行数据 + + byte [] bytes = new byte[1024]; + int len = -1; + while ((len = inputStream.read(bytes)) != -1) { //输出内容到本地表格上 + fileOutputStream.write(bytes,0,len); + fileOutputStream.flush(); + } + inputStream.close(); + fileOutputStream.close(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 从webData.xls中读入数据,并分析计算汇率 + */ + public void readExcel() { + Workbook workbook = null; + try { + InputStream inputStream = new FileInputStream("webData.xls"); + workbook = Workbook.getWorkbook(inputStream); + Sheet sheet = workbook.getSheet(0); //找到文件第一张表格 + int rows = sheet.getRows(); //获取总行数 + + //计算美元、欧元、港元汇率 + for (int i = 1; i < rows; ++i) { + Cell dollar_cell = sheet.getCell(1,i); + dollar += Double.parseDouble(dollar_cell.getContents()); + + Cell euro_cell = sheet.getCell(2,i); + euro += Double.parseDouble(euro_cell.getContents()); + + Cell hk_dollar_cell = sheet.getCell(4,i); + hk_dollar += Double.parseDouble(hk_dollar_cell.getContents()); + } + dollar = dollar/rows; + euro = euro/rows; + hk_dollar = hk_dollar/rows; + + inputStream.close(); + workbook.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (BiffException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 创建analysis.xls文件,并将分析结果输出到文件中 + */ + public void writeExcel() { + try { + OutputStream os = new FileOutputStream(new File("analysis.xls")); + WritableWorkbook writableWorkbook = Workbook.createWorkbook(os); + WritableSheet writableSheet = writableWorkbook.createSheet("人民币汇率分析",0); //创建表格文件中第一页表格 + + //输出表格第一行内容 + Label jieshao = new Label(0,0,"人民币汇率中间价"); + writableSheet.addCell(jieshao); + Label meiyuan = new Label(1,0,"美元"); + writableSheet.addCell(meiyuan); + Label ouyuan = new Label(2,0,"欧元"); + writableSheet.addCell(ouyuan); + Label gangyuan = new Label(3,0,"港元"); + writableSheet.addCell(gangyuan); + + //输出计算结果到表格中 + Label l_dollar = new Label(1,1,""+dollar); + writableSheet.addCell(l_dollar); + Label l_euro = new Label(2,1,""+euro); + writableSheet.addCell(l_euro); + Label l_hk = new Label(3,1,""+hk_dollar); + writableSheet.addCell(l_hk); + + writableWorkbook.write(); + writableWorkbook.close(); + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } catch (RowsExceededException e) { + e.printStackTrace(); + } catch (WriteException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ExchangeRate er = new ExchangeRate(); + er.getWebData(); + er.readExcel(); + er.writeExcel(); + } +} diff --git a/Top10Ip.sh b/Top10Ip.sh new file mode 100644 index 0000000..ddcd716 --- /dev/null +++ b/Top10Ip.sh @@ -0,0 +1,6 @@ +# tomcataccess logиʽ£ +# 127.0.0.1 37 [05/Jun/2012:17:23:43 +0800] -- 200 2806 127.0.0.1 8080 GET/cdbleasing/message.listMessagePrompt.action?_dc=1338888223333 HTTP/1.1 +# õһֶεip㹻ˡͳip +# ļƣlocalhost_access_log*.txtļͨ *access_log*.txt + +awk -F" " '{print $1}' *access_log*.txt|sort|uniq -c|sort -nr|head -n10 \ No newline at end of file diff --git a/linux2.sh b/linux2.sh new file mode 100644 index 0000000..9fe8937 --- /dev/null +++ b/linux2.sh @@ -0,0 +1,5 @@ +# Զscp +# scp ļ Զû@Զ̻ļַ +# ֪Զûʲôдroot + +scp /dir1 root@l-test.dev.cn1:/tmp \ No newline at end of file