Io流,多线程,网络编程的融合练习

package doit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Timer;
import java.util.TimerTask;

//实现从客户端的东西传到服务端,同时从后台复制文件到指定目录
public class mainStart {
	static FileInputStream in = null;

	public static void main(String[] s) {
		File fin = new File("d:\\ThreadPrint.java");
		File fout = new File("e:\\ThreadPrint.java");
		FileD fd = new FileD(fin, fout);
		fd.writeCopy();
		try {
			in = new FileInputStream(fin);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 服务端启动
		new Thread(new Runnable() {
			public void run() {
				new ServerSocketD(9000).read();
			}
		}).start();
		// 客户端启动(体现一下延时的上传)
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				new SocketD("192.168.1.103", 9000, in).write();
			}
		}, 1000);
		// new Thread(new Runnable() {
		// @Override
		// public void run() {
		// new SocketD("192.168.1.103", 9000, in).write();// 客户端启动
		// }
		// }).start();

		fd.closeInputStream();
		fd.closeOutputStream();

	}
}

 

 

package doit;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//文件操作类
public class FileD {
	BufferedOutputStream bo = null;
	BufferedInputStream bi = null;

	File in = null;

	FileD(File in, File out) {
		this.in = in;
		reset();
		try {
			bo = new BufferedOutputStream(new FileOutputStream(out));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	// read
	public void read() {
		int len = 0;
		byte[] by = new byte[1024 * 9];
		try {
			while ((len = bi.read(by)) != -1) {
				System.out.println(new String(by, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		reset();

	}

	// write
	public void writeCopy() {
		reset();
		int len = 0;
		byte[] by = new byte[1024 * 9];
		try {
			while ((len = bi.read(by)) != -1) {
				bo.write(by, 0, len);
				bo.flush();

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		reset();

	}

	// reset()重置bi输入流
	public void reset() {
		try {
			bi = new BufferedInputStream(new FileInputStream(in));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}

	// 自定义关闭输入流
	public void closeInputStream() {
		try {
			bi.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 自定义关闭输出流
	public void closeOutputStream() {
		try {
			bo.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

package doit;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端类

public class ServerSocketD {
	ServerSocket ss = null;
	Socket s = null;

	ServerSocketD(int port) {
		try {
			ss = new ServerSocket(port);
			s = ss.accept();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 数据来源

	public void read() {
		InputStream in = null;
		try {
			in = s.getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		while (true) {
			int len = 0;
			byte[] by = new byte[1024 * 9];
			try {
				while ((len = in.read(by)) != -1) {
					System.out.println(new String(by, 0, len));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}

 

package doit;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketD {
	Socket s = null;
	FileInputStream in = null;
	OutputStream out = null;

	public SocketD(String inet, int port, FileInputStream in) {
		try {
			s = new Socket(inet, port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.in = in;

	}

	//
	public void write() {
		try {
			out = s.getOutputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		int len = 0;
		byte[] by = new byte[1024 * 9];
		try {
			while ((len = in.read(by)) != -1) {
				out.write(new String(by, 0, len).getBytes());
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

 

转载于:https://my.oschina.net/u/3384770/blog/889417

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值