- 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;
- }
- }
[java practices]查找字符串出现的位置
最新推荐文章于 2023-06-18 23:00:56 发布
本文介绍了一个使用Java实现的基本程序,该程序可以读取指定路径下的文本文件,并按行存储到内存中。此外,程序还提供了一项功能,允许用户输入特定的单词进行搜索,显示包含该单词的所有行及其对应的行号。
1920

被折叠的 条评论
为什么被折叠?



