本周有趣的代码

视频的拷贝

package com.softeem.output;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopy {
    
    /**
     * 完成文件拷贝
     * @param source    源文件
     * @param targetDir    目标目录
     */
    public void copy(File source,File targetDir){
        //根据指定的目录以及源文件名称构建新的file对象
        File target = new File(targetDir,source.getName());
        InputStream is = null;
        OutputStream os = null;
        try {
            //创建源文件的输入流
            is = new FileInputStream(source);
            //创建目标文件的输出流
            os = new FileOutputStream(target);
            //声明字节缓冲区
            byte[] b = new byte[1024];
            //声明临时变量存储每次读取的真实长度
            int len = 0;
            System.out.println("开始拷贝...");
            while((len = is.read(b)) != -1){
                
                os.write(b,0,len);
            }
            System.out.println("拷贝完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(os != null){
                    os.close();
                }
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }
    
    public static void main(String[] args) {
        File f1 = new File("D:\\软帝\\1.mp4");
        File f2 = new File("D:\\软帝\\视频"); //d:/视频/1.mp4
        new FileCopy().copy(f1, f2);
    }

}

飞秋小程序

package com.softeem.net.udp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class MyFeiQ {

    public static void main(String[] args) throws IOException {
        String ip = "192.168.4.122";
        //创建基于UDP协议的网络通道
        DatagramSocket ds = new DatagramSocket(8800);
        //获取标准输入流并包装为缓冲流
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DatagramPacket dp = null;
        byte[] b = null;
        String msg = "";
        while(!(msg = br.readLine()).equals("quit")){
            //接受消息
            b = msg.getBytes();
            dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName(ip), 8081);
            ds.send(dp);
            //发送消息
            ds.receive(dp);
            msg = new String(dp.getData());
            System.out.println(dp.getAddress().getHostAddress()+":"+msg);
        }
        
        
    }
}

猜你喜欢

转载自blog.csdn.net/V_mzzj/article/details/81274406