zoukankan      html  css  js  c++  java
  • 课堂练习之“寻找最长单词链”

    package test;
    //20173522 李秦
    import java.applet.Applet;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Lian2 {
    
        public static void main(String[] args) throws IOException {
            // TODO 自动生成的方法存根
    
            String filename ="D:\大二下\软件工程\input.txt";
            File  a=new  File(filename);
         //judeFileExists(a);
         if(judeFileExists(a))
            {
                danci(filename);
            }
         else
            {
             System.out.println("文件不存在");
            }
             
            
        }
    
        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)]+"); // 非单词的字符来分割,得到所有单词
                if(words.length==1)
                {
                    if("".equals(words[0]))
                    System.out.println("文件中无单词");
                    else
                        System.out.println("文件中只有一个单词");
                }
    
                else
                {
                    StringBuffer yao = new StringBuffer();
                    String b1=words[0];
                    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(" ");
                    }
                    
                   }
    
                  String t=yao.toString();            
                  if("apple ".equals(t))
                  {
                      System.out.println("没有首尾相连的单词");
                  }
                   File file =new File("D:\大二下\软件工程\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()) {
            
            return true;
        } else {
            System.out.println("文件不存在");
            // try {
            //     file.createNewFile();
           //  } catch (IOException e) {
           // TODO Auto-generated catch block
          //      e.printStackTrace();      
         //   }
            return false;
        }
    }
    // 判断文件夹是否存在(未修改)
    public static void judeDirExists(File file) {
    
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdir();
        }
    
    }
    }
  • 相关阅读:
    虚拟内存思想
    虚拟内存映射 段分割 vm_area_struct
    进程、内存的理想与现实 VS 虚拟内存
    进程地址空间
    MMU CPU及思想
    链接器和加载器 好书
    编译器 链接器 加载器
    链接器简介
    C编译器、链接器、加载器详解
    静态库是.o文件的集合与弱符号
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/10994730.html
Copyright © 2011-2022 走看看