• Java文件与流测试


    import java.io.File;  
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.io.BufferedReader;  
    import java.io.BufferedWriter;  
    import java.io.FileInputStream;
    import java.util.Arrays;
    import java.util.List;
    import java.util.StringTokenizer;
    import java.io.FileWriter;  
      
    public class titletwo {  
        
        
        public static void main(String args[]) {  
        
            int totalnum=0;    
            String ha;
            int length;
            int n;
            char ba;
            char letter[]=new char[60];
            int sum[]=new int[60];
            int num=0;
            String chuan="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            letter=chuan.toCharArray();
             try {     
                 String pathname = "C:\Users\26624\Desktop\Harry Potter and the Sorcerer's Stone.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径  
                 File file = new File(pathname);   
                 InputStreamReader reader = new InputStreamReader(  
                         new FileInputStream(file));
                 BufferedReader br = new BufferedReader(reader);  
                 String line = "";  
                 line = br.readLine();  
                 while (line != null) {  
                     
                     StringTokenizer str = new StringTokenizer(line, ",,., ,!,?,“,”,‘,-,'");
                      
                     while (str.hasMoreElements()) {
                         ha=String.valueOf(str.nextElement());
                         length=ha.length();
                         n=0;
                         while(true)
                         {
                             ba=ha.charAt(n++);
                             switch(ba)
                             {
                             case 'a' : sum[0]++;break;
                             case 'b' : sum[1]++;break;
                             case 'c' : sum[2]++;break;
                             case 'd' : sum[3]++;break;
                             case 'e' : sum[4]++;break;
                             case 'f' : sum[5]++;break;
                             case 'g' : sum[6]++;break;
                             case 'h' : sum[7]++;break;
                             case 'i' : sum[8]++;break;
                             case 'j' : sum[9]++;break;
                             case 'k' : sum[10]++;break;
                             case 'l' : sum[11]++;break;
                             case 'm' : sum[12]++;break;
                             case 'n' :sum[13]++;break;
                             case 'o' :sum[14]++;break;
                             case 'p' :sum[15]++;break;
                             case 'q' :sum[16]++;break;
                             case 'r' :sum[17]++;break;
                             case 's' :sum[18]++;break;
                             case 't' :sum[19]++;break;
                             case 'u' :sum[20]++;break;
                             case 'v' :sum[21]++;break;
                             case 'w' :sum[22]++;break;
                             case 'x' :sum[23]++;break;
                             case 'y' :sum[24]++;break;
                             case 'z' :sum[25]++;break;
                         
                             case 'A' : sum[26]++;break;
                             case 'B' : sum[27]++;break;
                             case 'C' : sum[28]++;break;
                             case 'D' : sum[29]++;break;
                             case 'E' : sum[30]++;break;
                             case 'F' : sum[31]++;break;
                             case 'G' : sum[32]++;break;
                             case 'H' : sum[33]++;break;
                             case 'I' : sum[34]++;break;
                             case 'J' : sum[35]++;break;
                             case 'K' : sum[36]++;break;
                             case 'L' : sum[37]++;break;
                             case 'M' : sum[38]++;break;
                             case 'N' :sum[39]++;break;
                             case 'O' :sum[40]++;break;
                             case 'P' :sum[41]++;break;
                             case 'Q' :sum[42]++;break;
                             case 'R' :sum[43]++;break;
                             case 'S' :sum[44]++;break;
                             case 'T' :sum[45]++;break;
                             case 'U' :sum[46]++;break;
                             case 'V' :sum[47]++;break;
                             case 'W' :sum[48]++;break;
                             case 'X' :sum[49]++;break;
                             case 'Y' :sum[50]++;break;
                             case 'Z' :sum[51]++;break;
                             
                             }
                             totalnum++;
                             length--;
                             if(length<=0)
                             {
                                 break;
                             }
                         }
                         
                     }
                    
                     line = br.readLine(); // 一次读入一行数据 
                 }                
             } catch (Exception e) {  
                 e.printStackTrace();  
             }
             int ma,b;
             char a;
             for(int i=0;i<51;i++)
             {
                 ma=sum[i];
                 b=i;
                 a=letter[i];
                 for(int j=i+1;j<52;j++)
                 {
                     if(ma>=sum[j])
                     {
                         ma=sum[j];
                         b=j;
                     }
                 }
                 ma=sum[i];
                 sum[i]=sum[b];
                 sum[b]=ma;
                 letter[i]=letter[b];
                 letter[b]=a;
             }         
             for(int i=51;i>=0;i--)
             {
                 double what=(double)sum[i]/totalnum*100;
                 BigDecimal bg=new BigDecimal(what);
                 double f1=bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                 System.out.println(letter[i]+"  "+f1+"%");
             }                                   
        }
      
    

    功能二、三:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.nio.file.DirectoryStream;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class titlesix {
        public static void haha(String road)
        {
            int m=0;
            int n=0;
            int flag;       
            String word[]=new String[10000];
            int sum[]=new int[10000];
            String ha[]=new String[10000];
            List<String> list;
            String w;
            int totalnum=0;
            int i=0;
            
            try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw  
      
                /* 读入TXT文件 */  
                String pathname = road; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径  
                File filename = new File(pathname); // 要读取以上路径的input。txt文件  
                InputStreamReader reader = new InputStreamReader(  
                        new FileInputStream(filename));
                BufferedReader br = new BufferedReader(reader);  
                String line = "";  
                line = br.readLine();  
                while (line != null) {  
                                   
                    StringTokenizer st2 = new StringTokenizer(line, " ,-");
                    i=0;
                    while (st2.hasMoreElements()) {
                        ha[i]=String.valueOf(st2.nextElement());                   
                        ha[i]=ha[i].toLowerCase();                
                        m++;
                        list = Arrays.asList(word);
                        flag = list.indexOf(ha[i]);
                        if(flag==-1)
                        {
                            word[n]=ha[i];
                            sum[n]++;
                            n++;
                            totalnum++;
                        }
                        else
                        {
                            sum[flag]++;
                            totalnum++;
                        }                    
                        i++;
                    }               
                    line = br.readLine(); // 一次读入一行数据 
                }               
            } catch (Exception e) {  
                e.printStackTrace();  
            }      
            int ma=0;
            int biao=0;
            for(int j=0;j<n-1;j++)
            {
                ma=sum[j];
                biao=j;
                w=word[j];
                for(int k=j+1;k<n;k++)
                {
                    if(ma<sum[k])
                    {
                        ma=sum[k];
                        biao=k;
                    }
                }
                sum[biao]=sum[j];
                sum[j]=ma;
                word[j]=word[biao];
                word[biao]=w;
            }      
            System.out.println("请输入前N名单词的个数:");
            Scanner shuru=new Scanner(System.in);
            int mmm;
            mmm=shuru.nextInt();
            for(int j=0;j<mmm;j++)
            {
                double what=(double)sum[j]/totalnum*100;
                BigDecimal bg=new BigDecimal(what);
                double f1=bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                System.out.println(word[j]+"  "+f1+"%");
                
            }
        }
        public  int way(String dao)
        {
            int length;
            int flag=1;
            Path path=Paths.get(dao);
            try
            {
                DirectoryStream<Path> children=Files.newDirectoryStream(path);
                for(Path child:children)
                {
                    flag=1;
                    String lujing;
                    lujing=child.toString();
                    length=lujing.length();
                    if(length==0)
                    {
                        return 0;
                    }
                    char sta[]=new char[1000];
                    sta=lujing.toCharArray();
                    for(int i=0;i<length;i++)
                    {
                        if(sta[i]=='\')
                            {
                                    sta[i]='/';
                            }
                        if(sta[i]=='.')
                        {
                            
                            flag=0;
                        }
                    }
                    lujing=String.valueOf(sta);
                    System.out.println(lujing);
                    if(flag==1)
                    {
                        way(lujing);
                    }
                    else
                    {
                        titlefive.haha(lujing);                   
                    }                               
                }
            }catch(Exception e){}
            return 0;
        }
        public static void main(String[] args) {
            {
                titlesix sta=new titlesix();
                sta.way("D:/aaa");           
            }
        }
    
    }
  • 相关阅读:
    在光驱按钮不好使用时如何关闭光驱门
    将24位及32位图像数据保存到位图中
    VIM代码自动完成
    加载TLBB场景(一)
    创建异形窗口
    Windows快速关机与重启
    Direct3D9应用CEGUI
    利用GDI+加载图片
    ubuntu MP3乱码解决
    asp.net动态生成控件及访问控件
  • 原文地址:https://www.cnblogs.com/vvxvv/p/11807500.html
Copyright © 2020-2023  润新知