您的位置:首页 > 其它

统计文件中单词出现的行号及打印出该行内容

2016-11-01 13:33 295 查看
package com.company;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws IOException
{

File f = new File("c:/code.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String strRead = null;
List<String> lstLines = new ArrayList<String>();
int nLineNum = 1;
while((strRead=br.readLine()) != null)
{
lstLines.add(strRead);
}
System.out.println("请输入要查找的单词");
Scanner sc = new Scanner(System.in);
String strWord = sc.nextLine();
for(String strTemp : lstLines)
{
boolean b = ContainsStr(strTemp,strWord);
if(b)
{
System.out.println(nLineNum + ":" + strTemp);
}
nLineNum++;
}
}
public static boolean ContainsStr(String s1, String s2)
{
if(s1.indexOf(s2) >=0 )
return true;
else
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐