Stringint indexOf(String str) 
          Returns the index within this string of the first occurrence of the specified substring. 

解决方案 »

  1.   

    String filename = new String();
    FileInputStream in = null;
    in = new FileInputStream(path+"/"+name);
    int num = in.available();
    byte[] buffer = new byte[num];
    in.read(buffer);
    in.close();
    dessert = new String(buffer,0);
    int bein = 0;
    bein = dessert.indexOf("所要查询的字串");
    /*所以bein就是字符串所在的位置,如果是-1,就说明不包含该字串*/
      

  2.   

    public static void Find() throws Exception {
      
               BufferedReader bj=new BufferedReader(new InputStreamReader(System.in));                System.out.print("\nPlease input the name of file:>");                String FileName=bj.readLine().trim();                BufferedReader in=new BufferedReader(new FileReader(FileName));
                    
                    System.out.print("\nPlease input the String you want to locate:>");
                    
                    //enter the string that the users want to find;
                    String FindLine=bj.readLine();
                    
             String Line;
            
             int flat=0, SumLine=0;
            
             //read each line in file;
             Line=in.readLine();
            
               //if this file is empty,then display some information; then go to next loop; menu choice
               if (Line==null) {
            
             System.out.print("\nSorry,The string you want to find doesn't exist!"); 
            
            
               }
               
               else {
               
               //if Line isn't equal to null ,add one to SumLine;
               SumLine++;
            
               //find the column location of string;
               flat=Line.indexOf(FindLine);
               
               
                 
                 while(true){
               
                 //if find string in current line, then display some information;then go out of while loop
                 if (flat!=-1) {
                            
                           System.out.print("\nThe string you want to find is located in "+SumLine+ " Line, "
                               
                                            +(flat+1)+" Column.");
            
                    break;
                                 
                           } 
                              
                         //if do not find string in current line,the program will continuously go to next line,
                         //and if "Line"=null, we can conclude the process has already arrived the last Line. 
                         else  {
                                  
                                Line=in.readLine(); 
                                                                       
                 if (Line==null) {
            
                    System.out.print("\nSorry,The string you want to find doesn't exist!"); 
                                 
                                   //can't find. so quit the loop to go to the first menu loop;
                                   break;
                                
                                   } 
                                 
                                //if do not find string. pass the each Line ,the SumLine will be added itself;
                                SumLine++;
            
                 //find the column location of string repeatly;
                                flat=Line.indexOf(FindLine);
                              
                              }
                   
                      }
            
              }        
            
         }