From ff19bbd362eff4f83fb0158442aad3baa5ffd7b7 Mon Sep 17 00:00:00 2001 From: Dzwh <2672189610@qq.com> Date: Sun, 16 Sep 2018 19:29:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20201621123008?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123008 | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123008 diff --git a/201621123008 b/201621123008 new file mode 100644 index 0000000..e69de29 -- Gitee From 353f618023532a370f3d152f8d4eb9b07f4bc106 Mon Sep 17 00:00:00 2001 From: Dzwh <2672189610@qq.com> Date: Sun, 16 Sep 2018 19:31:03 +0800 Subject: [PATCH 2/2] Upload wordcount/FileRead.java wordcount/WordCount.java wordcount/Main.java --- wordcount/FileRead.java | 109 +++++++++++++++++++++++++++++++++++++++ wordcount/Main.java | 42 +++++++++++++++ wordcount/WordCount.java | 88 +++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100644 wordcount/FileRead.java create mode 100644 wordcount/Main.java create mode 100644 wordcount/WordCount.java diff --git a/wordcount/FileRead.java b/wordcount/FileRead.java new file mode 100644 index 0000000..d13699c --- /dev/null +++ b/wordcount/FileRead.java @@ -0,0 +1,109 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package wordcount; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Map; +import java.util.TreeMap; + +/** + * + * @author Administrator + */ +public class FileRead { + + public FileRead() { + } + + public WordCount readTxtFile(String filePath) { + WordCount count = new WordCount(); + Map dic = new TreeMap(); + int characters = 0; + int words = 0; + int lines = 0; + int line = 0; + String s = "!@#$%^&*().|,\t? \\"; + String str = ""; + ArrayList array = new ArrayList<>(); + try { + String encoding = "UTF-8"; + File file = new File(filePath); + if (file.isFile() && file.exists()) { + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), encoding); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + while ((lineTxt = bufferedReader.readLine()) != null) { + lines++; + characters = characters + lineTxt.length(); + str = ""; + if (lineTxt.length() == 0) { + line++; + } + for (int i = 0; i < lineTxt.length(); i++) { + if (s.indexOf(lineTxt.charAt(i)) >= 0) { + if (str.length() >= 4) { + if (array.contains(str.toLowerCase())) { + int value = Integer.parseInt(dic.get(str.toLowerCase())) + 1; + dic.put(str.toLowerCase(), "" + value); + str=""; + } else { + array.add(str.toLowerCase()); + dic.put(str.toLowerCase(), "" + 1); + str=""; + words++; + } + } else { + str = ""; + } + } + if ((lineTxt.charAt(i) >= 'a' && lineTxt.charAt(i) <= 'z') || (lineTxt.charAt(i) >= 'A' && lineTxt.charAt(i) <= 'Z')) { + str = str + lineTxt.charAt(i); + } + if (lineTxt.charAt(i) >= '0' && lineTxt.charAt(i) <= '9') { + if (str.length() >= 4) { + str = str + lineTxt.charAt(i); + } else { + str = ""; + } + } + if (i == lineTxt.length() - 1) { + if (str.length() >= 4) { + if (array.contains(str.toLowerCase())) { + int value = Integer.parseInt(dic.get(str.toLowerCase())) + 1; + dic.put(str.toLowerCase(), "" + value); + } else { + array.add(str.toLowerCase()); + dic.put(str.toLowerCase(), "" + 1); + } + } else { + str = ""; + } + } + } + } + read.close(); + count.setCharacters(characters + lines-1); + count.setLines(lines - line); + count.setWords(words); + count.setDic(dic); + } else { + System.out.println("找不到指定的文件"); + } + } catch (Exception e) { + System.out.println("读取文件内容出错"); + e.printStackTrace(); + } + if(characters==0) + return null; + else + return count; + } +} diff --git a/wordcount/Main.java b/wordcount/Main.java new file mode 100644 index 0000000..486e6b9 --- /dev/null +++ b/wordcount/Main.java @@ -0,0 +1,42 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package wordcount; + +import java.util.List; +import java.util.Map; + +/** + * + * @author Administrator + */ +public class Main { + public static void main(String[] args) { + + FileRead fileRead = new FileRead(); + WordCount count=fileRead.readTxtFile("D:\\Netbeanworkspace\\WordCount\\test\\1.txt"); + if(count==null){ + System.out.println("文件里的数据为空"); + } + else{ + List> list=count.sortMap(); + System.out.println("characters:"+count.getCharacters()); + System.out.println("words:"+count.getWords()); + System.out.println("lines:"+count.getLines()); + if(list.size()<10){ + for(int i=0;i:"+list.get(i).getValue()); + } + } + else{ + for(int i=0;i<10;i++){ + System.out.println("<"+list.get(i).getKey()+">:"+list.get(i).getValue()); + } + } + + } + System.out.println(args); + } +} diff --git a/wordcount/WordCount.java b/wordcount/WordCount.java new file mode 100644 index 0000000..543e674 --- /dev/null +++ b/wordcount/WordCount.java @@ -0,0 +1,88 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package wordcount; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * + * @author Administrator + */ +public class WordCount { + + private int characters; + private int words; + private int lines; + Map dic = new TreeMap(); + + public WordCount() { + this.characters = 0; + this.words = 0; + this.lines = 0; + } + + public void Mapsort() { + + } + + public void setDic(Map dic) { + this.dic = dic; + } + + public int getCharacters() { + return characters; + } + + public void setCharacters(int characters) { + this.characters = characters; + } + + public int getWords() { + return words; + } + + public void setWords(int words) { + this.words = words; + } + + public int getLines() { + return lines; + } + + public void setLines(int lines) { + this.lines = lines; + } + + + + public List> sortMap() { + List> list = new ArrayList>(this.dic.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Entry o1, Entry o2) { + int value1=Integer.parseInt(o1.getValue()); + int value2=Integer.parseInt(o2.getValue()); + if(value1==value2){ + return 0; + } + else{ + return value2-value1; + } + } + }); + if(list.size()==0){ + return null; + }else{ + return list; + } + } + +} -- Gitee