Java 如何读取文本文件中的特定单词并使用条件语句执行操作

Java 如何读取文本文件中的特定单词并使用条件语句执行操作,java,netbeans,try-catch,bufferedreader,fileinputstream,Java,Netbeans,Try Catch,Bufferedreader,Fileinputstream,我不熟悉java编码。有人能帮我修改密码吗?我目前正在制作一个程序,在jTextArea中输入一个字符串,如果输入的单词与文本文件中的匹配,那么它将执行一些操作 例如:我输入单词“Hey”,当输入的单词与文本文件匹配时,它会打印类似“Hello”的内容 我希望你明白我的意思 这是我的密码: String line; String yo; yo = jTextArea2.getText(); try ( InputStream fis = new File

我不熟悉java编码。有人能帮我修改密码吗?我目前正在制作一个程序,在jTextArea中输入一个字符串,如果输入的单词与文本文件中的匹配,那么它将执行一些操作

例如:我输入单词“Hey”,当输入的单词与文本文件匹配时,它会打印类似“Hello”的内容

我希望你明白我的意思

这是我的密码:

String line;
    String yo;
    yo = jTextArea2.getText();

    try (
        InputStream fis = new FileInputStream("readme.txt");
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
    ) 
    {
        while ((line = br.readLine()) != null) {

            if (yo.equalsIgnoreCase(line)) {
                System.out.print("Hello");
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(ArfArf.class.getName()).log(Level.SEVERE, null, ex);
    }

不能对行使用等号,因为行包含许多单词。您必须修改它以在一行中搜索单词的索引

       try (InputStream fis = new FileInputStream("readme.txt");
                InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
                BufferedReader br = new BufferedReader(isr);) {
            while ((line = br.readLine()) != null) {
                line = line.toLowerCase();
                yo = yo.toLowerCase();
                if (line.indexOf(yo) != -1) {
                    System.out.print("Hello");
                }
                line = br.readLine();
            }
        } catch (IOException ex) {
        }

由于您是java新手,我建议您花一些时间学习Java8,它可以编写更多干净的代码。下面是用Java8编写的解决方案,希望能给大家一种帮助

        String yo = jTextArea2.getText();
        //read file into stream, 
        try (java.util.stream.Stream<String> stream = Files.lines(Paths.get("readme.txt"))) {

            List<String> matchLines = stream.filter((line) -> line.indexOf(yo) > -1).collect(Collectors.toList()); // find all the lines contain the text
            matchLines.forEach(System.out::println); // print out all the lines contain yo

        } catch (IOException e) {
            e.printStackTrace();
        }
String yo=jtextraea2.getText();
//将文件读入流,
try(java.util.stream.stream=Files.lines(path.get(“readme.txt”)){
List matchLines=stream.filter((line)->line.indexOf(yo)>-1.collect(Collectors.toList());//查找包含文本的所有行
matchLines.forEach(System.out::println);//打印出包含yo的所有行
}捕获(IOE异常){
e、 printStackTrace();
}

请从代码中删除line=br.readLine(),因为该行是在while((line=br.readLine))中分配的。如果这样做,将跳过某些行。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordFinder {
    public static void main(String[] args) throws FileNotFoundException {
        String yo = "some word";
        Scanner scanner = new Scanner(new File("input.txt")); // path to file
        while (scanner.hasNextLine()) {
            if (scanner.nextLine().contains(yo)) { // check if line has your finding word
                System.out.println("Hello");
            }
        }
    }
}