第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();
}
// 将数组元素按有小到大顺序排列
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();
// 创建FileInputStream和DataInputStream 输入流
FileInputStream fout2 = new FileInputStream(file);
DataInputStream dout2 = new DataInputStream(fout2);
// 利用输入流从文件读取数据并输出
for (int i =0;i<data.length;i++){
data[i]=dout2.readInt();
if(i==data.length-1){
System.out.print(data[i]);
}else {
System.out.print(data[i]+"<");
}
}
// 关闭输入流
dout2.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 out =new FileWriter(file);
out.write(a,0,a.length);
out.flush();
out.close();
/********** End **********/
// 利用字符输入流FileReader读取文件,将密文输出
/********** Begin **********/
int n;
FileReader in1 =new FileReader(file);
char[]buf=new char[10];
System.out.println("密文:");
while (( n =in1.read(buf))!=-1){
String s=new String(buf);
System.out.print(s);
}
in1.close();
/********** End **********/
// 利用字符输入流FileReader读取文件,将密文转换为明文输出
/********** Begin **********/
FileReader in2 =new FileReader(file);
System.out.println("\n明文:");
while ((n = in2.read(buf))!=-1){
for (int i = 0; i < n; i++) {
buf[i]= (char)(buf[i] ^'q');
}
String s= new String(buf);
System.out.print(s);
}
in2.close();
/********** End **********/
}
}
第3关 简单TCP通信
客户端
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",8000);
/********** 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();
}
}
服务器
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(8000);
/********** 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通信实现奇偶数判断
客户端
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",8000);
/********** 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();
}
}
}
服务器
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(8000);
/********** 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();
}
}
}
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("数据读取异常");
}
}
}
标签:Java,Socket,java,编程,流式,io,new,import,DataOutputStream
From: https://www.cnblogs.com/Xqiao/p/18216297