简体   繁体   English

Java在文本文件中搜索文本

[英]Java search for text in text file

I'm trying to create a small Java program that can open programs on command, now, this is a very slow process since it looks through the whole C drive. 我正在尝试创建一个小的Java程序,该程序可以在命令中打开程序,现在,这是一个非常缓慢的过程,因为它遍历整个C驱动器。

Now, I wanted to know, if I were to make it to save the directory it found the program you had it open and write it into a .txt file, how could I make it so that if you ask the program to open the program again it checks if it's in the .txt file first and if it's not it searches through C again. 现在,我想知道,如果要使其保存到找到打开了该程序的目录并将其写入.txt文件中,那么如何制作它,以便如果您要求该程序打开该程序再次检查它是否首先在.txt文件中,如果不是,则再次搜索C。

This is about what I want: 这是关于我想要的:

User: Open notepad Program: Opening notepad =starts searching on C:/ for notepad.exe= Program: I opened notepad for you! 用户:打开记事本程序:打开记事本=开始在C:/上搜索notepad.exe =程序:我为您打开了记事本! =notepad opens and the directory of notepad.exe is saved in a txt file= = notepad打开,notepad.exe目录保存在txt文件中=

Then later 然后再

User: Open notepad Program: Opening notepad =checks the txt file for a directory with notepad.exe in it= 用户:打开记事本程序:打开记事本=检查txt文件中是否包含notepad.exe的目录=

What you want is a key/value pairing system. 您想要的是键/值配对系统。 There's countless ways to implement that, but if you're running off of a .txt file and parsing it to a String, I would use String.split . 有无数种实现方法,但是如果您要运行.txt文件并将其解析为String,则可以使用String.split

For example, you can set your .txt file up to save in the following syntax: 例如,您可以将.txt文件设置为使用以下语法进行保存:

programName:path,anotherProgramName:anotherPath,(etc, etc)

Then, after reading the .txt file, call: 然后,在读取.txt文件后,调用:

String string = //your file import;
String[] values = string.split(",");

for(int i = 0; i < values.length; i+){
    String[] currentItem = values[i].split(":");
    if(currentItem[0] == fileNameWeAreLookingFor){
        return currentItem[1];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM