qwb0614

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  187 随笔 :: 0 文章 :: 0 评论 :: 2048 阅读
复制代码
public class Test1 {

    public static void main(String[] args) throws IOException
    {
        // TODO 自动生成的方法存根
        String filename ="E:\\123.txt";
        File  a=new  File(filename);
        File  file=new  File("E:\\456.txt");
        long startTime = System.currentTimeMillis();
        //judeFileExists(a);
        if(judeFileExists(a))
        {
            danci(filename);
        }
        else
        {
            try {

                FileWriter fw =new FileWriter(file);
                fw.write("无此文件");
                fw.flush();
                fw.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间
    }

    public static void danci(String s) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(s));
        StringBuffer sb = new StringBuffer();
        String text = null;
        while ((text = br.readLine()) != null) {
            sb.append(text);// 将读取出的字符追加到stringbuffer中
        }
        br.close(); // 关闭读入流
        String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
        String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
        for (int i = 0; i < words.length; i++) {
            System.out.print(words[i]+" ");
            if ((i+1)%20==0)
                System.out.println();
        }
        String end;
        String start;
        int num=0;
        StringBuffer lainjie1 = new StringBuffer();
        int S=0,E=0;
        String b1=words[0];
        if(b1.equals(""))
        {
            System.out.println("文件为空");
            lainjie1.append("文件为空");
        }
        else
        {
            if(words.length==1)
            {
                System.out.println("只有一个单词");
                return;
            }


            for(int i=0;i<words.length-1;i++)
            {
                int num1=0;
                StringBuffer lainjie = new StringBuffer();
                boolean jued=false;
                int s1=0,e1=0;
                for (int j = i+1; j < words.length; j++) {
                    end=words[j-1].substring(words[j-1].length()-1);
                    start=words[j].substring(0,1);
                    if(end.equals(start))
                    {
                        if(jued==false)
                        {
                            lainjie.append(words[j-1]);
                            lainjie.append(" ");
                            num1++;
                            jued=true;
                            s1=j;
                        }

                        lainjie.append(words[j]);
                        lainjie.append(" ");
                        num1++;
                    }
                    else
                    {

                        e1=j;
                        break;
                    }
                }
                if(num1>num)
                {
                    num=num1;
                    lainjie1=lainjie;
                    S=s1;
                    E=e1;
                }
            }
            if(lainjie1.toString().equals("")) {lainjie1.append("无互联语句");}


            System.out.println("---------------------------------------");
            System.out.println(lainjie1.toString());
            System.out.println(num);
            System.out.println(S);
            System.out.println(E);
        }
        
        File file =new File("E:\\456.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            FileWriter fw =new FileWriter(file);
            fw.write(lainjie1.toString());
            fw.flush();
            fw.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static boolean judeFileExists(File file) {

        if (file.exists()) {
            System.out.println("文件存在");
            return true;
        } else {
            System.out.println("文件不存在");
           
            return false;
        }
    }
}
复制代码

1

posted on   嘎嘎鸭1  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示