先读取test.txt文件内容,将其保存成一个字符串,然后根据hello查找即可

解决方案 »

  1.   

    这个知道了,我查一下API吧,有代码的可以写来参考一下.
      

  2.   

    例如文件里有:hello hello hello hello hello a a
    而我只想打印hello hello hello hello hello,怎么实现?
      

  3.   


    public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("c:\test.txt"));
    String aa = br.readLine();
    String[] bb = aa.split("hello#hello");
    for(String s : bb){
      System.out.println(s);
    }
    }
      

  4.   

    以上有误
    应为aa.split("hello");
    从得到的数组个数考虑一下
      

  5.   

    smartpoko,首先谢谢你。你给的代码有两个错误。第一:new FileReader("c:\test.txt"),应该是c:\\test.txt
    第2:应为aa.split("hello"); 从得到的数组个数考虑一下。应该是aa.split("a");
    但又有个问题了。如果我不知道文本里的内容,但我只知道里面肯定有"hello",我怎么样才能单独取出呢?
      

  6.   

    读取文件后 对每读取的一行 split(" ") 或者用正则的 \b 单词边界匹配 得到每行中的 所有单词数组
    然后判断是否与hello匹配
      

  7.   


    你的想法是可以,但如果有这样的情况呢?hello hell o hello a b c 这里面就2个hello,按你的意思的话就
    可以弄出3个了.难道真的没有办法吗?
      

  8.   


    BufferedReader br = new BufferedReader(new FileReader("C://aa.txt"));
    String aa = br.readLine();
    String[] bb = aa.split("hello");
    for(int i=1;i<bb.length;i++){
        System.out.print("hello ");
    }
      

  9.   


    import java.net.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.io.*;
    public class c
    {
      public static void main(String[] args)
       {
        try 
        {
            String ur="file:///g:/qq.txt"; 
            URL MyURL=new URL(ur);
            String str;
            URLConnection con=MyURL.openConnection();
            InputStreamReader ins=new InputStreamReader(con.getInputStream(),"gbk");
            BufferedReader in=new  BufferedReader(ins);
            StringBuffer sb = new StringBuffer();
            while ((str=in.readLine())!=null)
            {  
             sb.append(str);
            }
                in.close();    
    Pattern p = Pattern.compile("(hello)");
         Matcher m  = p.matcher(sb.toString());
         m.matches();
    while(m.find())
    {
      System.out.print("  "+m.group(1));     }
         }
        catch (MalformedURLException mfURLe) {
          System.out.println("MalformedURLException: " + mfURLe);
        } 
        catch (IOException ioe) {
          System.out.println("IOException: " + ioe);
      
        }
      }
    }