个人感触:程序=数据结构+算法。好的数据结构和算法对于程序的性能是不言而喻的。
其余类见附件
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 说明:当实现自定义容器时,千万不要忘记在进行读写操作时,使用serializable接口
- * */
- public class WordCount {
- public static void writeMap(File Origin,File newFile){
- Map<String,Integer> map=getWordCount(Origin);
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(map);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println(map.toString());
- }
- public static Map<String,Integer> getWordCount(File f){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(f);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- MapWords mapword=new MapWords();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- Nodes node=new Nodes(m);
- mapword.addNewNodeElement(m, f.getName(), i);
- }
- Map<String,Integer> map=new HashMap<String,Integer>();
- for(int i=0;i<mapword.getMapSize()-1;i++){
- String Keyword=mapword.toGetKeyByIndex(i+1);
- Nodes node=mapword.getNode(Keyword);
- map.put(Keyword,node.getWordCountAll());
- }
- return map;
- }
- public static void writerMapIndex(File Origin,File newFile){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(Origin);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- MapWords mapword=new MapWords();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- // Nodes node=new Nodes(m);
- // List<String> list=new ArrayList<String>();
- // list=node.getDocNameAll();
- // seriMapArray[i]=new seriMap(m,list);
- mapword.addNewNodeElement(m, Origin.getName(), i);
- }
- // Set<Nodes> setArray=mapword.toGetValueSet();
- // Nodes nodeArray[]=new Nodes[setArray.size()];
- // Nodes node[]=setArray.toArray(nodeArray);
- // System.out.println("node[].length="+node.length);
- // System.out.println("Arrays.toString(node)="+Arrays.toString(node));
- // for(int i=1;i<node.length;i++){
- // System.out.println("i="+i);
- // seriMapArray[i-1]=new seriMap(node[i].getKeyword(),node[i].getDocNameAll());
- // }
- //
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(mapword);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void writerMapIndex(File[] Origin,File newFile){
- MapWords mapword=new MapWords();
- for(int n=0;n<Origin.length;n++){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(Origin[n]);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- mapword.addNewNodeElement(m, Origin[n].getName(), i);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(mapword);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void FileProcessAll(File[] files){
- File[] newFile=new File[files.length];
- String uri=files[0].getAbsolutePath();
- int m=uri.lastIndexOf(File.separator)+1;
- String newUrl=uri.substring(0, m);
- File f=new File(newUrl+"temp");
- if(f.exists()==false){
- f.mkdir();
- }
- for(int i=0;i<files.length;i++){
- newFile[i]=new File(f.getAbsoluteFile()+File.separator+files[i].getName());
- }
- for(int i=0;i<files.length;i++){
- FileProcess.processFile(files[i], newFile[i]);
- }
- }
- public static void getWordCountAll(File[] files,File newFile){
-
- MapWords mapword=new MapWords();
-
- for(int j=0;j<files.length;j++){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(files[j]);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- Nodes node=new Nodes(m);
- mapword.addNewNodeElement(m, files[j].getName(), i);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- Map<String,Integer> map=new HashMap<String,Integer>();
- for(int i=0;i<mapword.getMapSize()-1;i++){
- String Keyword=mapword.toGetKeyByIndex(i+1);
- Nodes node=mapword.getNode(Keyword);
- map.put(Keyword,node.getWordCountAll());
- }
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(map);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println(map.toString());
- System.out.println(map.size());
- }
- }



雷达卡



京公网安备 11010802022788号







