62,620
社区成员
发帖
与我相关
我的任务
分享
package readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class WordsCount {
HashMap<String,Integer> hashMap;
BufferedReader infile;
String filename = "src/readfile/test1.txt";
String string;
String outpath = "src/readfile/test.txt";
@SuppressWarnings("unchecked")
public WordsCount() throws IOException{
infile = new BufferedReader(new FileReader(filename));
hashMap=new HashMap<String,Integer>();
while((string = infile.readLine()) !=null) {
String[] words=string.split(" ");
for(int i=0;i<words.length;i++){
if(words[i].trim().equals("")){
continue;
}
String astr=words[i].trim();
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring( 0,astr.length());
}
if(hashMap.containsKey(words[i])){
Integer count=(Integer) hashMap.get(words[i]);
count++;
hashMap.remove(astr);
hashMap.put(astr, count);
}else{
hashMap.put(astr, 1);
}
}
}
infile.close();
List<String> arrayList=new ArrayList<String>();
Iterator<?> iter = hashMap.entrySet().iterator();
outer:while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
char aChar=key.charAt(0);
for(int i=0;i<arrayList.size();i++){
if(aChar<arrayList.get(i).charAt(0)){
arrayList.add(i,key);
continue outer;
}
}
arrayList.add(key);
}
StringBuffer outContent=new StringBuffer();
for(int i=0;i<arrayList.size();i++){
String key=arrayList.get(i);
outContent.append(key+hashMap.get(key)+"\r\n");
System.out.println(key+hashMap.get(key));
}
FileOutputStream outs=new FileOutputStream(new File(outpath));
outs.write(outContent.toString().getBytes());
outs.flush();
outs.close();
}
public static void main(String[] args){
try {
new WordsCount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class NewClass {
HashMap<String,Integer> hashMap;
BufferedReader infile;
String filename = "src/readfile/test3.txt";
String string;
String outpath = "src/readfile/test.txt";
class UWord implements Comparator<? extends UWord> {
private String word;
public UWord(String word) {this.word = word;}
public String toString() {return word;}
public boolean equals(Object o) {
if (o==null || !(o instanceof UWord)) return false;
return word.equalsIgnoreCase(((UWord)o).word); //
}
public int hashCode() {
return word.toLowerCase().hashCode(); //
}
}
@SuppressWarnings("unchecked")
public NewClass() throws IOException{
infile = new BufferedReader(new FileReader(filename));
hashMap=new HashMap<String,Integer>();
while((string = infile.readLine()) !=null) {
String[] words=string.split("[0-9\\.\\@_\\-~#\\,\\:\\;\\(\\)\\[\\]\\%\\&\\’\\‘\\—\\{\\}\\</\\>\\=\\s]+");
for(int i=0;i<words.length;i++){
HashMap<UWord,Integer> hashMap;
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring( 0,astr.length());
}
UWord w = new UWord(astr);
//if(hashMap.containsKey(words[i])){
if(hashMap.containsKey(w)){
//Integer count=(Integer) hashMap.get(words[i]);
//count++;
//hashMap.remove(astr);
//hashMap.put(astr, count);
hashMap.put(w, hashMap.get(w)+1);
}else{
//hashMap.put(astr, 1);
hashMap.put(w, 1);
}
}
}
infile.close();
List<String> arrayList=new ArrayList<String>();
Iterator<?> iter = hashMap.entrySet().iterator();
outer:while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
//key=key.replaceAll("(?i)","");
char aChar=key.charAt(0);
for(int i=0;i<arrayList.size();i++){
if(aChar<arrayList.get(i).charAt(0)){
arrayList.add(i,key);
continue outer;
}
}
arrayList.add(key);
}
StringBuffer outContent=new StringBuffer();
for(int i=0;i<arrayList.size();i++){
String key=arrayList.get(i);
outContent.append(key+" "+hashMap.get(key)+"次"+"\r\n");
System.out.println(key+hashMap.get(key));
}
FileOutputStream outs=new FileOutputStream(new File(outpath));
outs.write(outContent.toString().getBytes());
outs.flush();
outs.close();
}
public static void main(String[] args){
try {
new WordsCount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class UWord implements Comparator<? extends UWord> {
private String word;
public UWord(String word) {this.word = word;}
public String toString() {return word;}
public boolean equals(Object o) {
if (o==null || !(o instanceof UWord)) return false;
return word.equalsIgnoreCase(((UWord)o).word); //忽略大小写
}
public int hashCode() {
return word.toLowerCase().hashCode(); //统一为小写或大写
}
}
//然后
HashMap<UWord,Integer> hashMap;
...
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring( 0,astr.length());
}
UWord w = new UWord(astr);
//if(hashMap.containsKey(words[i])){
if(hashMap.containsKey(w)){
//Integer count=(Integer) hashMap.get(words[i]);
//count++;
//hashMap.remove(astr);
//hashMap.put(astr, count);
hashMap.put(w, hashMap.get(w)+1);
}else{
//hashMap.put(astr, 1);
hashMap.put(w, 1);
}
...
String[] words=string.split("[0-9\\.\\@_\\-~#\\,\\:\\;\\(\\)\\[\\]\\%\\&\\’\\‘\\—\\{\\}\\</\\>\\=\\s]+");String key = "<abcd> [1234] \\test";
key=key.replaceAll("<|>|\\\\|\\[|\\]","");
System.out.println(key);
package readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class WordsCount {
HashMap<String,Integer> hashMap;
BufferedReader infile;
String filename = "src/readfile/test3.txt";
String string;
String outpath = "src/readfile/test.txt";
@SuppressWarnings("unchecked")
public WordsCount() throws IOException{
infile = new BufferedReader(new FileReader(filename));
hashMap=new HashMap<String,Integer>();
while((string = infile.readLine()) !=null) {
String[] words=string.split("\\s+");
for(int i=0;i<words.length;i++){
if(words[i].trim().equals("")){
continue;
}
String astr=words[i].trim();
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring( 0,astr.length());
}
if(hashMap.containsKey(words[i])){
Integer count=(Integer) hashMap.get(words[i]);
count++;
hashMap.remove(astr);
hashMap.put(astr, count);
}else{
hashMap.put(astr, 1);
}
}
}
infile.close();
List<String> arrayList=new ArrayList<String>();
Iterator<?> iter = hashMap.entrySet().iterator();
outer:while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
key=key.replaceAll("(?i)","");
key=key.replaceAll("<","");
key=key.replaceAll(",","");
char aChar=key.charAt(0);
for(int i=0;i<arrayList.size();i++){
if(aChar<arrayList.get(i).charAt(0)){
arrayList.add(i,key);
continue outer;
}
}
arrayList.add(key);
}
StringBuffer outContent=new StringBuffer();
for(int i=0;i<arrayList.size();i++){
String key=arrayList.get(i);
outContent.append(key+" "+hashMap.get(key)+"次"+"\r\n");
System.out.println(key+hashMap.get(key));
}
FileOutputStream outs=new FileOutputStream(new File(outpath));
outs.write(outContent.toString().getBytes());
outs.flush();
outs.close();
}
public static void main(String[] args){
try {
new WordsCount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}