java 查找一个TXT文件内容。
在test.txt中“Thefacecanspeakathousandemotionsbutitcaneasilymaskwhatthehearttrulyfeels.T...
在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语句该怎么写啊?
展开
5个回答
展开全部
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));
}
}
展开全部
io流读取文件
通过indexof()可以返回查找的内容的序号
如果有需要可以继续追问!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用正则表达式
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);
}
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,代码如下
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();
}
}
}
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();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用正则表达式
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);
}
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);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询