简体   繁体   English

如何在文件中搜索字符串

[英]how to search for string in file

How could I search for string in text file in java? 如何在Java的文本文件中搜索字符串? Would it have to be in a text file or could it read a .ini or some other file type example 它必须在文本文件中还是可以读取.ini或其他文件类型的示例

sound = off
opengl = on
openal = off

I tried searching for it on google but I couldnt really find anything. 我尝试在Google上进行搜索,但找不到任何东西。 Thanks in advance 提前致谢

It looks like you want to use a Properties file. 您似乎要使用属性文件。

You can load one using Properties.load(String path); 您可以使用Properties.load(String path);加载一个Properties.load(String path);

If your file is a properties file or an ini file ( key = value ), you may want to use the Apache Commons Configuration API. 如果文件是properties文件或ini文件( key = value ),则可能要使用Apache Commons Configuration API。 It is much better than the Properties class from the JDK. 它比JDK中的Properties类好得多。

There are tons of information with those typical of questions. 有大量的信息和典型的问题。

Here you have two easy examples: 这里有两个简单的例子:

In short it is easy to load a file into a Properties object, for example to obtain, in your case, the sound value in a example.properties file: 简而言之,很容易将文件加载到Properties对象中,例如,在您的情况下,可以在example.properties文件中获取sound值:

Properties props = new Properties();
props.load(new FileInputStream("example.properties"));
String isOnOrOff = props.getProperty("sound");

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

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