单词接龙

package 查找英语单词;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Find_wordlist {

    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
         System.out.println("请输入文本文件");
         Scanner sc=new Scanner (System.in);
         String str=sc.nextLine();
        
        File  a=new  File(str);
        sc.close();
     //judeFileExists(a);
     if(judeFileExists(a))
        {
            wordlist(str);
        }
                 
    }

    public static void wordlist(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)]+"); // 非单词的字符来分割,得到所有单词
            StringBuffer yao = new StringBuffer();
            String b1=words[0];
            if(words.length==1) {
                System.out.println("只有一个单词");
            }
            
            else {
            yao.append(b1);
            yao.append(" ");
            //System.out.println(b1);
            String end=b1.substring(b1.length()-1,b1.length());
            //System.out.println(end);
           for(int i=1;i<words.length;i++)
           {  
            String start=words[i].substring(0,1);
            if(end.equals(start))
            {
                end=words[i].substring(words[i].length()-1,words[i].length());
                yao.append(words[i]);
                yao.append(" ");
            }
           }
           
          // for( String a:words)
          // {
             //  System.out.println(a);
         //   }
         //  System.out.println(yao.toString()); 
           File file =new File("f:\\output1.txt");
            try {
                 file.createNewFile();
            } catch (IOException e) {
               e.printStackTrace();      
           }
          
            try {
                
                  FileWriter fw =new FileWriter(file);
                  fw.write(yao.toString());
                  fw.flush();
                  fw.close();
            }
            catch (IOException e) {
                   e.printStackTrace();      
               }
         
    }

}
// 判断文件是否存在
public static boolean judeFileExists(File file) {

    if (file.exists()) {
        System.out.println("文件已读取");
        if(file.length() == 0) {
            System.out.println("文件为空");
            return false;
        }
        return true;
    }
//    if(file.length() == 0) {
//        System.out.println("文件为空");
//        return false;
//    }if(file.length() ==1) {
//        System.out.println("只有一个单词");
//        return false;
//    }
        else {
        System.out.println("未能读取到文件");
        return false;
    }
}

}

 

posted @ 2019-06-06 22:33  birdmmxx  阅读(230)  评论(0编辑  收藏  举报