实验六 Java流式编程与网络程序设计 头歌
制作不易!点个关注!给大家带来更多价值!
第1关 字节输入/输出流实现数据的保存和读取
package step1;import java.io.*;
import java.util.*;public class SortArray {public static void main(String[] args) {/********** Begin **********/// 创建保存整型数据的数组(数组大小10)int[] data= new int[10];// 给数组赋随Scanner sc = new Scanner(System.in);for (int i = 0; i < data.length; i++) {data[i] = sc.nextInt();//data[i]=1+(int)(Math.random()*100);}// 将数组元素按有小到大顺序排列Arrays.sort(data);try {// 创建数据保存文件,如果文件不存在,重新创建File file = new File("data.txt");if (!file.exists()) {file.createNewFile();}// 创建FileOutputStream和DataOutputStream 输出流FileOutputStream fout=new FileOutputStream(file);DataOutputStream dout=new DataOutputStream(fout);// 利用输出流向文件中写入数组数据for(int i=0;i<data.length;i++){dout.writeInt(data[i]);}// 关闭输出流dout.close();fout.close();// 创建FileInputStream和DataInputStream 输入流FileInputStream f=new FileInputStream(file);DataInputStream d=new DataInputStream(f);// 利用输入流从文件读取数据并输出for(int i=0;i<data.length;i++){System.out.print(d.readInt());if(i<data.length-1){System.out.print("<");}}// 关闭输入流d.close();f.close();} catch (IOException e) {// 异常处理System.out.println("读写发生异常");}/********** End **********/}
}
第2关 字符输入/输出流实现发送电报
package step2;import java.io.*;
import java.util.Scanner;public class Encrypt {public static void main(String[] args) throws IOException {// 创建要发送的电报Scanner sc = new Scanner(System.in);String data = sc.next();// 将电报分割成字符数组/********** Begin **********/char[]a=data.toCharArray();/********** End **********/// 打开指定存放电报的文件,如果文件不存在,则创建File file = new File("data.txt");if(!file.exists()) {file.createNewFile();}// 循环遍历字符数组,将每个字符加密处理for (int i = 0; i < a.length; i++) {a[i] = (char) (a[i] ^ 'q');}// 利用字符输出流FileWriter将加密后的字符数组写入文件中/********** Begin **********/FileWriter fw =new FileWriter(file);fw.write(a,0,a.length);fw.close();/********** End **********/// 利用字符输入流FileReader读取文件,将密文输出/********** Begin **********/FileReader fr=new FileReader(file);char[] arr=new char[a.length];fr.read(arr);System.out.println("密文:");System.out.println(arr);/********** End **********/// 利用字符输入流FileReader读取文件,将密文转换为明文输出 /********** Begin **********/for(int i=0 ;i<a.length;i++){arr[i]=(char)(arr[i]^'q');}System.out.println("明文:");System.out.println(arr);/********** End **********/ }
}
第3关 简单TCP通信
Client.java
package step3;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;public class Client {public static void main(String[] args) throws Exception {Server server = new Server();server.start();Scanner sc = new Scanner(System.in);//创建客户端Socket(s),指定服务器端IP地址和端口号/********** Begin **********/Socket s = new Socket("127.0.0.1", 8080);/********** end **********/DataOutputStream dos = new DataOutputStream(s.getOutputStream());DataInputStream dis = new DataInputStream(s.getInputStream());System.out.println(dis.readUTF());String name = sc.next();dos.writeUTF(name);System.out.println(dis.readUTF());s.close();}
}
Server.java
package step3;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class Server extends Thread {@Overridepublic void run() {try {//创建服务器端ServerSocket(ss),指定端口号8000/********** Begin **********/ServerSocket ss = new ServerSocket(8080);/********** end **********/Socket s = ss.accept();DataOutputStream dos = new DataOutputStream(s.getOutputStream());DataInputStream dis = new DataInputStream(s.getInputStream());dos.writeUTF("你已经连上服务器了,告诉我你的姓名...");String name = dis.readUTF();dos.writeUTF("再见:" + name);s.close();} catch (IOException e) {e.printStackTrace();}}
}
第4关 TCP通信实现奇偶数判断
ClientPlus.java
package step4;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;public class ClientPlus {public static void main(String[] args) {Scanner sc = new Scanner(System.in);ServerPlus server = new ServerPlus();server.start();try {//创建客户端Socket(s),指定服务器端IP地址和端口号/********** Begin **********/Socket s = new Socket("127.0.0.1", 8080);/********** end **********/DataOutputStream dos = new DataOutputStream(s.getOutputStream());DataInputStream dis = new DataInputStream(s.getInputStream());//客户端通过循环依次接收服务器返回的结果,并输入新的整数传递给服务器/********** Begin **********/while (true) {System.out.println(dis.readUTF());String num = sc.next();dos.writeUTF(num);}/********** end **********/} catch (EOFException e) {} catch (IOException e) {e.printStackTrace();}}
}
ServerPlus.java
package step4;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class ServerPlus extends Thread {@Overridepublic void run() {try {/********** Begin **********///创建服务器端ServerSocket(ss),指定端口号8000ServerSocket ss = new ServerSocket(8080);/********** end **********/Socket s = ss.accept();DataOutputStream dos = new DataOutputStream(s.getOutputStream());DataInputStream dis = new DataInputStream(s.getInputStream());ReceiveThread thread = new ReceiveThread(s, dos, dis);thread.start();} catch (IOException e) {e.printStackTrace();}}
}
ReceiveThread.java
package step4;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;class ReceiveThread extends Thread {DataOutputStream dos;DataInputStream dis;Socket s;public ReceiveThread(Socket s, DataOutputStream dos, DataInputStream dis) {this.s = s;this.dos = dos;this.dis = dis;}@Overridepublic void run() {try {dos.writeUTF("请输入一个整数,我知道是奇数还是偶数");while(true) {String num = dis.readUTF();if("-1".equals(num)) {s.close();break;} String result = (Integer.parseInt(num)%2==0)?"偶数":"奇数";dos.writeUTF(num + "是..." + result);} } catch (SocketException e) {try {s.close();} catch (IOException e1) {e1.printStackTrace();}} catch (IOException e) {System.out.println("数据读取异常");} }
}