如何编程编程实现:在一个文本文件中,查找某句话是否存在?

解决方案 »

  1.   

    用ArrayList的contains方法可以的
    还有别的思路吗?
      

  2.   

    首先,文件必须要读出来。
    匹配的话,我看还是用lucence吧。
    自己写有点复杂咯。
      

  3.   

    试试这个吧!
    public static void main(String[]args){
    File file=new File("F:\\test.txt");
    try {
    //       
          //使用文件对象创建文件输入流对象,相当于打开文件
          FileInputStream fis = new FileInputStream(file);
          for (int i = 0; i < file.length(); i++) {
            char ch = (char)(fis.read());  //循环读取字符
            System.out.print(ch);
          }
          System.out.println();
          fis.close();     //关闭流
        } catch (FileNotFoundException fnfe) {
          System.out.println("文件打开失败。");
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
    }
      

  4.   

    public static void main(String[]args){
    File file=new File("F:\\test.txt");
    try {
    //       File file = new File("test.txt");  //创建文件对象
          FileInputStream fis = new FileInputStream(file);
          //根据文件的字节长度创建字节数组
          byte[] buf = new byte[(int)(file.length())];
          fis.read(buf);  //读取文件中的数据存放到字节数组中
          String str = new String(buf);  //利用字节数组创建字符串
          System.out.println(str);   //打印字符串
          fis.close();     //关闭流
        } catch (FileNotFoundException fnfe) {
          System.out.println("文件打开失败。");
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
    }
      

  5.   

    肯定得读出来,读出来之后放到数组还是List就都差不多啦!
      

  6.   

    用indexOf最快了。或者用正则匹配。
      

  7.   

    先用流的形式读取成二进制数组。
    byte[] array = new byte[file.length()];
    stream.read(array , 0 , array.length);String s = new String(array);//这一步注意编码,否则会有乱码
    //String s = new String(array,"[encoding]");
    if(s.indexOf(temp) != -1){//temp是需要查找的字串
       //说明存在字串temp,doSomeThing
    }
      

  8.   

    1: BufferedReader#readLine
    2: String#indexOf
      

  9.   

    public static void main(String[] args) { //读取data1.txt到数组a中
            String FilePath = "F:/data/data1.txt";
            String FilePath2 = "F:/data/data2.txt";
            FileReader fr = null;
            BufferedReader br = null;
            ArrayList<String> data1=new ArrayList<String>();
            ArrayList<String> data2=new ArrayList<String>();
            String[] sp=null;
            data1=getElements(FilePath, fr, br, data1);
            data2=getElements(FilePath2, fr, br, data2); 
            
            for(String a:data1){
            
              if(!data2.contains(a)){
              System.out.println("不包含的对象:"+a);
              }
            
            }
            
             
            
    }//main() private static ArrayList<String> getElements(String FilePath, FileReader fr, BufferedReader br, ArrayList<String> data1) {
    String[] sp;
    try
            {
                try
                {
                    fr = new FileReader(FilePath);// 建立FileReader对象,并实例化为fr
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                }
                br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为br
                String Line = br.readLine();// 从文件读取一行字符串
                while (Line != null) // 判断读取到的字符串是否不为空
                {
                 sp=Line.split("\\s{1,}");
                 for(String s:sp){
                        data1.add(s);            
                 }
                    Line = br.readLine();// 从文件中继续读取一行数据
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    if (br != null)
                        br.close();// 关闭BufferedReader对象
                    if (fr != null)
                        fr.close();// 关闭文件
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            
            // 输出读取的结果
    /*        for(String a:data1){
             System.out.println("a:"+a);
            }*/
            
            return data1; }