• 文件与流课堂实践


    实验一:读取英文文件,顺序输出字母个数及百分比(区分大小写)

    package text1;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.TreeMap;
    import java.util.TreeSet;
    public class lianxi{
        public static void main(String [] args) throws IOException {
            BufferedReader br=new BufferedReader(new FileReader("E:\java\Harry Potter and the Sorcerer's Stone.txt"));        
            TreeMap<Character,Integer> hm=new TreeMap<>();
            int  bb;
            while((bb=br.read())!=-1) {    
                 if((bb>='A'&&bb<='Z')||(bb>'a'&&bb<='z')) {
                     hm.put((char)bb,hm.containsKey((char)bb)?hm.get((char)bb)+1:1);
                }   
                }    
            br.close();
            int max=0;
            int sum=0;
            int t=0;
            for(Character k: hm.keySet()) {
                sum=sum+hm.get(k);
              }
            TreeSet<Character> ts=new TreeSet<>(new Comparator<Character>()
            {
                public int compare(Character a,Character b) {
                    int num=hm.get(a)-hm.get(b);
                    return num==0?1:(-num);
                }
            });
            for(Character k: hm.keySet()) {
                ts.add(k);
            }
            DecimalFormat df = new DecimalFormat("0.00%");
            for (Character c : ts)
            {
                float bai=(float)hm.get(c)/sum;
                System.out.println(c+" "+hm.get(c)+" "+df.format(bai));
            }
           
            System.out.println(sum);
    }}

    实验二:读取英文文件,顺序输出单词出现次数(不分大小写)

    package text2;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    public class ketang {
        class Word                                        //定义单词类
        {
            String value;                                  //具体的单词
            int geshu;                                     //出现的个数
            Word next;                                     //将单词链起来
            public Word(String value,int geshu)            //带参构造函数
            {
                this.value=value;
                this.geshu=geshu;
                next=null;
            }
            public Word()                                   //空构造函数
            {
                this.value="";
                this.geshu=0;
                next=null;
            }
        }    
        // 读取指定路径下的文件名和目录名
        public void getFileList() throws IOException {
            BufferedReader br=new BufferedReader(new FileReader("E:\java\Harry Potter and the Sorcerer's Stone.txt"));        
            TreeMap<Character,Integer> hm=new TreeMap<>();
            int  bb;
            while((bb=br.read())!=-1) {    
                        Word word=new Word(); //单词的链头
                        Word lian,xin;                                             
                        String str=""; //读取英文文件
                        char[] c=new char[1];       //每次读取一个字母
                        int b=0;
                        boolean exist=false;       //判断单词是否存在于  word 链中
                        while((b=br.read(c))!=-1)        //每次读取一个字母直到最后
                        {
                            //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
                            if(String.valueOf(c).equals("
    ")||String.valueOf(c).equals("
    ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals(""")||String.valueOf(c).equals("'"))
                            {
                                lian=word;
                                while(lian!=null)            
                                {
                                    if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
                                    {
                                        lian.geshu++;exist=true;
                                        break;
                                    }
                                    else
                                    {
                                        lian=lian.next;
                                    }
                                }
                                if(exist==false)                        //如果不存在,则在单词链中添加
                                {
                                    xin=new Word(str,1);
                                    xin.next=word.next;
                                    word.next=xin;
                                    str="";
                                }
                                else
                                {
                                    exist=false;
                                    str="";
                                }
                            }
                            else                                      //单词
                            {
                                str+=String.valueOf(c);
                            }
                        }
        System.out.print("请输入您想查询的前几个出现此处最多的单词:");
                        Scanner scan=new Scanner(System.in);
                        int N=scan.nextInt();
                        for(int i=1;i<=N;i++)                   
                        {
                            xin=new Word("",0);
                            lian=word.next;
                            //找到单词链中个数最多的
                            while(lian!=null)
                            {
                                if(lian.geshu>xin.geshu)
                                {
                                    xin=lian;
                                }
                                lian=lian.next;
                            }
                            //输出单词链中个数最多的
                            System.out.println("第"+i+"个 :"+xin.value+"  个数:"+xin.geshu);
                            lian=word;
                            //删除单词链中单词个数最多的
                            while(lian.next!=null)
                            {
                                if(lian.next.value.equalsIgnoreCase(xin.value))
                                {
                                    lian.next=lian.next.next;
                                    break;
                                }
                                lian=lian.next;
                            }
                        } 
                    }            
                }         
        public static void main(String[] args) throws IOException {
            ketang rf = new ketang();
            rf.getFileList();
        }
    }

    实验三:遍历文件夹下所有文件

    import java.io.File;
    public class main {
        public static void main(String[] args) {
            String path = "E:\JAVA";        //要遍历的路径
            File file = new File(path);        //获取其file对象
            File[] fs = file.listFiles();    //遍历path下的文件和目录,放在File数组中
            for(File f:fs){                    //遍历File[]数组
                if(!f.isDirectory())        //判断文件是否为目录
                    System.out.println(f);
            }
        }
    }
  • 相关阅读:
    不依赖OFFICE组件实现带图片的EXCEL导出
    关于安装SQL的错误
    Web开发学习点滴(持续更新)
    窗体间传值汇总
    沈阳.NET俱乐部MSN群开张了。。。
    VoIP协议标准浅析
    名词解释
    创建和运行一个解决方案
    400电话为什么叫免费电话?
    XPO基础简化的属性语法:
  • 原文地址:https://www.cnblogs.com/fengjingfei/p/11809344.html
Copyright © 2020-2023  润新知