解释这段代码package question7; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.Random; public class Test { public static void main(String args[]){ Test test=new Test(); Scanner input=new Scanner(System.in); String s1=new String(); s1=input.nextLine(); String[] s2 = s1.split(" "); int[] l=new int[s2.length]; for(int i=0 ; i<s2.length;i++){ l[i]=Integer.parseInt(s2[i]); } test.write("test.txt", l); int[] readlist=test.read("test.txt",l); isPrime isprime=new isPrime(readlist); for(int i=1;i<=10;i++){ new Thread(isprime).start(); } try { Thread.sleep(1000); //1000 毫秒,也就是1秒. } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } Set<Integer> set=new HashSet(); set=isprime.getSet(); System.out.println("素数set"+set); System.out.println("输入查询数字"); int num=input.nextInt(); if(set.contains(num)){ System.out.println(num+"是素数"); } else System.out.println(num+"不是是素数"); } public void write(String filename,int a[]){ File file = new File(filename); try { PrintWriter output=new PrintWriter(file); for(int i=0;i<a.length;i++){ output.print(a[i]+" "); } output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int[] read(String filename,int a[]){ File file=new File(filename); try { Scanner input=new Scanner(file); int i=0; while(input.hasNext()){ a[i]=input.nextInt(); i++; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return a; } }
时间: 2023-06-02 20:08:12 浏览: 150
这段代码是一个 Java 程序,主要功能是读取用户输入的一串整数,将其写入文件 test.txt,然后又从这个文件中读取整数列表,并查找出其中的素数并存储在一个 HashSet 中,最后判断用户输入的数字是否为素数。具体实现包括定义了一个 isPrime 类用于判断一个数字是否为素数,使用了多线程来处理多个数字的判断,使用了 HashSet 来存储素数列表。
相关问题
package socket; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class SimpleTCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8885); // 1、创建服务器,指定端口 while(true) { Socket socket = server.accept();// 2、接收客户端连接 阻塞式 Handler handler = new Handler(socket); handler.start(); } } } class Handler extends Thread{ private Socket socket; public Handler(Socket socket) { this.socket = socket; } public void run() { InputStream dis = null; OutputStream dos = null; System.out.println("线程名称:" + Thread.currentThread().getName()); try { dis = socket.getInputStream(); dos = socket.getOutputStream(); byte[] buf = new byte[1024]; int len = dis.read(buf); String msg = new String(buf,0,len); System.out.println("服务端从客户端接收到: " + msg); String echo = "欢迎"; dos.write(echo.getBytes()); // 3、发送数据 dos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { dos.close(); dis.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
这是一个简单的TCP服务器端的Java代码,可以监听客户端连接并接收消息,然后发送欢迎消息。代码流程如下:
1. 创建ServerSocket对象,并指定端口号为8885;
2. 通过ServerSocket对象的accept()方法阻塞式地等待客户端连接,一旦有客户端连接则创建一个Handler对象处理该连接;
3. Handler类继承了Thread类,重写了run()方法,其中获取到了与客户端通信的InputStream和OutputStream对象,通过read()方法读取客户端发送来的数据,并将读取到的字节数据转换为字符串类型后输出到控制台;
4. 将欢迎消息转换为字节数组后通过write()方法发送给客户端,并调用flush()方法刷新输出流;
5. 在finally块中关闭数据输入流、数据输出流和Socket对象。
需要注意的是,这是一个单线程的服务器端程序,每次只能处理一个客户端连接,如果需要同时处理多个客户端连接,则需要使用多线程或NIO技术。
package hadoop9999.training.exp1; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import org.apache.hadoop.io.Writable; /** * Hadoop 序列化和反序列化工具 */ public class HadoopSerializationUtil { /** * 序列化 * @param writable * @return * @throws IOException */ public static byte[] serialize(Writable writable) throws IOException { // create bytes ByteArrayOutputStream ByteArrayOutputStream out = new ByteArrayOutputStream(); // create DataOutputStream DataOutputStream dataout = new DataOutputStream(out); // call write method writable.write(dataout); dataout.close(); // bytes return out.toByteArray(); } /** * 反序列化 * @param writable * @param bytes * @throws Exception */ public static void deserialize(Writable writable, byte[] bytes) throws Exception { // create ByteArrayInputStream ByteArrayInputStream in = new ByteArrayInputStream(bytes); // create DataInputStream DataInputStream datain = new DataInputStream(in); // read fields writable.readFields(datain); datain.close(); } }
这是一个 Hadoop 序列化和反序列化工具类,其中包含了两个静态方法 serialize 和 deserialize。
serialize 方法接收一个实现了 Writable 接口的对象作为参数,通过将其写入 ByteArrayOutputStream 中,最终返回字节数组。
deserialize 方法接收一个实现了 Writable 接口的对象和一个字节数组作为参数,在创建 ByteArrayInputStream 和 DataInputStream 后,通过 readFields 方法将字节数组中的数据读入到对象中。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)