文件操作--哈利波特

package Harry;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

public class Harry {
    public static <type> void main (String[] args) throws FileNotFoundException {
        File file=new File("C:\\Users\\Administrator\\Desktop\\Harry Potter and the Sorcerer's Stone.txt");                  //读取文件
        if(!file.exists()){
            System.out.println("文件不存在");
            return;
        } 
        int []num=new int[100];
        char []letter=new char[100];
        char a='A';
        char b='a';
        
        for(int i=1;i<=52;i++) {
            if(i<=26) {
                letter[i]=a++;
            }
            else {
                letter[i]=b++;
            }
        }
        
        Scanner scanner=new Scanner(file);
        HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
        while(scanner.hasNextLine()) {
            String line=scanner.nextLine();
            String[] lineWords=line.split(" ");
            for(int i=0;i<lineWords.length;i++) {
                for(int j=0;j<lineWords[i].length();j++) {
                    if(lineWords[i].charAt(j)>='A'&&lineWords[i].charAt(j)<='Z') {
                        num[lineWords[i].charAt(j)-'A'+1]++;
                    }
                    else if(lineWords[i].charAt(j)>='a'&&lineWords[i].charAt(j)<='z') {
                        num[lineWords[i].charAt(j)-'a'+1+24]++;
                    }
                        
                }
            }
                }
        int sum=0;
        for(int i=1;i<=52;i++) {
            sum+=num[i];
        }
        for(int i=1;i<=52;i++) {
            for(int j=i+1;j<=52;j++) {
                if(num[i]<num[j]) {
                    int t=num[i];
                    num[i]=num[j];
                    num[j]=t;
                    
                    char s=letter[i];
                    letter[i]=letter[j];
                    letter[j]=s;
                }
            }
        }
        
        for(int i=1;i<=52;i++) {
            
            double ans=num[i]*1.0/sum;
            System.out.print(letter[i]+":"+num[i]+" ");
            String precent=String.format("%.2f", ans*100);
            System.out.println(precent+" %");
        }
}
    
}
复制代码

 

 

实验二:统计文本中单词出现的次数

复制代码
package Harry;
import java.io.*; 

import java.util.Scanner;

public class danci 
{ 
private static String str="";
private static Scanner sc=new Scanner(System.in);
private static BufferedReader br=null;
private static String a[]=new String[1000000]; 
private static String c[]=new String[10000000]; 
private static int b[]=new int[1000000]; 
private static int l=0;
private static int l1=0;
private static int n=0;
private static int j=0;
//private static Boolean false;
public static void cunfang() throws IOException
{//将单词存到数组a
     
    while(str!=null) 
   {
      int i=0;
      str=str.toLowerCase(); //把大写改成小写
      for(i=0;i<str.length();i++) 
      {
       if((str.charAt(i)>96&&str.charAt(i)<123)) 
       { 
        a[j]=a[j]+str.charAt(i); 
       }
        if(str.charAt(i)==' '||str.charAt(i)==','||str.charAt(i)=='.')
        {
          if(!a[j].equals(""))
          {
             j=j+1;
             a[j]="";
          }
        }

       } 
             str=br.readLine();
     }
           l=j;
            
}
public static void show() 
{//显示
    for(int k=0;k<n;k++) 
    {
     System.out.println(c[k]+"\t"+b[k]);
    }
}
public static void paixu() 
{//排序
   int t3=0;
   int t2=0;
   String sr="";
   for(int i=0;i<l-1;i++)
     {
        t3=i;
        for(int j=i+1;j<l1;j++) 
        {
          if(b[t3]<b[j])
          {
            t3=j;
          }
        }
    if(t3!=i) 
    {
      t2=b[i];
      b[i]=b[t3];
      b[t3]=t2;
      sr=c[i];
      c[i]=c[t3];
      c[t3]=sr;
     }
    }
}
public static void quchong()
{//去重
     for(int k=0;k<l;k++) 
     {
      b[k]=0;
     }
   c[0]=a[0];
   int tt=1;
   Boolean rt=true;

   for(int i=1;i<l;i++)
   {
    rt=false;
    for(int j=0;j<tt;j++)
    {
      if(a[i].equals(c[j])) 
      {
        rt=true;
        break;
      }
     }
      if(!rt) 
      {
       c[tt]=a[i];
       tt++;
      }
     }



     l1=tt;
     for(int i=0;i<l1;i++) 
     {
      for(int j=0;j<l;j++)
      {
        if(c[i].equals(a[j])) 
        {
         b[i]++;
        }
       }
      }
}
public static void Readfile() 
{
    File file=new File("C:\\Users\\Administrator\\Desktop\\Harry Potter and the Sorcerer's Stone.txt"); 
    try 
    {
      InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//InputStreamReader实现从字节流到字符流的转换
      br=new BufferedReader(read);
      str=br.readLine();
      cunfang();
      br.close();
      read.close();
     }
    catch(IOException e) 
    {
      
      e.printStackTrace();
    }
}

public static void Writefile() throws IOException
{
     File file=new File("C:\\Users\\Administrator\\Desktop\\Harry Potter and the Sorcerer's Stone.txt");
       if(!file.exists())
       file.createNewFile();
       FileWriter write = new FileWriter(file,true);
      BufferedWriter out=new BufferedWriter(write);
      for(int i=0;i<n;i++)
      {
      StringBuffer sb=new StringBuffer();
      out.write("这是第"+(i+1)+"个: ");
      out.write(c[i]+"\t"+b[i]);
      out.write("\r\n");
      } 
      out.close();
}
public static void main(String[] args) throws IOException 
{
     System.out.println("请输入您要统计前几个最常出现的单词:");
     n=sc.nextInt();
     a[0]="";
     Readfile(); 
     quchong();
    paixu();
     show(); 
     Writefile();

} 

}
复制代码

实验三: 将.txt文档放进目录中,进行递归查找;先查找目录是否存在,将.txt文档存放进数组中;依次递归调用,输出相同单词的个数;

复制代码
package Ioliu;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import Ioliu.*;

public class wenjianjia 
{
    public static List<String> getAllFilePaths( String filePath, List<String> filePathList ) {
        File[] files = new File( filePath ).listFiles();
        if ( files == null ) {
            return filePathList;
        }
        for ( File file : files ) {
            if ( file.isDirectory() ) {
                filePathList.add( file.getPath() + "注:此处为文件夹" );
                getAllFilePaths( file.getAbsolutePath(), filePathList );
            } else {
                filePathList.add( file.getPath() );
            }
        }
        return filePathList;
    }


    public static void main(String[] args) throws IOException 
    {    
     
        piao p=new piao();
         File file=null;
         List<String> filePaths = new ArrayList<>();
         filePaths = getAllFilePaths( "D:\\\\大二java\\\\halibote", filePaths );
         for ( String path : filePaths ) {
              System.out.println( path );
              file=new File(path);
              p.Readfile(file);
              p.quchong();
              p.paixu();
              p.show();
              
         }

    }
}
posted @ 2019-10-10 22:32  Mr_WildFire  阅读(187)  评论(0编辑  收藏  举报