用java写一个程序,在txt中搜索某字符串(出现多次),输出它出现的所有位置(注:txt中有空行)
展开全部
public class FindString{
/**
* fileName 要查找的文本,str要查找的字符串
**/
public int findString(String fileName,String str){
int count = 0;
try {
int line = 0;//记录行数
BufferedReader br = new BufferedReader(new FileReader(fileName));
String tmp = br.readLine();
while(tmp!=null){
line ++;
int index = tmp.indexOf(str, 0);
while(index>=0){
count ++;
System.out.println("在"+line+"行,第"+index+"列,找到字符:"+str);
index = tmp.indexOf(str, index+str.length());
}
tmp = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return count;
}
}
/**
* fileName 要查找的文本,str要查找的字符串
**/
public int findString(String fileName,String str){
int count = 0;
try {
int line = 0;//记录行数
BufferedReader br = new BufferedReader(new FileReader(fileName));
String tmp = br.readLine();
while(tmp!=null){
line ++;
int index = tmp.indexOf(str, 0);
while(index>=0){
count ++;
System.out.println("在"+line+"行,第"+index+"列,找到字符:"+str);
index = tmp.indexOf(str, index+str.length());
}
tmp = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return count;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class StringSearch {
static int row = 0;
static int count = 0;
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String str = scan.next();
String filename = scan.next();
File file = new File(filename);
FileReader reader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(reader);
String s = null;
while((s = bufReader.readLine()) != null){
row ++;
while(s.indexOf(str) != -1){
count++;
int sline = s.indexOf(str) + 1;
System.out.println("匹配位置:行"+row + "列"+sline);
s = s.substring(s.indexOf(str) + str.length());
}
}
if(0 == count){
System.out.println("没有匹配的字符串");
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class StringSearch {
static int row = 0;
static int count = 0;
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String str = scan.next();
String filename = scan.next();
File file = new File(filename);
FileReader reader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(reader);
String s = null;
while((s = bufReader.readLine()) != null){
row ++;
while(s.indexOf(str) != -1){
count++;
int sline = s.indexOf(str) + 1;
System.out.println("匹配位置:行"+row + "列"+sline);
s = s.substring(s.indexOf(str) + str.length());
}
}
if(0 == count){
System.out.println("没有匹配的字符串");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询