Java BIO
BIO 全称Block-IO 是一种同步且阻塞的通信模式。是一个比较传统的通信方式,模式简单,使用方便。但并发处理能力低,通信耗时,依赖网速。
BIO编程简单流程
1)服务器端启动一个ServerSocket
2)客户端启动Socket对服务器进行通信,默认情况下服务器端需要对每个客户 建立一个线程与之通讯
3)客户端发出请求后, 先咨询服务器是否有线程响应,如果没有则会等待,或者被拒绝
4)如果有响应,客户端线程会等待请求结束后,在继续执行
代码展示
//服务端等待客户端接入部分
class BIOSever {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动");
while (true){
System.out.println("等待客户端连接.....");
Socket socket = serverSocket.accept();
System.out.println("客户端连接");
new Thread(new ClientThread(socket)).start();
}
}
}
//用于接收和处理客户端信息的线程
public class ClientThread implements Runnable{
Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
handler(socket);
}
public static void handler(Socket socket){
try{
InputStream inputStream = socket.getInputStream();
byte[] b = new byte[1024];
while (true){
System.out.println("等待客户端输入.....");
int read = inputStream.read(b);
if (read != -1){
System.out.println(new String(b, 0, read));
}else {
break;
}
}
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//客户端部分,就经典行传
public class BIOClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
String message = scanner.nextLine();
if ("exit".equals(message)) {
break;
}
outputStream.write(message.getBytes());
}
outputStream.close();
socket.close();
}
}
File类
Java为文件这块的操作而设计的类
一般来说有四种获取File对象的构造方法
//这里是三种获取File对象的方法,还有一种是通过url的方式new的,是用来搞爬虫来着,不太记得了。
File file1 = new File("D:\\desktop\\a.txt");//以文件路径new File对象非
File file2 = new File ("D:\\desktop","a.txt");//以文件所在目录和文件名new File对象
File parentFile = new File("D:\\desktop");//这里new 了一个文件夹
File file3 = new File(parentFile,"a.txt");//以new好的文件夹File和文件名new 新的File对象
常用的方法
public String getPath()//获取文件的路径
public String getName()}//获取文件名,包含后缀名
public String getParent()//获取文件所在目录路径
public boolean isAbsolute()//检测该实例对象是否为绝对路径
public String getAbsolutePath()//获取实例对象代表的文件的绝对路径
public boolean exists()//判断实例对象代表的文件是否存在
public boolean delete()//删除文件或文件夹,不过删除文件夹,首先得保证文件夹中没有文件才行
demo
File file1 = new File("D:\\desktop\\a.txt");
System.out.println("file1对象所对应的文件是否存在:"+file1.exists());
System.out.println("文件路径为:"+file1.getPath());
System.out.println("文件名为:"+file1.getName());
System.out.println("文件目录为:"+file1.getParent());
System.out.println("该路径是否为绝对路径:"+file1.isAbsolute());
System.out.println("该文件的绝对路径为:"+file1.getAbsolutePath());
System.out.println("是否可以删除:"+file1.delete());
result
利用FIle类实现文件传输
String url1 = "D:\\desktop\\ig\\src\\page\\image\\face.png";
String url2 = "D:\\desktop\\face.png";
File dest1 = new File(url1);//文件来源
File dest2 = new File(url2);//目的地
InputStream inputStream = null;
OutputStream outputStream = null;
inputStream = new FileInputStream(dest1);
outputStream = new FileOutputStream(dest2);
byte[] flush = new byte[1024];
int len = 0;
while ((len = inputStream.read(flush)) != -1) outputStream.write(flush, 0, len);
outputStream.flush();
outputStream.close();
inputStream.close();