From d7d6a926e21acf67be7e573629f18465ab59843d Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Mon, 17 Sep 2018 21:53:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20201621123086/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123086/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123086/.keep diff --git a/201621123086/.keep b/201621123086/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From 4a770a0b505e97f8ec6cd35fa8be6c81bc410975 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Mon, 17 Sep 2018 21:55:46 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20src/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123086/src/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123086/src/.keep diff --git a/201621123086/src/.keep b/201621123086/src/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From fe547954e6b9d1418e9df7c77b48623a958fedf6 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Mon, 17 Sep 2018 21:56:12 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20wordConut/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123086/src/wordConut/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123086/src/wordConut/.keep diff --git a/201621123086/src/wordConut/.keep b/201621123086/src/wordConut/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From b5691e685e77b8df2b99845537abf84734fd2eda Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Mon, 17 Sep 2018 21:56:39 +0800 Subject: [PATCH 4/4] Upload Main.java FileIO.java WordCount.java --- 201621123086/src/wordConut/FileIO.java | 77 ++++++++++++++++++++++ 201621123086/src/wordConut/Main.java | 39 +++++++++++ 201621123086/src/wordConut/WordCount.java | 80 +++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 201621123086/src/wordConut/FileIO.java create mode 100644 201621123086/src/wordConut/Main.java create mode 100644 201621123086/src/wordConut/WordCount.java diff --git a/201621123086/src/wordConut/FileIO.java b/201621123086/src/wordConut/FileIO.java new file mode 100644 index 0000000..53bcea8 --- /dev/null +++ b/201621123086/src/wordConut/FileIO.java @@ -0,0 +1,77 @@ +package wordCount; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; + +public class FileIO { + + + + public static String readFileByStream(String filePath)throws IOException{ + FileReader reader = new FileReader(filePath); + File file = new File(filePath); + long i = file.length() ; + char[] c = new char[(int) i]; + reader.read(c); + String str = String.valueOf(c); + reader.close(); + return str; + } + + public static String[] readFile(String filePath)throws IOException{ + BufferedReader reader = null; + int i = 0 ; + String[] str = new String[120000]; + File file = new File(filePath); + if(!file.exists()) { + return null; + } + try { + reader = new BufferedReader(new FileReader(filePath)); + String tempString ; + while ((tempString = reader.readLine()) != null) { + str[i++] = tempString; + } + reader.close(); + return str; + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + } + + + + public static String writeFile(String filePath, String str) throws IOException { + BufferedWriter out = null; + try { + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true))); + out.write(str); + out.newLine(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if(out != null){ + out.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return ""; + } + +} diff --git a/201621123086/src/wordConut/Main.java b/201621123086/src/wordConut/Main.java new file mode 100644 index 0000000..a20749a --- /dev/null +++ b/201621123086/src/wordConut/Main.java @@ -0,0 +1,39 @@ +package wordCount; + +import java.io.IOException; +import java.util.ArrayList; + +public class Main { + + public static void main(String[] args) throws IOException { + // TODO Auto-generated method stub + String filePath = args[0]; + if(filePath == null) { + System.out.println("please input the file name!"); + System.exit(0); + } +// String filePath = "E:\\eclipse-workspace\\WordCount\\t9.txt"; + String resultPath = "result.txt"; + String[] strs = FileIO.readFile(filePath); + if(strs == null) { + System.out.println("couldn't find the file"); + System.exit(0); + } + String str = FileIO.readFileByStream(filePath); + ArrayList words = WordCount.countWords(strs); + System.out.println("characters: "+ WordCount.countChar(str)); + FileIO.writeFile(resultPath, "characters: "+ WordCount.countChar(str)); + FileIO.writeFile(resultPath, "words: "+ words.size()); + FileIO.writeFile(resultPath, "lines: "+ WordCount.countLines(strs)); + System.out.println("words: "+ words.size()); + System.out.println("lines: "+ WordCount.countLines(strs)); + for(int i = 0 ; i < 10 ; i++ ) { + if(i == words.size()) { + break; + } + FileIO.writeFile(resultPath, words.get(i)); + System.out.println(words.get(i)); + } + } + +} diff --git a/201621123086/src/wordConut/WordCount.java b/201621123086/src/wordConut/WordCount.java new file mode 100644 index 0000000..f04aa0c --- /dev/null +++ b/201621123086/src/wordConut/WordCount.java @@ -0,0 +1,80 @@ +package wordCount; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class WordCount { + + + + public static int countChar(String strs) { + char c ; + int sum = 0; + for (int i = 0; i < strs.length(); i++) { + c = strs.charAt(i); + if (c >= 32 && c <= 126 || c == '\r' || c == '\n'|| c == '\t') { + sum++; + } + } + return sum; + } + + public static int countLines(String[] strs) { + int sum = 0; + for (int i = 0; i < strs.length; i++) { + if ( strs[i] == null) { + break; + } + if (strs[i].trim().length() == 0 || strs[i].trim().equals("")) { + continue; + } + sum++; + } + return sum; + } + + public static ArrayList countWords(String[] strs) { + ArrayList result = new ArrayList(); + Map map = new HashMap(); + Pattern p = Pattern.compile("\\b[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][A-Za-z0-9]*\\b"); + for (int i = 0; i < strs.length; i++) { + if ( strs[i] == null) { + continue; + } + if (strs[i].length() == 0 || strs[i].equals("")) { + continue; + } + Matcher m = p.matcher(strs[i]); + while (m.find()) { + String mstr = m.group(); + if (map.containsKey(mstr)) { + map.put(mstr, map.get(mstr) + 1); + } else { + map.put(mstr, 1); + } + } + } + List> list = new ArrayList>(map.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Entry o1, Entry o2) { + if (o1.getValue() == o2.getValue()) { + return o1.getKey().compareTo(o2.getKey()); + } + return o2.getValue() - o1.getValue(); + } + + }); + for(Map.Entry mapping:list){ + result.add("<"+mapping.getKey().toLowerCase()+">" + ": " + mapping.getValue()); + } + + return result; + } +} -- Gitee