diff --git a/CountMostImport.java b/CountMostImport.java new file mode 100644 index 0000000..910cf86 --- /dev/null +++ b/CountMostImport.java @@ -0,0 +1,103 @@ +import java.io.*; +import java.util.*; +import java.util.Map.Entry; + + + +public class CountMostImport { + + static HashMap importCount; + //输出import最多的前NUMBER个类 + public static final int NUMBER = 10; + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("请输入java项目的路径:"); + importCount = new HashMap(); + countMostImport(scan.nextLine()); + } + + public static void countMostImport(String path) { + File file = new File(path); + + if (!file.isDirectory()) + { //目录不存在 + System.out.println("java项目路径不存在!"); + return; + } + //遍历各个文件并进行统计 + filesList(file, importCount); + + //对统计结果进行排序并打印 + printImportClass(importCount); + + } + public static void printImportClass(HashMap importCount) { + //对对HashMap按值进行排序 + List> list = new ArrayList>(importCount.entrySet()); + Collections.sort(list, new Comparator>() { + //降序排序 + @Override + public int compare(Entry o1, Entry o2) { + if(o2.getValue().compareTo(o1.getValue())==0){ + //如果值相等,则按照字典序列进行排序 + String str1 = o1.getKey().toLowerCase();//转换成小写是因为在ASICC中大写字符一定在小写字母前面,与字典序列不符 + String str2 = o2.getKey().toLowerCase(); + return str1.compareTo(str2); + } + else + return o2.getValue().compareTo(o1.getValue()); + } + }); + + //遍历输出被import最多前NUMBER个的类 + int count = 0; + for (Entry mapping : list) { + if( count importCount) { + File[] files = file.listFiles(); + for(File fileName : files) { + //如果是文件夹,遍历每个文件和文件夹 + if(fileName.isDirectory()) { + filesList(fileName, importCount); + } + else { + String suffix = fileName.getName().substring(fileName.getName().lastIndexOf(".") + 1); + //判断是否是java文件 + if(suffix.equals("java")) { + BufferedReader bufferedReader = null; + try { + bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); + String sentence = null; + while((sentence = bufferedReader.readLine()) != null){ + String trimSentence = sentence.trim(); + //判断是否为引用包语句 + if(trimSentence.indexOf("import ") == -1 || !trimSentence.trim().startsWith("import ")) + continue; + + String importClassName = trimSentence.substring( trimSentence.indexOf("import ")+"import ".length() ).trim(); + if(importCount.containsKey(importClassName)) + importCount.put(importClassName, importCount.get(importClassName)+1); + else + importCount.put(importClassName, 1); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if(bufferedReader != null) { + bufferedReader.close(); + } + } catch (Exception e) { + } + } + } + } + } + } +} \ No newline at end of file diff --git a/EffectiveLines.java b/EffectiveLines.java new file mode 100644 index 0000000..a761da8 --- /dev/null +++ b/EffectiveLines.java @@ -0,0 +1,67 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + + +public class EffectiveLines { + + private static int whiteLines = 0; + private static int commentLines = 0; + private static int normalLines = 0; + + + /** + * @param args + */ + public static void main(String[] args) { + File f = new File("F:\\Idea_pro\\EffectiveLines.java"); + sumCode(f); + } + + private static void sumCode(File file) { + BufferedReader br = null; + boolean comment = false; + try { + br = new BufferedReader(new FileReader(file)); + String line = ""; + try { + while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.trim().equals("")) { + whiteLines++; + } else if (line.startsWith("/*") && !line.endsWith("*/")) { + commentLines++; + comment = true; + } else if (true == comment) { + commentLines++; + if (line.endsWith("*/")) { + comment = false; + } + } else if (line.startsWith("//")) { + commentLines++; + } else { + normalLines++; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally { + if (br != null) { + try { + //System.out.println("空行数:"+whiteLines); + //System.out.println("注释行数:"+commentLines); + System.out.println("有效行数:"+normalLines); + br.close(); + br = null; + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/ExchangeRate.java b/ExchangeRate.java new file mode 100644 index 0000000..00011a8 --- /dev/null +++ b/ExchangeRate.java @@ -0,0 +1,178 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.URL; +import java.net.URLConnection; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; + + + +public class ExchangeRate { + private File crawlerResult; + private String rateResult; + public ExchangeRate(){ + crawlerResult = new File("F:\\Idea_pro\\crawlerResult.html"); + rateResult = "F:\\Idea_pro\\rateResult.xls"; + } + public void crawler_30_days(){ + String address = "http://www.safe.gov.cn/AppStructured/view/project_RMBQuery.action"; + Date date = new Date(); + GregorianCalendar gc =new GregorianCalendar(); + gc.setTime(date); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//获取系统的日期 + String endDate = df.format(gc.getTime()); + gc.add(5, -29); + String beginDate = df.format(gc.getTime()); + System.out.println("从:"+beginDate); + System.out.println("到:"+endDate); + String param = "projectBean.startDate=" + beginDate + "&projectBean.endDate=" + endDate + "&queryYN=true"; + PrintWriter out = null; + BufferedReader in = null; + OutputStreamWriter outWriter = null; + try{ + URL url = new URL(address); + URLConnection conn = url.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + conn.setDoOutput(true); + conn.setDoInput(true); + out = new PrintWriter(conn.getOutputStream()); + out.print(param); + out.flush(); + in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); + outWriter = new OutputStreamWriter(new FileOutputStream(crawlerResult),"UTF-8"); + String str = null; + while((str = in.readLine()) != null){ + outWriter.write(str + "\n"); + } + }catch(Exception e){ + System.out.println("在发送Post请求时发生错误!"); + e.printStackTrace(); + }finally{ + try{ + if(outWriter != null) + outWriter.close(); + if(in != null) + in.close(); + if(out != null) + out.close(); + }catch(Exception e){ + e.printStackTrace(); + } + } + + } + + public void writeExcel(){ + Document doc = null; + try { + doc = Jsoup.parse(crawlerResult,"UTF-8"); + } catch (IOException e) { + e.printStackTrace(); + } + Elements links = doc.getElementsByAttributeValue("class", "first"); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet("汇率表"); + HSSFRow row = sheet.createRow((int) 0); + HSSFCellStyle style = wb.createCellStyle(); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 + + HSSFCell cell = row.createCell((int)0); + cell.setCellValue("日期"); + cell.setCellStyle(style); + cell = row.createCell((int) 1); + cell.setCellValue("美元"); + cell.setCellStyle(style); + cell = row.createCell((int) 2); + cell.setCellValue("欧元"); + cell.setCellStyle(style); + cell = row.createCell((int) 3); + cell.setCellValue("港币"); + cell.setCellStyle(style); + Date date = new Date(); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + try { + date = df.parse(df.format(date)); + } catch (ParseException e1) { + e1.printStackTrace(); + } + GregorianCalendar gc =new GregorianCalendar(); + gc.setTime(date); + gc.add(5,1); + int rowno = 0; + + for(int i = 0; i < links.size(); ++i){ + String tmp = links.get(i).text().replace(Jsoup.parse(" ").text(), " "); + String rate[] = tmp.split("[ ]{1,}"); + String d = rate[0]; + Date tmpDate = null; + try { + tmpDate = df.parse(d); + } catch (ParseException e) { + e.printStackTrace(); + } + while((date.getTime() - tmpDate.getTime())/(24*60*60*1000) > 1){ + row = sheet.createRow((int) rowno + 1); + gc.add(5, -1); + row.createCell((int) 0).setCellValue(df.format(gc.getTime())); + row.createCell((int) 1).setCellValue("--"); + row.createCell((int) 2).setCellValue("--"); + row.createCell((int) 3).setCellValue("--"); + ++rowno; + + date = gc.getTime(); + } + row = sheet.createRow((int) rowno + 1); + rowno++; + row.createCell((int) 0).setCellValue(rate[0]); + row.createCell((int) 1).setCellValue(rate[1]); + row.createCell((int) 2).setCellValue(rate[2]); + row.createCell((int) 3).setCellValue(rate[4]); + gc.add(5,-1);//减一天 + date = gc.getTime(); + } + while(rowno < 30){ + row = sheet.createRow((int) rowno + 1); + rowno++; + gc.add(5,-1); + row.createCell((int) 0).setCellValue(df.format(gc.getTime())); + row.createCell((int) 1).setCellValue("--"); + row.createCell((int) 2).setCellValue("--"); + row.createCell((int) 3).setCellValue("--"); + } + try + { + FileOutputStream fout = new FileOutputStream(rateResult); + wb.write(fout); + fout.close(); + System.out.println("Excel文件生成成功!"); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + public static void main(String args[]) throws IOException{ + ExchangeRate rate = new ExchangeRate(); + rate.crawler_30_days(); + rate.writeExcel(); + } +} \ No newline at end of file diff --git a/Top10Ip.sh b/Top10Ip.sh new file mode 100644 index 0000000..1366df9 --- /dev/null +++ b/Top10Ip.sh @@ -0,0 +1 @@ +awk '{print $1}' access.log|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..db33c9f --- /dev/null +++ b/linux2.sh @@ -0,0 +1 @@ +scp /tmp/filename username@l-test.dev.cn1:/tmp/ \ No newline at end of file