JAVA高级应用之文件切割-加密图片\视频与初识线程

练习1

使用System.in + BufferedReader + PrintWriter 实现键盘写入文件
输入quit 结束输入文件
字节流 转换到 字符流 inputStreamReader
public static void main(String[] args) throws IOException {
    InputStream in = System.in;  // 字节流父类接收
    InputStreamReader isr = new InputStreamReader(in);  // 将字节流转换为字符流
    BufferedReader br = new BufferedReader(isr);  // 字符缓冲输出流
    PrintWriter pw = new PrintWriter(new File("/Users/lanou/Desktop/test3/kkk.txt"));  // 字符打印流
    String string = null;
    while((string = br.readLine()) != null) {
        if(string.equals("quit")) {
            break;
        }
        pw.write(string);
    }
    br.close();  // 关闭流
    pw.close();
}

导入外来包 调用里面的方法

包是别人建好的,里面封装了许多有用的方法,我们可以将此包导入,建立路径,然后使用里面的方法,
有助于我们编写代码

学习方法:
1.先找类 找对应的类(先看后缀)
2.看该类如何创建对象(获取对象)
3.看方法名 揣测方法的用意
4.测试看结果 然后整理在方法集中

commons-io 第三方源 jar包
commons-io-2.4-sources
commons-io-2.4.jar

获取路径扩展名 .txt .png
static String getExtension(String filename)
获取文件的名字
static String getName(String filename)
判断是不是这个扩展名
static boolean isExtension(String filename,String extension)

复制文件夹
static void copyDirectoryToDirectory(File src,File desc)
复制文件
static void copyFile(File src,File desc)
写入字符串到文件
static void writeStringToFile(File src,String date)
读取文件按字符串
static String readFileToString(File src)

写入文件 可以选取用什么字节流写入
static void write(String data, OutputStream output)
读取文件到集合中以字符串形式
static List<String> readLines(InputStream input)

代码示例:

public static void main(String[] args) throws IOException {
    String extension = FilenameUtils.getExtension("/Users/lanou/Desktop/test3/kkk.txt");
    System.out.println("扩展名:" + extension);
    String name = FilenameUtils.getName("/Users/lanou/Desktop/test3/kkk.txt");
    System.out.println("名字:" + name);
    boolean b = FilenameUtils.isExtension("txt","/Users/lanou/Desktop/test3/kkk.txt");
    System.out.println("是吗?:" + b);

    FileUtils.copyDirectoryToDirectory(new File("/Users/lanou/Desktop/test3"), new File("/Users/lanou/Desktop/test3/fff"));
    FileUtils.copyFile(new File("/Users/lanou/Desktop/test3/kkk.txt"), new File("/Users/lanou/Desktop/test3/kkk1111.txt"));
    FileUtils.writeStringToFile(new File("/Users/lanou/Desktop/test3/kkk.txt"), "中国最伟大");
    FileUtils.readFileToString(new File("/Users/lanou/Desktop/test3/kkk.txt"));

    FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/kkk.txt");
    IOUtils.write("我爱我的住过", fos);
    List readLines = IOUtils.readLines(new FileInputStream(new File("/Users/lanou/Desktop/test3/kkk.txt")));
    for (String string : readLines) {
        System.out.println(string);
    }
}

SequenceInputStream 合并流

SequenceInputStream(EnumerationInputStream> e)
合并功能(可以把多个流读出(合并)一个流)
构造方法:
参数 是迭代器 是Vector特有的
     该Vector要保存的是InputStream的子类
参数:传两个字节输入流
public static void main(String[] args) throws IOException {
    // 创建三个源文件
    File file = new File("/Users/lanou/Desktop/test3/1.txt"); 
    File file1 = new File("/Users/lanou/Desktop/test3/2.txt");
    File file2 = new File("/Users/lanou/Desktop/test3/1.txt");
    // 创建三个字节输入流
    FileInputStream fis = new FileInputStream(file);
    FileInputStream fis1 = new FileInputStream(file1);
    FileInputStream fis2 = new FileInputStream(file2);
    // 创建Vector集合 并 将输入流添加到集合中
    Vector vector = new Vector<>();
    vector.add(fis);
    vector.add(fis1);
    vector.add(fis2);
    // 创建Vector的迭代器 
    Enumeration enumeration = vector.elements();
    // Sequence构造方法 并将迭代器传入
    SequenceInputStream sis = new SequenceInputStream(enumeration);
    // 字节输出流
    FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/5.txt");
    // 数组循环写入   
    int len = 0;
    byte[] b = new byte[1024];
    while((len = sis.read(b)) != -1) {
        fos.write(b);
    }
    // 关闭两个流    
    fos.close();
    sis.close();
} 

代码示例

1.将图片切割成多个1M的文件
2.然后将这些文件合并成一个图片

当程序中的某些图片或者视频不希望别人看到时,可以使用图片切割功能,编写代码,将6.9M的图片
分割为7个1M的文件,后缀名也可改为任意后缀名,保存在一个文件夹中,然后将原完整的图片删除,只保留这些碎片
当你想查看这些视频或者图片的时候,编写代码,进行图片连接,将这些图片连接成一个完整的图片,并改回后缀名,便可顺利查看
public static void main(String[] args) throws IOException {
    //fun1();
    // 创建输入流 读取碎片文件
    FileInputStream fis = null;
    // 创建Vector集合保存碎片文件
    Vector< FileInputStream> vector = new Vector<>();
    for(int i = 1; i < 8; i++) {
    // 创建正确路径
        String string = "/Users/lanou/Desktop/Test2/" + i + ".xxx";
        File file = new File(string);
        fis = new FileInputStream(file);
        // 将碎片文件添加入vector文件中
        vector.add(fis);
    }
    // 创建迭代器
    Enumeration enumeration = vector.elements();
    // 合并流
    SequenceInputStream sis = new SequenceInputStream(enumeration);
    // 改回文件名后缀
    FileOutputStream fos = new
    FileOutputStream("/Users/lanou/Desktop/Test2/x.png");
    int len = 0;
    byte[] b = new byte[1024];
    // 循环读取写入
    while((len = sis.read(b)) != -1) {
        fos.write(b,0,len);
    }
    // 关闭流
    sis.close();
    fos.close();
}

    /**
     * @throws FileNotFoundException
     * @throws IOException
     图片/视频切割
     */
public static void fun1() throws FileNotFoundException, IOException {
    // 创建输入流 连接完整图片
    FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test3/图1.png");
    // 字节缓冲输入流:提高效率
    BufferedInputStream bis = new BufferedInputStream(fis);
    // 按数组容量就行读取 内容保存在数组中
    byte[] b = new byte[1024*1024];
    int num = 1;
    int len = 0;
    // 输出流 将碎片输出指定位置
    BufferedOutputStream bos = null;
    // 循环读取 切割 写入
    while((len = bis.read(b)) != -1) {
    // 字符串连接 创建新的路径 文件名不同,路径相同
        String string = "/Users/lanou/Desktop/Test2/" + num + ".xxx";
        bos = new BufferedOutputStream(new FileOutputStream(string));
        // 写入碎片文件
        bos.write(b, 0, len);
        num++;
        // 关闭流
        bos.close();
    }
    bis.close();
}

线程

进程:一个正在运行的程序(独立运行的程序)
一个进程中可以只有一个线程 单线程程序
一个进程中也可以有多个线程 多线程程序
线程:一个线程 相当于一个cup的执行路径
(大大提升了处理效率)

标准的单线程程序

绝对安全 程序从上到下依次执行 但效率不高
public static void main(String[] args) {
        // 添加
        add();
        // 删除
        remove();
    }
    public static void add() {
        for(int i = 0; i < 100; i++) {
            System.out.println(i);
        }
    }
    public static void remove() {
        System.out.println("删除");
    }
}

理解

代码是如何开启线程
JVM调用main方法--->找操作系统(系统)--->开启一个执行路径
开启一个叫main的执行路径 main就是一个线程 main是线程的名字
又叫主线程
public static void main(String[] args) {
    System.out.println(10/0);
    add();
    System.out.println("程序完成");
}
public static void add() {
    for(int i = 0; i < 100; i++) {
        System.out.println(i);
    }
}

简单调用线程示例

package com.lanou3g.p03;

public class p11 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        for (int i = 0; i < 30; i++) {
            System.out.println("main--->" + i);
        }
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i = 0; i < 30; i++) {
            System.out.println("Thread--->" + i);
        }
    }
}
 *创建Thread子类 NameThread
 * 要求:
 * 成员变量 String name set/get方法 重写run方法
 * 要有(带名字的)构造方法 无参
package com.lanou3g.p03;

public class p12 {

}
class MyThread extends Thread{
    private String mname;
    public MyThread() { 
    }
    public MyThread(String name,String myName) {
        super(name);
        this.mname = myName;
    }
    public String getMname() {
        return mname;
    }
    public void setMname(String mname) {
        this.mname = mname;
    }
    /*
    * 父类已经有了 getName()方法 并且用final修饰 不能被重写 所有报错
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
    }
}

Runnable接口实现线程

package com.lanou3g.p03;

public class Demo13 {
    public static void main(String[] args) {
        MyRunable myRunable = new MyRunable();
        Thread thread = new Thread(myRunable);
        thread.start();
    }
}
class MyRunable implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i = 0; i < 20;i++) {
            System.out.println(i);
        }
    }

}

你可能感兴趣的:(JAVA高级应用之文件切割-加密图片\视频与初识线程)