Classroom tests - Calculating the longest word chain

The idea is first read from the file, and then writes a word to word array, here's an array operate the. First acquires the first letter of each word and the last letter, then it is determined whether the end to end, the first word is first written into StringBuffer object which, if followed by end to end, and then sequentially written into the object inside, and finally write-targeted into the output file. As for the various stages of judgment, just use the if statement to be judged on the line, the following is a detailed Code:

package test;

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); // read-out characters are appended to the stringbuffer 
            } 
            br.close (); // Close read into the stream 
            String = sb.toString STR () the toLowerCase ();. // will turn stringBuffer and lowercase character 
            String [] = str.split words ( "[^ (the Z-zA-a)] +"); // character to non-word segmentation, all words 
            IF (words.length. 1 == ) 
            { 
                IF ( "" .equals (words [0 ])) 
                System.out.println ( "no word file" );
                 the else 
                    System.out.println ( "one word file" ); 
            } 

            the 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 ( "" ); 
                } 
                
               } 

              T String = yao.toString ();            
               IF ( "Apple" .equals (T)) 
              { 
                  System.out.println ( "no end to end word" ); 
              } 
               File File = new new File ( "D: \\ sophomore under \\ software Engineering output1.txt \\ " );
                 the 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 booleanjudeFileExists (File File) { 

    IF (File.Exists ()) { 
        
        return to true ; 
    } the else { 
        System.out.println ( "file does not exist" );
         // the try {
         //      file.createNewFile ();
        //   } the catch (IOException E) {
        // the TODO Auto-Generated the catch Block
       //       E .printStackTrace ();      
      //    } 
        return  to false ; 
    } 
} 
// determines whether there is a folder (unmodified) 
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();
    }

}
}

 

Guess you like

Origin www.cnblogs.com/qianmo123/p/10991574.html