学JavaSeven

线程:

继承Thread -> 重写run -> 调用start

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                for (int i=1;i<=6;i++) {
                    try {
                        Thread.sleep(500);// 先等0.5秒
                        System.out.println("i--->" + i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }// 线程任务
        };//匿名内部类
        thread.start();//开启子线程

        //主线程直接快速跑完
        for (int j=1;j<=6;j++) {
            System.out.println("j--->" + j);
        }
    }
}

例子:

  1. 程序(Program) = 餐厅的菜谱(静态的代码)。
  2. 进程(Process) = 餐厅本身(拥有厨房、桌椅、食材等资源)。
  3. 线程(Thread) = 餐厅里的服务员/厨师(真正干活的人)。
线程的几个核心特点:
  • 轻量级:开一个餐厅(进程)很贵,但多雇一个服务员(线程)很便宜。线程共享同一个进程的资源(内存、文件等),不需要重新分配一大块地盘。
  • 并发干活:一个餐厅可以有好几个服务员同时上菜、点单、擦桌子,这就是多线程并发。
  • 切换快:服务员之间换活儿(上下文切换)比换一家餐厅(进程切换)要快得多。

IO流:

该代码copy 方法只支持本地文件路径,不支持网络 URL

复制到的文件dest要加后缀。

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        copy("C:/Users/天/Downloads/学习通.lnk", "D:/学习通.lnk", 1024);

    }
    public static void copy(String src, String dest, int n) {
        InputStream in = null;
        OutputStream out = null;

        byte[] buf = new byte[n];
        int len;
        try {
            in = new FileInputStream(src);//只能读取本地硬盘上的文件,比如 C:/Users/天/Desktop/xxx.jpg
            out = new FileOutputStream(dest);//必须指定完整的目标文件名(比如 C:/Users/天/Pictures/download.jpg)
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 规范关闭流:先关输出流,再关输入流
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
支持网络 URL,复制图片到文件夹里面
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        downloadImage(" https://img2.baidu.com/it/u=1293741573 ,2278362301&fm=253&app=138&f=JPEG?w=800&h=1350","C:/Users/天/Pictures/Screenshots/downloadImage.jpg",1024);
    }
    public static void downloadImage(String src, String dest, int n) {
        InputStream in = null;
        OutputStream out = null;

        byte[] buf = new byte[n];
        int len;
        try {
            //1.打开网络连接
            URL url = new URL(src);
            URLConnection conn = url.openConnection();
            //设置超时,防止程序卡死
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(10000);
            in = conn.getInputStream();

            //2.写入本地文件
            out = new FileOutputStream(dest);
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
            System.out.println("图片下载完成!路径:" + dest);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

网上普通照片 → 统一写 .jpg;截图 / 透明图 → 写 .png
视频下载到文件
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        downloadFile("https://weibo.com/1714145293/R6uHalN2s?pagetype=profilefeed","D:/视频.mp4",4096);
    }
    public static void downloadFile(String src, String dest, int n) {
        InputStream in = null;
        OutputStream out = null;

        byte[] buf = new byte[n];
        int len;
        try {
            //1.打开网络连接
            URL url = new URL(src);
            URLConnection conn = url.openConnection();
            //设置超时,防止程序卡死
            conn.setConnectTimeout(8000);
            conn.setReadTimeout(15000);
            // 模拟浏览器请求,防止拦截
            conn.setRequestProperty("User-Agent","Mozilla/5.0");
            in = conn.getInputStream();

            //2.写入本地文件
            out = new FileOutputStream(dest);
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
            System.out.println("视频下载完成!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2026-07-05 21:08  -柘  阅读(12)  评论(0)    收藏  举报