Java实验六: Java流式编程与网络程序设计(头歌)
一、字节输入/输出流实现数据的保存和读取
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();
}
// 将数组元素按有小到大顺序排列
Arrays.sort(data);
try {
// 创建数据保存文件,如果文件不存在,重新创建
File file = new File("data.txt");
if (!file.exists()) {
file.createNewFile();
}
// 创建FileOutputStream和DataOutputStream 输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
// 利用输出流向文件中写入数组数据
for (int datum : data) {
dataOutputStream.writeInt(datum);
}
// 关闭输出流
dataOutputStream.close();
// 创建FileInputStream和DataInputStream 输入流
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
// 利用输入流从文件读取数据并输出
for (int i = 0; i < data.length; i++) {
System.out.print(dataInputStream.readInt());
if (i != data.length - 1) {
System.out.print("<");
}
}
System.out.println();
// 关闭输入流
dataInputStream.close();
} catch (IOException e) {
// 异常处理
System.out.println("读写发生异常");
}
/********** End **********/
}
}
二、字符输入/输出流实现发送电报
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 fileWriter = new FileWriter(file);
fileWriter.write(a, 0, a.length);
fileWriter.flush();
fileWriter.close();
/********** End **********/
// 利用字符输入流FileReader读取文件,将密文输出
/********** Begin **********/
FileReader fileReader = new FileReader(file);
char[] buffer = new char[10];
int x; //保存读取的数据量
System.out.println("密文:");
while ((x = fileReader.read(buffer)) != -1) {
System.out.print(new String(buffer));
}
fileReader.close();
/********** End **********/
// 利用字符输入流FileReader读取文件,将密文转换为明文输出
/********** Begin **********/
FileReader in = new FileReader(file);
System.out.println("\n明文:");
while ((x = in.read(buffer)) != -1) {
for (int i = 0; i < x; i++) {
buffer[i] = (char)(buffer[i] ^ 'q');
}
System.out.print(new String(buffer));
}
in.close();
/********** End **********/
}
}
三、简单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 {
@Override
public 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();
}
}
}
四、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 {
@Override
public void run() {
try {
/********** Begin **********/
//创建服务器端ServerSocket(ss),指定端口号8000
ServerSocket 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;
}
@Override
public 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("数据读取异常");
}
}
}
本文来自博客园,作者:Cloudservice,转载请注明原文链接:https://www.cnblogs.com/whwh/p/18215594,只要学不死,就往死里学!