解决方案 »

  1.   

    用IO读的话 最好用带readline()那个方法的 前提是你的字符串木有换行符
      

  2.   

    这是我在某本面试宝典上面看到的写法:public static void getThing(String filename,String str) throws Exception
    {
    int count=0;
    Reader rd = new FileReader(filename);
    int c;
    while((c=rd.read())!=-1)
    {
    while(c==str.charAt(0))//我感觉就是这里错了,但是不知道这里应该怎么判断
    {
    for (int i = 0; i < str.length(); i++) {
    c=rd.read();
    if(c!=str.charAt(i))
    {
    break;
    }
    if(i==str.length()-1)
    {
    count++;
    }
    }
    }
    }
    System.out.println("count="+count);
    }
    但是不对,显示不出来效果!
      

  3.   


    public int occurTimes(String s, File f) {
    int times=0,index=0;
    try {
    FileInputStream fis = new FileInputStream(f);
    byte[] bs = new byte[(int)f.length()];
    fis.read(bs);
    String sfile = new String(bs); // 根据文件字符编码,这里可能需要相应调整
    while ((index=sfile.indexOf(s,index)) >= 0) {
    index += s.length();
    times++;
    }
    } catch (Exception e) {
    e.printStackTrace(System.out);
    }
    return times;
    }
      

  4.   

    如果不管读文件,只比较字符串,那代码就更简单了
    public int occurTimes(String s, String bigs) {
    int times=0, index=0;
    while ((index=bigs.indexOf(s, index)) >= 0) {
    index += s.length();
    times++;
    }
    return times;
    }
      

  5.   

    8F正解,和下面我经常用的commons API如出一辙(除了最开始的空的判断)
        /**
         * <p>Counts how many times the substring appears in the larger String.</p>
         *
         * <p>A <code>null</code> or empty ("") String input returns <code>0</code>.</p>
         *
         * <pre>
         * StringUtils.countMatches(null, *)       = 0
         * StringUtils.countMatches("", *)         = 0
         * StringUtils.countMatches("abba", null)  = 0
         * StringUtils.countMatches("abba", "")    = 0
         * StringUtils.countMatches("abba", "a")   = 2
         * StringUtils.countMatches("abba", "ab")  = 1
         * StringUtils.countMatches("abba", "xxx") = 0
         * </pre>
         *
         * @param str  the String to check, may be null
         * @param sub  the substring to count, may be null
         * @return the number of occurrences, 0 if either String is <code>null</code>
         */
        public static int countMatches(String str, String sub) {
            if (isEmpty(str) || isEmpty(sub)) {
                return 0;
            }
            int count = 0;
            int idx = 0;
            while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
                count++;
                idx += sub.length();
            }
            return count;
        }
    org.apache.commons.lang.StringUtils
      

  6.   

    while(c==str.charAt(0))//我感觉就是这里错了,但是不知道这里应该怎么判断
    把while改成if
    这是最原始的比较方法,现判断第一个字符是否相同,
    然后for (int i = 0; i < str.length(); i++) { //安长度循环判断依次判断后面的字符是否相同
    这里的循环有点问题,应该是i=1位置开始,因为i=0在之前说的先比较第一个字符已经判断过了
      

  7.   

      public static void main(String[] args) throws Exception {
        String sub = "abc";
        String[] tests = new String[] {
            "abcabc",
            "1234abcaabc1234",
            "1234abcaabc1234ab1234abcaabc1234",
            "1234abcaabc1234ab1234abcaabc1234a"
        };
        for (String test : tests) {
          BufferedReader reader = new BufferedReader(new StringReader(test));
          System.out.print(count(reader, sub));
          System.out.println(StringUtils.countMatches(test, sub));
          reader.close();
        }
      }  // Must be BufferedReader, because the  method is not supported in FileReader.
      static int count(BufferedReader reader, String substring) throws Exception {
        if (substring == null || substring.length() == 0) {
          return 0;
        }
        char[] chars = substring.toCharArray();
        char[] buff = new char[substring.length()];
        char firstChar = substring.charAt(0);
        buff[0] = firstChar;
        int count = 0;
        int read;
        while ((read = reader.read()) != -1) {
          char c = (char) read;
          if (c == firstChar) {
            reader.(substring.length());
            int len = reader.read(buff, 1, buff.length - 1);
            if (len != buff.length - 1) {
              return count;
            }
            if (Arrays.equals(chars, buff)) {
              count++;
            } else {
              reader.reset();
            }
          }
        }
        return count;
      }
      

  8.   

    可以考虑用BufferedReader的&reset方法,FileReader不支持,但是BufferedReader支持
      

  9.   

    这一行好像有错:System.out.println(StringUtils.countMatches(test, sub));
    StringUtils是这个包下面的吗?com.sun.xml.internal.ws.util.StringUtils;