例如关1.txt中的内容是12334afasf我爱你死我活无可厚非
如何用JAVA实现提取其中的我爱你

解决方案 »

  1.   

    读取文本文件,然后解析,可以使用正则表达式提取特定串。
    File file = new File(path);
    FileInputStream fileIns = new FileInputStream(file);
    java.io.InputStreamReader isReader = new java.io.InputStreamReader(fileIns)
    java.io.BufferedReader br = new java.io.BufferedReader(isReader);然后使用br.readLine()就可以一行一行读取文本文件。
      

  2.   

    我写的函数:
    import java.io.*;
    static final int BUFSIZE = 102400; public static InputStream InputStreamFromFile(String path) throws Exception{
    File f = new File(path);
    if (f.exists() == false){ 
    throw new Exception(path + " not exists!");
    }
    return new FileInputStream(f);
    }
    public static String getStrFromIS(InputStream is) throws Exception{
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    int read;
    byte[] bytes = new byte[BUFSIZE];
    while ((read = is.read(bytes)) != -1) {
    os.write(bytes, 0, read);
    }
    return new String(os.toByteArray());
    } ===================
    调用
    String str = getStrFromIS(InputStreamFromFile("c:\test.txt"));
    这时str的内容就是"12334afasf我爱你死我活无可厚非"
    你调用String相关的函数就可以取str内的任意内容,具体查看API帮助.
      

  3.   

    比如用str.indexOf("我爱你") 可以找到("我爱你") 的位置,用str.substring(...)可以取出来,这些字符串操作函数你查一下帮助就知道了.