java简单搜索代码 java实现搜索

java编写简单的搜索方法如何实现?

可以使用全文java搜索引擎来实现。你可以去研究一下Lucene。基本上是开源搜索引擎中的标准。

成都创新互联公司是一家专业提供五华企业网站建设,专注与网站建设、网站设计、H5页面制作、小程序制作等业务。10年已为五华众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

Java如何通过网络进行寻找附近的设备,附源码?

在 Java 中,可以通过使用 Java 的网络编程技术来实现查找附近的设备。具体的做法如下:

获取本机的 IP 地址和子网掩码,以计算出本机所在网络中的 IP 地址范围。

使用 for 循环和 InetAddress 类扫描网络中的每一个 IP 地址。

对于每一个 IP 地址,使用 isReachable() 方法判断其是否可达,如果可达则表明该 IP 地址对应的设备存在。

以下是一份简单的示例代码:

import java.net.InetAddress;

import java.net.UnknownHostException;

public class FindDevices {

public static void main(String[] args) throws UnknownHostException {

InetAddress localHost = InetAddress.getLocalHost();

String hostAddress = localHost.getHostAddress();

String subnet = hostAddress.substring(0, hostAddress.lastIndexOf(".") + 1);

for (int i = 1; i 256; i++) {

String host = subnet + i;

try {

InetAddress address = InetAddress.getByName(host);

if (address.isReachable(1000)) {

System.out.println(host + " is reachable");

}

} catch (Exception e) {

System.out.println(host + " is not reachable");

}

}

}

}

请注意,这是一份示例代码,其中的扫描范围和扫描方法可能不是最佳的,根据实际需要进行修改。

用java写一个程序,从一个很长的字符串中搜索出某一段字符,列出所有符合的字符

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RecE {

Pattern pattern;

Matcher matcher;

/* ps:

字符类

[abc] a、b 或 c(简单类)

[^abc] 任何字符,除了 a、b 或 c(否定)

[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)

[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)

[a-z[def]] d、e 或 f(交集)

[a-z[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)

[a-z[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)

预定义字符类

. 任何字符(与行结束符可能匹配也可能不匹配)

\d 数字:[0-9]

\D 非数字: [^0-9]

\s 空白字符:[ \t\n\x0B\f\r]

\S 非空白字符:[^\s]

\w 单词字符:[a-zA-Z_0-9]

\W 非单词字符:[^\w]

*/

public void getMatchedPattern(String regx,String source){

pattern = Pattern.compile(regx);

matcher = pattern.matcher(source);

int index = 1;

while(!matcher.hitEnd()){

if(matcher.find()){

System.out.println("找到第"+index +"个"+matcher.group());

index++;

}

}

System.out.println("一共找到"+(index-1)+"组匹配项");

}

public static void main(String[] args) {

new RecE().getMatchedPattern("love","ilove12what i love23e tolove dolove a efe");

}

}

结果:

找到第2个love

找到第3个love

找到第4个love

一共找到4组匹配项

java如何实现搜索功能。比如,输入txt就能搜索出这个文件夹内所有txt格式的文件。请给完整代码。

import java.io.*;

public class FileDemo{

public static void main(String[] args)throws Exception{

//第一个参数是文件路径,第二个参数是要搜索的文件扩展名

getFile("D:\\JavaDemo",".txt");

}

private static void getFile(String pathName, final String endsWith)throws Exception{

File file = new File(pathName);

if(!file.exists())

throw new RuntimeException("文件不存在,你检索个P呀。");

file.listFiles(new FileFilter(){

public boolean accept(File file){

if(file.getName().endsWith(endsWith)){

System.out.println(file.getName());

return true;

}else

return false;

}

});

}

}


标题名称:java简单搜索代码 java实现搜索
浏览路径:http://ybzwz.com/article/ddogdij.html