java 查找一个TXT文件内容。

在test.txt中“The face can speak a thousand emotions but it can easily mask what the heart truly feels. The happiest face may be masking the most hurting heart”。这么一段话我想查找ha开头这个单词(在一段文字中只有一个ha开头的词,但我查找到的一定是要单词词)。java语句该怎么写啊?

1、定义读取txt文件的目录路径

2、通过 File文件流逐行读取文件内容

--

直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class txttest {
    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file){
        String result = "";
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                result = result + "\n" +s;
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    
    public static void main(String[] args){
        File file = new File("D:/luceneData/test1.txt");
        System.out.println(txt2String(file));
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-09-16

    io流读取文件

    通过indexof()可以返回查找的内容的序号

    如果有需要可以继续追问!

第2个回答  2015-08-08
用正则表达式
public static void main(String[] args) {
String model = "The face can speak a thousand emotions but it can easily mask what the heart truly feels. The happiest face may be masking the most hurting heart";
Pattern p = Pattern.compile("\\W(ha.*?)\\W");
Matcher m = p.matcher(model);
while (m.find()) {
System.out.println(m.group(1));
}

System.out.println(model);
}
第3个回答  2015-08-20
你好,代码如下
package com.zj.demo.test1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestFile {
public static void main(String[] args) {
int db2Name;
String fname = "c:\\1.txt";

File f = new File(fname);
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();

while (s != null) {
int num = s.indexOf("s");// 获得s所在的下标,
// 让后看你要的应该是s开始到di8个
if (num != -1) {
String str = null;
try {
str = s.substring(num, num + 8);
} catch (Exception e) {
str = s.substring(num);
}
System.out.println(str);// 这个就应该是你要的东西了
}
s = br.readLine();
}

br.close();// 关闭缓冲读入流及文件读入流的连接.
System.out.println("a");
} catch (FileNotFoundException e1) {
System.err.println("File not found: " + fname);
} catch (IOException e2) {
e2.printStackTrace();
}
}

}
第4个回答  推荐于2016-02-08
用正则表达式
public static void main(String[] args) {
String model = "The face can speak a thousand emotions but it can easily mask what the heart truly feels. The happiest face may be masking the most hurting heart";
Pattern p = Pattern.compile("\\W(ha.*?)\\W");
Matcher m = p.matcher(model);
while (m.find()) {
System.out.println(m.group(1));
}

System.out.println(model);
}本回答被提问者采纳
相似回答